Establish Connection From a Server-side JDBC Class

SQL statements in JDBC are built using the createStatement method of a Connection object. Even classes running inside the server need to establish a connection to create a Connection object.

Establishing a connection from a server-side JDBC class is more straightforward than establishing an external connection. Because a user already connected executes the server-side class, the class simply uses the current connection.

Notes on JDBC Connections

  • 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 connection properties to their default values, so 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 );
    }

    This discussion applies not only to AutoCommit, but also to other connection properties such as TransactionIsolation and is ReadOnly.

Related concepts
Connect From a JDBC Client Application Using jConnect
Sybase jConnect JDBC Driver
Related tasks
Running the External Connection Example