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

