BEA Logo BEA WebLogic Server Release 1.1

  Corporate Info  |  News  |  Solutions  |  Products  |  Partners  |  Services  |  Events  |  Download  |  How To Buy

Using the JTS Driver

 

Overview
Implementing with the JTS Driver

Overview

The Java Transaction Services or JTS driver is a server-side Java Database Connectivity (JDBC) driver that provides access to both connection pools and SQL transactions from applications running in WebLogic Server. Connections to a database are made from a connection pool and use a two-tier JDBC driver running in WebLogic Server to connect to the Database Management System (DBMS) on behalf of your application.

Once a transaction is begun, all of the database operations in a execute thread that get their connection from the same connection pool will share the same connection from that pool. These operations may be made through services such as Enterprise JavaBeans (EJB), or Java Messaging Service (JMS), or by directly sending SQL statements using standard JDBC calls. All of these operations will share the same connection and participate in the same transaction.When the transaction is committed or rolled back, the connection will be returned to the pool.

Although Java clients may not register the JTS driver themselves, they may participate in transactions via Remote Method Invocation (RMI). You can begin a transaction in a thread on a client and then have the client call a remote RMI object. The database operations executed by the remote object will become part of the transaction that was begun on the client. When the remote object is returned back to the calling client, you can then commit or roll back the transaction. The database operations executed by the remote objects must all use the same connection pool to be part of the same transaction.

Implementing with the JTS Driver

To use the JTS driver, you must have defined a database connection pool in WebLogic Server. For information on creating connection pools, see Creating and Using Connection Pools.

For this explanation we assume the connection pool is named "myConnectionPool." This explanation demonstrates creating and using a JTS transaction from a server-side application.

  1. Import the following classes:

    import javax.jts.UserTransaction;
    import java.sql.*;
    import javax.naming.*;
    import java.util.*;
    import weblogic.jndi.*;

  2. Establish the transaction by using the UserTransaction class. This class can be looked up in the Java Naming and Directory Service (JNDI). 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.put(Context.INITIAL_CONTEXT_FACTORY,
    "weblogic.jndi.WLInitialContextFactory");

    ctx = new InitialContext(env);

    UserTransaction tx = (UserTransaction)
    ctx.lookup("javax.jts.UserTransaction");

  3. Start a transaction on the current thread:

    tx.begin();

  4. Load the JTS driver

    Driver myDriver = (Driver)
    Class.forName("weblogic.jdbc.jts.Driver").newInstance();

  5. Get a connection from the connection pool.

    Properties props = new Properties(); 
    conn = myDriver.connect("jdbc:weblogic:jts:myConnectionPool", null);

  6. Execute your database operations. These operations may be made by any service that uses a database connection. These include EJB, JMS, or standard JDBC statements. If these operations use the JTS driver to access the same connection pool as the transaction begun in step number 3., they will participate in that transaction.

    If the additional database operations using the JTS driver use a different connection pool than the one specified in step 5., an exception will be thrown when you try to commit or rollback the transaction.

  7. Close all your statements and result sets.

  8. Close your connection objects. Note that closing the connections does not commit the transaction nor return the connection to the pool:

    conn.close();

  9. Execute any other database operations. If these operations are made by connecting to the same connection pool, the operations will use the same connection from the pool and become part of the same UserTransaction as all of the other operations in this thread.

  10. Complete the transaction by either committing the transaction or rolling it back. The JTS driver will commit all the transactions on all connection objects in the current thread and return the connection to the pool.

    tx.commit();

    // or:

    tx.rollback();