package examples.jdbc.t3client; import java.net.*; import java.util.*; import java.sql.*; import weblogic.db.jdbc.*; import weblogic.common.*; /** * This example illustrates the use of a JDBC connection from * a connection pool. The example here uses the connection "oraclePool" * that is included (commented out) in the weblogic.properties * file that is shipped with the distribution. *

*

To set up this example:

*
    *
  1. Set up your development shell as described in Setting up your environment. * *

    * *

  2. Set up an appropriate connection pool * in your weblogic.properties *file by uncommenting the lines that refer to the connectionPool.oraclePool. Make sure to uncomment *the ACLs for this connection pool as well. If you are not using an *Oracle database, adjust the code inside the try block in this example to a test appropriate for *your database. * *

    *

  3. Modify the example so that the correct URL for your WebLogic Server is specified. * *

    *

  4. Compile the example by executing the following command in your development shell: * *

    *$ javac -d %CLIENT_CLASSES% connectionpool.java * * *

    * *

    *

  5. Start WebLogic Server in a separate shell. * *

    *

  6. Run this example by executing the following command in your *development shell: * *

    * *$ java examples.jdbc.t3client.connectionpool * *

    * *

* * @author Copyright (c) 1996-1999 by BEA WebXpress, Inc. All Rights Reserved. */ public class connectionpool { /** * In this main, the T3Client is created and connected. The * code then illustrates the use of the connection pool "oraclePool" * (commented out in the weblogic.properties file) that is shipped * with the distribution. This example also illustrates the use of * the refresh() method for a connection from the pool. */ public static void main(String[] argv) throws Exception { // Create a T3Client and connect to the WebLogic Server // modify the next line to match your host name and port number. T3Client t3 = new T3Client("t3://localhost:7001"); t3.connect(); // This demonstrates accessing a connection in the pool "oraclePool" that // was created by an entry in the WebLogic Server's // weblogic.properties file. The default properties file shipped // with the distribution has the appropriate example for this // connection pool (commented out). Properties t3props = new Properties(); t3props.put("weblogic.t3", t3); // Use the connectionPoolID to identify the name of the connection // pool this client will use. This client should be associated // with a T3User when created, or "guest" should be listed as a // member of the pool. The sample "oraclePool" connection pool // illustrates a connection to an Oracle database. t3props.put("weblogic.t3.connectionPoolID", "oraclePool"); Class.forName("weblogic.jdbc.t3.Driver").newInstance(); for (int j = 0; j < 10; j++) { // Connect to the DBMS Connection conn = DriverManager.getConnection("jdbc:weblogic:t3", t3props); // Do some arbitrary database work QueryDataSet qds = new QueryDataSet(conn, "select * from emp"); qds.fetchRecords(); System.out.println("Record count = " + qds.size()); qds.close(); // WARNING: THE FOLLOWING CODE IS NOT FOR GENERAL APPLICATIONS. // THE REFRESH CALL ILLUSTRATED BELOW IS AN ADMINISTRATIVE // FUNCTION TO BE USED WHEN IT IS KNOWN THAT THE CONNECTIONS // IN A POOL HAVE BECOME DEFUNCT BECAUSE THE DBMS WENT DOWN, // AND FURTHER, IT IS KNOWN THAT THE DBMS IS BACK UP SO THAT // THE WEBLOGIC SERVER CAN SUCCESSFULLY RECONNECT. // There are additional options to manually refreshing the // connection pool(s). Check the documentation on connection // pools at: // // ../../../classdocs/API_jdbct3.html#T5h // // One alternate method for refreshing a pool is with the // utility class, weblogic.event.actions.ActionRefreshPool. // With this class you can set up an automatic timed event in // the WebLogic Server, which will periodically test a connection // from a pool. If it fails, this handler will attempt refresh // the pool. Note that until the DBMS is accepting new // connections, this will also fail, but will retry till // successful. // In situations where the database goes down and is back up, // and WebLogic is not restarted, the connection pool connections // will be invalid/stale. If this is a likely scenario for you, // the commented out code below shows how to determine if the // connection is bad and how to refresh it. /* // If a usually unfailable SQL statement fails, you can suspect // that the connection is bad. try { Statement stmt = conn.createStatement(); stmt.execute("select 1 from dual"); ResultSet rs = stmt.getResultSet(); rs.next(); rs.close(); stmt.close(); } catch (SQLException sqe) { // Sleep here till it is known that the DBMS is back. ((weblogic.jdbc.t3.Connection)conn).refresh(); } */ // Release the connection and return it to the connection pool conn.close(); } // Disconnect the client t3.disconnect(); } }