JDBC-ODBC Bridge Enhancements


Overview

The JDBC-ODBC Bridge allows apps written in Java to use the JDBC API with existing ODBC drivers. The bridge is defined in the class sun.jdbc.odbc.JdbcOdbcDriver.

The JDBC-ODBC Bridge should be considered a transitional solution. Sun Microsystems does not consider the JDBC-ODBC Bridge to be a supported product.

You can specify a character encoding scheme other than the client default. For example, to use Big5 for all character data:

Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);

java.util.Properties prop = new java.util.Properties();
prop.put("charSet", "Big5");
prop.put("user", username);
prop.put("password", password);

con = DriverManager.getConnection(url, prop);


DataSource Implementations

Implementations of javax.sql.DataSource and javax.sql.ConnectionPoolDataSource are now available. Both are in the sun.jdbc.odbc.ee package. The following code fragment illustrates how to set DataSource properties. The last two lines use JNDI API to bind the DataSource object ds to "jdbc/OdbcDB1". Assume that the DataSource object is targeting an ODBC DSN "dsn1" connecting to an Oracle Database.

 

// Establish the DataSource object instance

sun.jdbc.odbc.ee.DataSource ds = new sun.jdbc.odbc.ee.DataSource();


// Provide user credentials and database name

ds.setUser("scott");
ds.setPassword("tiger");
ds.setDatabaseName("dsn1");
ds.setCharSet("..."); 
ds.setLoginTimeout(100); 


// Establish initial context and bind to the datasource target

InitialContext ic = new InitialContext();
 ic.bind("jdbc/OdbcDB1",ds);

Properties such as port number and role name are not implemented in the JDBC-ODBC bridge DataSource implementations as these properties are not applicable to ODBC paradigm.

The following code fragment demonstrates retrieving the DataSource object by looking up the JNDI name "jdbc/OdbcDB1". With the DataSource object that is obtained, the code then activates tracing and creates two connections.

 
// Get the initial context of JNDI and lookup the datasource.
 InitialContext ic = new InitialContext();
 javax.sql.DataSource ds1 = (javax.sql.DataSource) ic.lookup("jdbc/OdbcDB1");
// Set the optional printwriter where the trace log is to be directed. ds1.setLogWriter(new PrintWriter(new FileOutputStream("/tmp/datasource.log"))); Connection con1 = ds1.getConnection(); Connection con2 = ds1.getConnection("system","manager");
The implementation of javax.sql.ConnectionPoolDataSource uses an underlying pool of JDBC-ODBC connections. A ConnectionPoolDataSource object is used to create PooledConnection objects, which are in turn used to get Connection objects. From the user's viewpoint, the Connection object is just like any other connection.

The following code fragment creates the ConnectionPoolDataSource object cpds and sets its properties. The final two lines use JNDI API to bind cpds to "jdbc/OdbcPool", which can later be supplied to the method InitialContext.lookup to retrieve cpds.

// Establish ConnectionPoolDataSource instance

sun.jdbc.odbc.ee.ConnectionPoolDataSource cpds =

 new sun.jdbc.odbc.ee.ConnectionPoolDataSource("jdbc/OdbcPool");

// Provide user credentials and database name
 cpds.setUser("scott");
 cpds.setPassword("tiger");
 cpds.setDatabaseName("dsn1");
 cpds.setCharSet("...") // optional property
 cpds.setLoginTimeout(100); // optional property

cpds.setMinPoolSize("10"); 
cpds.setInitialPoolSize("15");
cpds.setMaxPoolSize("20");
cpds.setMaxIdleTime("300");
cpds.setTimeoutFromPool("600");

// Maintenance interval of the pool. A maintenance thread will remove
// unwanted connections and cleanup the pool at the interval specified.
// This cannot be zero.
cpds.setMaintenanceInterval("900");

InitialContext ic = new InitialContext();
ic.bind("jdbc/OdbcPool",cpds);

In all cases in order get the ConnectionPoolDataSource to function as a pooled datasource, it is the responsibility of the application to configure the pool as described in the code example above. The default behavior of pooled datasource uses a zero minimum, initial and maximum pool sizes. Note that the JDBC-ODBC bridge implementation of ConnectionPoolDataSource does not yet include the pooling of Statement objects or the property propertyCycle.

The following code fragment shows how to use a ConnectionPoolDataSource object as simply a DataSource object. This is done by doing a JNDI lookup of "jdbc/OdbcPool" and casting it to a DataSource object instead of to a ConnectionPoolDataSource object.

InitialContext ic = new InitialContext();
 javax.sql.DataSource ds1 = (javax.sql.DataSource) ic.lookup("jdbc/OdbcPool");

// First getConnection will initializes the pool.
 Connection con1 = ds1.getConnection();
 Connection con2 = ds1.getConnection("system","manager");


 // An application need to close the connection explicitly. This will allow the pool to recycle the connection.
 con1.close();
 con2.close();

Using the implementation as a ConnectionPoolDataSource object is shown in the following line of code. Note that closing a PooledConnection object closes the actual physical connection, whereas closing a connection that was created from a PooledConnection object just returns it to the pool of connections.

 
InitialContext ic = new InitialContext();
javax.sql.ConnectionPoolDataSource cpds = (javax.sql.ConnectionPoolDataSource) ic.lookup("jdbc/OdbcPool");

PooledConnection pc1 = cpds.getPooledConnection();
Connection con1 = pc1.getConnection();
PooledConnection pc2 = cpds.getPooledConnection("system","manager");
Connection con2 = pc2.getConnection();

// Apps need to close connections explicitly to allow pool to recycle the connection.
con1.close();
con2.close();

Connection con3 = pc1.getConnection();
Connection con4 = pc2.getConnection();

// Close the physical connection!
pc1.close();
pc2.close();

A pool of connections can be shut down in two ways. If the method shutDown is given the argument false, only those connections that are not being used will be closed. If the argument true is supplied, all connections will be closed immediately, regardless of whether they are being used or not.

 // Hot shutdown
 ((sun.jdbc.odbc.ee.ConnectionPoolDataSource) cpds).shutDown(true);
or

 // Hot shutdown
 // Cold shutdown
 ((sun.jdbc.odbc.ee.ConnectionPoolDataSource) cpds).shutDown(false);