File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes autocommit - turning on after transaction is committed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "autocommit - turning on after transaction is committed?" Watch "autocommit - turning on after transaction is committed?" New topic
Author

autocommit - turning on after transaction is committed?

Toby Davis
Ranch Hand

Joined: Apr 09, 2002
Posts: 65
When I turn off autocommit to perform transactions (such as two stored procedure calls involving inserts), is it necessary that I set autocommit(true) after committing?
Example code (using Sun's example, which is close to mine):

Why do I need to set the connection's autocommit back to true at the end? I was advised to do this, but logically, I've already committed the transaction... and have no need for autocommit in my apps.
(Note the connection is from a pooled Datasource)
Thanks
--
Mahesh Mamani
Ranch Hand

Joined: Jun 25, 2001
Posts: 110
Hello,
I do agree that the statement used at the end is not required, but u may make Ur code a bit smarter thur' this:
try {
if (!con.getAutoCommit()) {
con.setAutoCommit(true);
}
}
catch (DBException dbe) {}
Try it n let me know

MSM
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
It doesn't make any difference to the transaction that you have just committed, but it would make a difference to later bits of code using the same connection.
If you're using a connection pool, that's more true.
You might have a piece of code afterwards that doesn't want a transaction and doesn't bother checking that autocommit is true and so never does a commit.


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Toby Davis
Ranch Hand

Joined: Apr 09, 2002
Posts: 65
Got it, thanks for the replies. I have transaction off for all my calls so far - but you never know.
---
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: autocommit - turning on after transaction is committed?