Code: Select all
Connection con = DriverManager.getConnection(dbURL);
//code for enabling transactions
String updateString =
"update SALES set T_AMOUNT = 100 where T_NAME = 'BOB'";
Statement stmt = con.createStatement();
stmt.executeUpdate(updateString);
stmt.executeUpdate("update ACCOUNTS set STATUS=1");
con.commit(); //REPLACE THIS LINE OF CODE
is meant to be replaced by either con.setAutoCommit(false); or con.setAutoCommit(true);//code for enabling transactions
We are including either con.setAutoCommit(false); or con.setAutoCommit(true); here for the sake of this question. Even without either one of them being included here specifically, con.setAutoCommit(true); is set by default in Java for us.
Is that correct?
Question 1
---------------
In the quote immediately above this question, you said "even setAutoCommit(false) without any impact." Why without any impact? Is that because of the following order of reasoning?Now, in this question, it is not clear from the code whether the auto-commit has been set to true or false.
Case 1: If auto-commit was set to true. In this case, every statement is committed after it completes. Therefore, the call to commit() is not actually required and so it can be replaced with setAutoCommit(true) or even setAutoCommit(false) without any impact.
The line indicated by (1) below is followed by
a lot of execute statements and all of them will be committed due to the auto commit being true in the line indicated by (1).
By the time we get to the line indicated by (2) below, all the commits have been made and thus the auto commit being false in (2) won't make any impact because all the work has be done at this point.
Is that correct?
Code: Select all
setAutoCommit(true) // <========== (1)
String updateString =
"update SALES set T_AMOUNT = 100 where T_NAME = 'BOB'";
Statement stmt = con.createStatement();
stmt.executeUpdate(updateString);
stmt.executeUpdate("update ACCOUNTS set STATUS=1");
setAutoCommit(false) // <========== (2)
Schmichael
PS
Sorry about this question being too loaded.