setAutoCommit behaves odd

Hi!

I am currently working on a project where I am providing DB access for others.

My code looks as follows:

privatestatic Connection dbConnection;

privatestatic Statement statement;

private String databaseUrl =null;

private String driver ="com.ibm.db2.jcc.DB2Driver";

.

.

.

private Connection getConnection()throws SQLException{

try{

Class.forName(driver);

}

catch (ClassNotFoundException e){

System.out.println("Database driver not found. Contact system administrator. "

+"Closing down.\n\nFull error log for debugging purposes.");

System.out.println(e.getMessage());

}

return DriverManager.getConnection(databaseUrl, sciName, sciPass);

}

private Statement getStatement(Connection dbConnection)throws SQLException{

return dbConnection.createStatement();

}

publicvoid login(String url, String user, String password)throws SQLException{

sciName = user;

sciPass = password;

databaseUrl = url;

dbConnection = getConnection();

statement = getStatement(dbConnection);

}

privatevoid tableExplode(Container c)throws DBException{

String sql ="";

try{

dbConnection.setAutoCommit(false);

for (int i = 0; i < c.get(0).getNumberofRows(); i++){

//this produces valid SQL statements

sql ="" + INSERTINTO + c.get(0).getName() + OPEN

+ explodeColumns(c.get(0)) + CLOSE + VALUES

+ explodeValues(c.get(0).getRow(i)) + CLOSE;

statement.executeUpdate(sql);

}

dbConnection.commit();

dbConnection.setAutoCommit(true);

}

catch (SQLException e){

try{

dbConnection.rollback();

}

catch (SQLException e1){

thrownew DBException(e.getMessage());

}

thrownew DBException(e.getMessage());

}

}

Everything works fine, apart from the AutoCommit. Even though its definately set to "false" during the for-loop (I checked with the getAutoCommit method) it's not working as intended. With every run of the for-loop the executeUpdate immediately sends a SQL-statement, even though all should happen together when I call commit().

Any help is appreciated!

-Christian

[4129 byte] By [ChrisGoesPlacesa] at [2007-11-27 7:43:33]
# 1

What do you mean by "sends a SQL-statement"? The program will send the SQL statement to the server to be executed regardless of commit mode. The total effects of all the statements can then be rolled back or committed.

If you want to increase performance check out statement batching. Batched statements are sent over the network to the db server in bigger chunks.

Also when concerned about performance a PreparedStatement may make sense when executing the same statement several times with different parameters. That allows the db server to parse and create an execution plan for the statement only once.

sjasjaa at 2007-7-12 19:24:16 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Oh ok, I assumed that AutoCommit on false behaves like statement batching, should've paid more attention.

Prepared statements don't work at all, must be some Problem with the DB2-Server thats running here (which only the supervisors can access, I am in a college environment..)

Anyways, thanks a lot for you help!

ChrisGoesPlacesa at 2007-7-12 19:24:16 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...