50. What is the purpose of setAutoCommit()

Autocommit behavior    The JDBC specification requires that, by default, a COMMIT is performed after each data modification statement. Currently, the server-side JDBC behavior is to commit. You can control this behavior using a statement such as the following:

  • conn.setAutoCommit( false ) ;

    where conn is the current connection object.

  • Connection defaults    From server-side JDBC, only the first call to getConnection( “jdbc:default:connection” ) creates a new connection with the default values. Subsequent calls return a wrapper of the current connection with all connection properties unchanged. If you set AutoCommit to OFF in your initial connection, any subsequent getConnection calls within the same Java code return a connection with AutoCommit set to OFF.

    You may wish to ensure that closing a connection resets the connection properties to their default values, so that subsequent connections are obtained with standard JDBC values. The following type of code achieves this:

    Connection conn = DriverManager.getConnection("");
    boolean oldAutoCommit = conn.getAutoCommit();
    try {
         // do code here
    }
    finally {
        conn.setAutoCommit( oldAutoCommit );
    }
  • Advertisements

    One Response

    1. […]  Add comment September 9th, 2006 […]

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: