package examples.jdbc.t3client;
import java.net.*;
import java.util.*;
import java.sql.*;
import weblogic.db.jdbc.*;
import weblogic.common.*;
/**
* The example connectioncache demonstrates the creation
* of persistent workspaces, and the reuse of JDBC connections stored
* in these workspaces.
*
* This example taks no arguments, and expects to connect to
* a WebLogic Server on the local host, listening on port 7001. The
* JDBC connection is to an Oracle instance named DEMO. A
* table named "emp" is queried.
*
*
To set up this example:
*
*- Set up your development shell as described in Setting up your environment.
*
- Change connection parameters to correspond to your database
*configuration. If you need more help, check the section on connecting
*to a database in the Developers Guide, Using jdbcKona/Oracle.
*
*
- Compile the example by executing the following command in your development shell:
*
*
*$ javac -d %CLIENT_CLASSES% connectioncache.java
*
*
*
- Start WebLogic Server in a separate shell.
*
*
*
- Run this example by executing the following command in your
*development shell:
*
*
*
*$ java examples.jdbc.t3client.connectioncache
*
*
*
*
* @author Copyright (c) 1996-1999 by BEA WebXpress, Inc. All Rights Reserved.
*/
public class connectioncache {
/**
* Gets a new connection if a cached one doesn't exist, or gets a
* cached connection if one is found in the T3Client's Workspace.
*/
static Connection getConnection(T3Client t3, String dbconnid) throws Exception {
Properties dbprops = new Properties();
dbprops.put("user", "scott");
dbprops.put("password", "tiger");
dbprops.put("server", "DEMO");
Properties t3props = new Properties();
t3props.put("weblogic.t3", t3);
t3props.put("weblogic.t3.dbprops", dbprops);
t3props.put("weblogic.t3.driverClassName", "weblogic.jdbc.oci.Driver");
t3props.put("weblogic.t3.driverURL", "jdbc:weblogic:oracle");
t3props.put("weblogic.t3.connectionID", dbconnid);
Class.forName("weblogic.jdbc.t3.Driver").newInstance();
// Make the connection and return the JDBC Connection object
return DriverManager.getConnection("jdbc:weblogic:t3", t3props);
}
/**
* Runs this example from the command line.
*/
public static void main(String[] argv) throws Exception {
// Create T3Client and connect; make the soft disconnect
// timeout indefinite so that the WebLogic Server will preserve
// the T3Client's Workspace, in which the cached connection
// will be stored
// modify the next line to match your host name and port number.
T3Client t3 = new T3Client("t3://localhost:7001");
t3.connect()
.setSoftDisconnectTimeoutMins(T3Client.DISCONNECT_TIMEOUT_NEVER);
// Get the workspace ID so you can create this client again
String wsid = t3.services.workspace().getID();
System.out.println("Logging into database and saving session as myconn");
Connection conn = getConnection(t3, "myconn");
// Disconnect the client. Since the Soft Disconnect timeout
// interval is NEVER, the WebLogic Server will preserve the client's
// Workspace and all the objects in it, including the cached
// JDBC Connection.
t3.disconnect();
t3 = null;
System.out.println("Reconnecting client " + wsid);
// Create another T3Client. This one will have access to the same
// workspace as the first since the workspace ID exists on the
// WebLogic Server.
t3 = new T3Client("t3://localhost:7001", wsid);
t3.connect();
// Reestablish the cached connection and do some arbitrary
// database work
for (int j = 0; j < 5; j++) {
System.out.println("Reestablishing database connection");
conn = getConnection(t3, "myconn");
System.out.println("Performing query");
QueryDataSet qds = new QueryDataSet(conn, "select * from emp");
qds.fetchRecords();
System.out.println("Record count = " + qds.size());
qds.close();
}
// Close the connection. This disposes of the cached connection on
// the WebLogic Server. Calling getConnection() with "myconn" again will
// create a new JDBC Connection by that name.
conn.close();
// Set the timeout internal to 0 to force an immediate disconnect
// on the server, and to force the WebLogic Server to clean up (destroy)
// the resources associated with this T3Client. Connecting again
// with "wsid" will create a new client workspace unrelated to
// this one.
t3.setSoftDisconnectTimeoutMins(0)
.disconnect();
}
}