I have seen code snippets of using connection pool to get a connection to databases. Some snippets include a statement: connection.setAutoCommit(autocommit); but most do not. What does that statement do? What is the difference does it make?
I have not actually seen the code snippet you reference. However, my experience has been that AutoCommit is on by default. If you turn off AutoCommit, then you have explicit control of the "transaction". You are responsible for determining when it is appropriate to commit. Examples where this is useful are: 1. You are loading a large number of records. You have determined that say 100 records is the optimum point to commit the transaction. 2. Also, maybe you are updating multiple tables or even multiple databases. Cheers, Mike The function of the imagination is not to make strange things settled, so much as to make settled things strange. -G.K. Chesterton (1874-1936)
JiaPei, I'm sorry, but I didn't see "setAutoCommit()" anywhere in the code snippet you posted. In any case, I concur with Mike that 'auto commit' is usually on (by default). This means that whenever you modify the database (via an insert, update or delete operation), that change is immediately saved to the database. When 'auto commit' is off, none of the changes you make to the database will be saved -- until you invoke the "commit()" method (in class "java.sql.Connection"). Alternatively, you can decide to 'undo' your changes and invoke the "rollback()" method. If you are using batch updates, then (I believe) you must turn off 'auto commit' while creating the 'batch' and then invoke "commit()" after "executeBatch()". Please see the javadocs for more details on the methods I have mentioned above. Also, if you haven't already, I recommend reading the Getting Started with the JDBC API guide. Hope this has helped you. (Please don't disrespect me again!) Good Luck, Avi.
Honk if you love justice! And honk twice for tiny ads!