Home > Blockchain >  Can I overwrite a statement if autocommit is disabled?
Can I overwrite a statement if autocommit is disabled?

Time:03-01

Lets say I do have code like this:

let connection;
let preparedStatement;

connection = createConnectionSomehow();
connection.setAutoCommit(false);

preparedStatement = connection.prepareStatement(query1);
preparedStatement.executeUpdate();

preparedStatement = connection.prepareStatement(query2);
preparedStatement.executeUpdate();

connection.commit();

Will both queries been executed or will the second query overwrite the first one?

CodePudding user response:

In short - both queries will be executed as a single atomic operation when auto commit is false and you are only committing at the end. This will happen in the order you issues them usually but can depend on which database you are using.

That should be fairly easy to verify if you run the code commenting the operations or updating the same record with both queries.

CodePudding user response:

Connection getConnection() throws SQLException {
Connection con = ... // create the connection
con.setAutoCommit(false);
return con;

}

setAutoCommit(false) call here so that callers of this method never have to worry about setting it.

  • Related