• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setAutoCommit(autocommit);

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mike Farnham
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, the code snippet is shown below:

[ June 17, 2003: Message edited by: JiaPei Jen ]
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic