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 ?
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 callscommit()after each statement that you execute. You can still callcommit()as often as you like, it just won’t have any effect (except thatrollback()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
falsewhenever you get a connection and to roll back the connection if you get an exception. I suggest to write a wrapper:This code will correctly handle the connection for you and you don’t need to worry about it anymore in your business code.