4-6 Oracle Fusion Middleware Programming JDBC for Oracle WebLogic Server
can then commit or roll back the transaction. The database operations executed by the remote objects must all use the same data source to be part of the same transaction.
For the JTS driver and your application to participate in a global transaction, the application must call conn = myDriver.connectjdbc:weblogic:jts,
props; within a global transaction. After the transaction completes gets committed
or rolled back, WebLogic Server puts the connection back in the data source. If you want to use a connection for another global transaction, the application must call conn
= myDriver.connectjdbc:weblogic:jts, props; again within a new
global transaction.
4.2.1 Sample Client Code for Using the JTS Driver
To use the JTS driver, you must first use the Administration Console to create a data source in WebLogic Server.
This explanation demonstrates creating and using a JTS transaction from a server-side application and uses a data source named myDataSource.
1.
Import the following classes: import javax.transaction.UserTransaction;
import java.sql.; import javax.naming.;
import java.util.; import weblogic.jndi.;
2.
Establish the transaction by using the UserTransaction class. You can look up this class on the JNDI tree. The UserTransaction class controls the transaction
on the current execute thread. Note that this class does not represent the transaction itself. The actual context for the transaction is associated with the
current execute thread.
Context ctx = null; Hashtable env = new Hashtable;
env.putContext.INITIAL_CONTEXT_FACTORY, weblogic.jndi.WLInitialContextFactory;
Parameters for the WebLogic Server. Substitute the correct hostname, port number
user name, and password for your environment: env.putContext.PROVIDER_URL, t3:localhost:7001;
env.putContext.SECURITY_PRINCIPAL, Fred; env.putContext.SECURITY_CREDENTIALS, secret;
ctx = new InitialContextenv; UserTransaction tx = UserTransaction
ctx.lookupjavax.transaction.UserTransaction;
3.
Start a transaction on the current thread: Start the global transaction before getting a connection
tx.begin;
4.
Load the JTS driver: Driver myDriver = Driver
Class.forNameweblogic.jdbc.jts.Driver.newInstance;
Using WebLogic Wrapper Drivers 4-7
5.
Get a connection from the data source: Properties props = new Properties;
props.putconnectionPoolID, myDataSource; conn = myDriver.connectjdbc:weblogic:jts, props;
6.
Execute your database operations. These operations may be made by any service that uses a database connection, including EJB, JMS, and standard JDBC
statements. These operations must use the JTS driver to access the same data source as the transaction begun in step 3 in order to participate in that transaction.
If the additional database operations using the JTS driver use a different data source than the one specified in step 5, an exception will be thrown when you try to
commit or roll back the transaction.
7.
Close your connection objects. Note that closing the connections does not commit the transaction nor return the connection to the pool:
conn.close;
8.
Complete the transaction by either committing the transaction or rolling it back. In the case of a commit, the JTS driver commits all the transactions on all connection
objects in the current thread and returns the connection to the pool.
tx.commit; or:
tx.rollback;
4.3 Using the WebLogic Pool Driver Deprecated