![]() |
![]() |
Using a DataSource object to get a JDBC connection
Setting up WebLogic Server to use a DataSource
Obtaining a client connection using DataSource
Introduction
A DataSource object provides a new way for JDBC clients to obtain a DBMS connection. To create a DataSource object you define it with an entry in the weblogic.properties file. This DataSource entry then points to a connection pool that is also defined in the weblogic.properties file.
DataSource objects can be defined with or without Java Transaction Services (JTS) enabled. Creating a DataSource object with JTS enabled causes the connection to behave like a JTS connection, with support for transactions.
Import statements
To use the DataSource objects, import the following classes in your client code:
import java.sql.*;
import java.util.*;
import javax.naming.*;
Setting up WebLogic Server to use a DataSource
To define a JTS-enabled DataSource named "myJtsDataSource" for the connection pool "myConnectionPool", add the following line to the weblogic.properties file:
weblogic.jdbc.TXDataSource.myJtsDataSource=myConnectionPool
To define a DataSource named "myNonJtsDataSource" for the connection pool "myConnectionPool" that is not JTS-enabled, add the following line to the weblogic.properties file:
weblogic.jdbc.DataSource.myNonJtsDataSource=myConnectionPool
You can define multiple DataSources that use a single connection pool. This allows you to define both JTS-enabled and non-JTS-enabled DataSource objects that share the same database.
weblogic.jdbc.connectionPool.myConnectionPool=\
url=jdbc20:weblogic:oracle,\
driver=weblogic.jdbc20.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
(The example above shows a connection pool to an Oracle database, using WebLogic jDriver for Oracle. Substitute the URL and class name--shown in italics--of the JDBC driver you will be using to connect to your DBMS)
Obtaining a client connection using DataSource
To obtain a connection from a JDBC client, use a Java Naming and Directory Interface (JDNI) look up to locate the DataSource object, as shown in this code fragment:
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 ("myJtsDataSource");
java.sql.Connection conn = ds.getConnection();
// You can now use the conn object to create
// Statements and retrieve result sets:
Statement stmt = conn.createStatement();
stmt.execute("select * from someTable");
ResultSet rs = stmt.getResultSet();
// 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
}
}
(Substitute the correct hostname and port number for your WebLogic Server.
Note: The code above use one of several available procedures for obtaining a JNDI context. For more information on JNDI, see Using WebLogic JNDI.
Code examples
|
Copyright © 2000 BEA Systems, Inc. All rights reserved.
|