J Deckarm wrote:For me neither of these seem to produce an SQLException in autocommit mode. Perhaps this is also vendor dependent? I use Oracle JDBC version 11.x, autocommit mode is true by default, but issuing explicit conn.commit() or conn.rollback() in this state does not seem to cause any exceptions
I had a go as well and used MySql (mysql-connector-java-5.1.36).
I created a little code snippet
When I ran this little program, I got an
SQLException as expected:
SQLException: Can't call rollback when autocommit=true
SQLState: 08003
VendorError: 0
And when changing
rollback to
commit on
line1, I get an
SQLException as well:
SQLException: Can't call commit when autocommit=true
SQLState: null
VendorError: 0
Here's a snippet of the javadoc of the
Connection interface for both the
commit() and
rollback() methods:
Java 7 API Specification, Connection wrote:void commit() throws SQLException
Throws:
SQLException - if a database access error occurs, this method is called while participating in a distributed transaction, if this method is called on a closed conection or this Connection object is in auto-commit mode
Java 7 API Specification, Connection wrote:void rollback() throws SQLException
Throws:
SQLException - if a database access error occurs, this method is called while participating in a distributed transaction, this method is called on a closed connection or this Connection object is in auto-commit mode
So I have to say I'm a little bit surprised Oracle doesn't fulfill the contract of the
Connection interface and doesn't throw an exception. After some browsing on the internet, I discovered
the CONNECTION_PROPERTY_AUTO_COMMIT_SPEC_COMPLIANT property. Based on this information,
you should upgrade to 12.1 to get JDBC specification 4.1 compliant behavior.
Hope it helps!
Kind regards,
Roel