Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8618683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:10:11+00:00 2026-06-12T06:10:11+00:00

Got the db connection ( conn ) from the pool. Assume that autocommit is

  • 0

Got the db connection (conn) from the pool.

Assume that autocommit is TRUE on that connection.

Now conn.setautocommit(false) has set ;

then after few statement updates and finally conn.commit()/conn.rollback() has done.

Now do i need to do explicitly code setautocommit(true) to revert to the previous conn state?

OR commit()\rollback() will set setautocommit(true) inherently ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:10:12+00:00Added an answer on June 12, 2026 at 6:10 am

    That depends on where you got that connection from. If you created the connection yourself, there is no need to restore the state of auto commit.

    If you got it from a data source, you should restore the state to what it was because the data source might keep the connections in a pool and the next piece of code might not expect what you set.

    commit() doesn’t influence the value of auto commit. Enabling auto commit just makes sure that the JDBC driver calls commit() after each statement that you execute. You can still call commit() as often as you like, it just won’t have any effect (except that rollback() will not always do what you want).

    [EDIT] How auto commit is handled depends on your connection pool. dbcp has a config option to turn auto commit off before giving you a connection, c3p0 will roll back connections when you return then to the pool. Read the documentation for your connection pool how it works.

    If you don’t know which pool is used, the safe solution is to set auto commit to false whenever you get a connection and to roll back the connection if you get an exception. I suggest to write a wrapper:

    public <T> T withTransaction( TxCallback<T> closure ) throws Exception {
        Connection conn = getConnection();
        try {
            boolean autoCommit = conn.getAutoCommit();
            conn.setAutoCommit(false);
    
            T result = closure.call(conn); // Business code
    
            conn.commit();
            conn.setAutoCommit(autoCommit);
        } catch( Exception e ) {
            conn.rollback();
        } finally {
            conn.close();
        }
    }
    

    This code will correctly handle the connection for you and you don’t need to worry about it anymore in your business code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the scenario where a connection is got from a database pool(like Commons DBCP,
I've got a Runnable which gets a connection from a connection pool as below
This is how my connection is set: Connection conn = DriverManager.getConnection(url + dbName +
I've checked the connection string (I got it from the server explorer). I've checked
I have a database connection that I got like this: db = Mongo::Connection.new.db(app-development) but
I've got a ruby script that opens a connection to Apple's push server and
I've got a data access class that sets up three phpcassa connection pools on
I am connecting to an oracle database using a connection pool. I have set
I've got 3 connection strings in my app: <add name=DBConnectionString connectionString=/> <add name=ADConnectionString connectionString=
I got error No connection could be made because the target machine actively refused

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.