![]() |
![]() |
Using WebLogic JDBC/RMI and WebLogic Clustered JDBC
Setting up WebLogic Server to use the WebLogic JDBC/RMI driver
Setting up the client to use the WebLogic JDBC/RMI driver
Obtaining a client connection with a JNDI lookup
Obtaining a client connection with only the JDBC/RMI driver
Setting up WebLogic Clustered JDBC
Introduction
The WebLogic JDBC/RMI driver is a multi-tier, type-3, Java Data Base Connectivity (JDBC) driver that runs in WebLogic Server and can be used with any two-tier JDBC driver to provide database access. Additionally, when configured in a cluster of WebLogic Servers, the WebLogic JDBC/RMI driver can be used for clustered JDBC, allowing JDBC clients the benefits of load balancing and fail-over provided by WebLogic Clusters. This document describes how to set up your WebLogic Server(s) and JDBC clients to use the WebLogic JDBC/RMI driver and WebLogic Clustered JDBC.
WebLogic JDBC/RMI driver
Overview
The BEA WebLogic JDBC/RMI driver operates within WebLogic Server and provides connectivity from JDBC clients to a Database Management System (DBMS). This DBMS connection is made by means of the WebLogic JDBC/RMI driver, a DataSource object, and a connection pool operating in WebLogic Server.
The DataSource object defines the connection to the DBMS. The connection parameters are specified in the weblogic.properties by pointing to a connection pool that is also defined in the weblogic.properties file. This connection pool is in turn configured for two-tier JDBC access to a DBMS.
JDBC/RMI clients make their connection to the DBMS by looking up this DataSource object. This look up is accomplished by using a Java Naming and Directory Service (JNDI) lookup, or by directly calling the WebLogic JDBC/RMI driver which performs the JNDI lookup on behalf of the client.
BEA implements the DataSource interface as a clusterable RMI object. This implementation allows JDBC clients access to the fail-over and load balancing features of WebLogic Clusters. For more information on Clustered JDBC, see WebLogic Clustered JDBC.
The WebLogic JDBC/RMI driver is similar in functionality to the WebLogic JDBC/T3 driver or the WebLogic pool driver (both included with WebLogic Server), but uses the Java standard Remote Method Invocation (RMI) to connect to WebLogic Server, rather than the proprietary T3 protocol. Since the details of the RMI implementation are taken care of automatically by the driver, a knowledge of RMI is not required to use the WebLogic JDBC/RMI driver. Note that BLOB/CLOB's are not serializable and therefore not supported with the JDBC/RMI Driver used with WebLogic 5.1.
Setting up WebLogic Server to use the WebLogic JDBC/RMI driver
To set up WebLogic Server to use the WebLogic JDBC/RMI driver:
For more information on setting up a connection pool to connect to your own DBMS, see Using connection pools in the WebLogic Server documentation.
Here is an example from the weblogic.properties file of a connection pool called "eng" that is set up to access an Oracle DBMS:
weblogic.jdbc.connectionPool.eng=\
url=jdbc:weblogic:oracle,\
driver=weblogic.jdbc.oci.Driver,\
loginDelaySecs=1,\
initialCapacity=4,\
maxCapacity=10,\
capacityIncrement=2,\
allowShrinking=true,\
shrinkPeriodMins=15,\
refreshTestMinutes=10,\
testTable=dual,\
props=user=SCOTT;password=tiger;server=DEMO
weblogic.allow.reserve.weblogic.jdbc.connectionPool.eng=\
guest,joe,jill
weblogic.allow.reset.weblogic.jdbc.connectionPool.eng=\
joe,jill
weblogic.allow.shrink.weblogic.jdbc.connectionPool.eng=\
joe,jill
To define a JTS-enabled DataSource named "myJtsDataSource" for the connection pool "mypool" you would make the following entry in the weblogic.properties file:
weblogic.jdbc.TXDataSource.myJtsDataSource=mypool
To define a DataSource named "myNonJtsDataSource" for the connection pool "mypool" that is not JTS-enabled, you would make the following entry in the weblogic.properties file:
weblogic.jdbc.DataSource.myNonJtsDataSource=mypool
Note that you may define multiple DataSources to use a single connection pool. A practical application of this would be to define both a JTS-enabled and non-JTS-enabled DataSource that share the same database.
Setting up the client to use the WebLogic JDBC/RMI driver
To make a client connection, first import the following packages:
javax.sql.DataSource
java.sql.*
java.util.*
javax.naming.*
A WebLogic JDBC/RMI client obtains its connection to a DBMS from the DataSource object that was defined in the weblogic.properties file. There are two ways the client can obtain a DataSource object:
Obtaining a client connection with a JNDI lookup
To access the WebLogic JDBC/RMI driver using JNDI, obtain a Context from the JNDI tree by looking up the name of your DataSource object. For example, to access a DataSource called "myDataSource" that is defined in the weblogic.properties file of a WebLogic Server:
Context ctx = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,
"t3://hostname:port");
try {
ctx = new InitialContext(ht);
javax.sql.DataSource ds
= (javax.sql.DataSource) ctx.lookup ("myDataSource");
java.sql.Connection conn = ds.getConnection();
// You can now use the conn object to create
// a Statement object to execute
// SQL statements and process result sets:
Statement stmt = conn.createStatement();
stmt.execute("select * from someTable");
ResultSet rs = stmt.getResultSet();
// Do not forget to close the statement and connection objects
// when you are finished:
stmt.close();
conn.close();
}
catch (NamingException e) {
// a failure occurred
}
finally {
try {ctx.close();}
catch (Exception e) {
// a failure occurred
}
}
(Where hostname is the name of the machine running your WebLogic Server and port is the port number where that machine is listening for connection requests.)
In this example a Hashtable object is used to pass the parameters required for the JNDI lookup. There are other ways to perform a JNDI look up. These are discussed under Using WebLogic JNDI from a client.
Notice that the JNDI lookup is wrapped in a try/catch block in order to catch a failed look up and also that the context is closed in a finally block.
Obtaining a client connection with only the JDBC/RMI driver
You can also access the WebLogic JDBC/RMI driver using the Driver.connect() method, in which case the JDBC/RMI driver performs the JNDI lookup. To access the WebLogic JDBC/RMI driver, pass the parameters defining the URL of your WebLogic Server and the name of the DataSource object to the Driver.connect() method. For example, to access a DataSource called "myDataSource" that is defined in the weblogic.properties file of a WebLogic Server:
java.sql.Driver myDriver = (java.sql.Driver)
Class.forName("weblogic.jdbc.rmi.Driver").newInstance();
String url = "jdbc:weblogic:rmi";
java.util.Properties props = new java.util.Properties();
props.put("weblogic.server.url", "t3://hostname:port");
props.put("weblogic.jdbc.datasource", "myDataSource");
java.sql.Connection conn = myDriver.connect(url, props);
(Where hostname is the name of the machine running your WebLogic Server and port is the port number where that machine is listening for connection requests.)
You can also define the following properties which will be used to set the JNDI user information:
Once you have obtained a connection using one of the two procedures described above, you can use it to make standard JDBC calls such as execute and update, and to access result sets. For example:
Statement stmt = conn.createStatement();
stmt.execute("select * from sometable");
ResultSet rs = stmt.getResultSet();
Additional information
For more information on using BEA WebLogic JDBC drivers see:
For more information on using BEA WebLogic Server:
WebLogic Clustered JDBC Overview
Clustered JDBC allows JDBC clients to access the fail-over and load balancing features of clustered WebLogic Servers. To use Clustered JDBC, you use the WebLogic JDBC/RMI driver to connect to the DBMS, as described earlier in this document. The only change you need to make to use Clustered JDBC is to define identical DataSource objects in each of the per-server weblogic.properties files for each WebLogic Server in the cluster.
When a client looks up a DataSource object whose name is replicated in the per-server weblogic.properties files of a WebLogic cluster, the load balancing and fail-over logic is used to determine which WebLogic Server in the cluster will handle the connection. Once a connection is obtained, the connection will be pinned to the server in the cluster that first made the connection, maintaining the DBMS context.
Setting up WebLogic Clustered JDBC
To set up WebLogic Clustered JDBC:
For more information on WebLogic Clusters, see Setting up a WebLogic Cluster, in the WebLogic Server Administrators guides.
|
Copyright © 2000 BEA Systems, Inc. All rights reserved.
|