• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling commit() and rollback() in autocommit mode (K&B7, chapter 15, page 925)

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chapter 15, page 925
"Note that both commit() and rollback() are transaction methods, and if either of these methods is invoked when a Connection is not in a transaction (for example, when a Connection is in auto-commit mode), these methods will throw a SQLException."
=>
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
On a related note, issuing conn.setSavepoint() actually does seem to produce "java.sql.SQLException: could not set a Savepoint with auto-commit on", which is not mentioned in the book as much as I saw where discussing savepoints
This seems to be a heavily featured topic on the exam, so clarification would be essential

(This post originated in the K&B7 errata thread)
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 snippetWhen 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
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the study guide's point of view, it's reasonable (and even recommended) to explain what's mentioned in the javadoc for a method, because that's probably what you will be tested about on the actual exam Although not every driver seems to meet the contract defined by the JDBC interfaces, the javadoc of the commit() and rollback() methods of Connection clearly state an SQLException is thrown if the method is called on a Connection object in auto-commit mode. So I think there's no need to add this one to the errata overview.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic