Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

always commit transaction before closing connection?

843859Jun 13 2010 — edited Nov 20 2014
Hi, I have the following code:
Connection c = null;
      try {
          ...
         //get a connection from pool
         ...
         c.setAutoCommit(false);
         ...
         if(condition) {
              return;
         }
         //do some update in the database     
         ...  
         c.commit();
      } catch(SQLException sqle) {
         //rollback transaction
         return;
      } finally {
         //close connection
      }
If a specific condition happens during the transaction, then I just close the connection and return without commiting. In the docs it says closing a connection releases all database and jdbc resources, but when getting a connection from a pool, closing it may just return it to the pool. Also, the docs say that commit releases any database locks the connection held. If the connection was holding any database locks and I close without commiting, does it mean that the locks are still held by the connection now in the pool? Should I always commit a transaction before closing the connection?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 13 2010
Added on Jun 13 2010
7 comments
2,289 views