Oracle8i CORBA Developer's Guide and Reference
Release 3 (8.1.7)

Part Number A83722-01

Library

Solution Area

Contents

Index

Go to previous page Go to beginning of chapter Go to next page

Two-Phase Commit JTA Transaction Example

Employee.IDL

module employee {
  struct EmployeeInfo {
    wstring name;
    long number;
    double salary; 
  };

  exception SQLError {
    wstring message;
  };

  interface Employee {
    void initialize (in wstring user, in wstring password, 
		     in wstring serviceURL, in wstring objectName, 
		     in wstring utName, in wstring localDSName,
		     in wstring remoteDSName) raises (SQLError);

    EmployeeInfo getEmployee (in wstring empName) raises (SQLError);
    void updateEmployee (in EmployeeInfo empInfo) raises (SQLError);

    EmployeeInfo getRemoteEmpInfo (in wstring name) raises (SQLError);
    void updateRemoteEmployee (in EmployeeInfo empInfo) raises (SQLError);
  };
};

Client.java

import employee.*;

import java.sql.DriverManager;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;

import java.sql.SQLException;
import javax.naming.NamingException;

import oracle.aurora.jndi.jdbc_access.jdbc_accessURLContextFactory;
import oracle.aurora.jndi.sess_iiop.ServiceCtx;

public class Client
{
  public static void main (String[] args) throws Exception 
  {
    if (args.length != 7)
    {
      System.out.println ("usage: Client sessiiopURL objectName user password"
			  + " userTxnName localDataSrcName remoteDataSrcName");
      System.exit (1);
    }
    String sessiiopURL = args [0];
    String objectName = args [1];
    String user = args [2];
    String password = args [3];
    String utName = args [4];
    String localDSName = args [5];
    String remoteDSName = args [6];

    Hashtable env = new Hashtable ();
    env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
    env.put (Context.SECURITY_PRINCIPAL, user);
    env.put (Context.SECURITY_CREDENTIALS, password);
    env.put (Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN);
    Context ic = new InitialContext (env);

    Employee employee = null;
    EmployeeInfo info;

    try {
      employee = (Employee)ic.lookup (sessiiopURL + objectName);
      employee.initialize (user, password, sessiiopURL, objectName, utName,
			   localDSName, remoteDSName);

      info = employee.getEmployee ("SCOTT");
      System.out.println (info.name + " "  + " " + info.salary);
      System.out.print ("Increase by 10% to ");
      info.salary += (info.salary * 10) / 100;
      System.out.println (info.salary);
      employee.updateEmployee (info);
    } catch (SQLError e) {
      System.out.println ("  Got SQLError: " + e.toString ());
    }
  }
}

Server

package employeeServer;

import employee.*;

import java.sql.Connection;
import java.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Hashtable;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
import javax.naming.NamingException;

import oracle.aurora.jndi.sess_iiop.ServiceCtx;
import oracle.aurora.transaction.xa.OracleJTADataSource;

public class EmployeeImpl extends _EmployeeImplBase 
{
  Context inSessionLookupctx = null;
  UserTransaction ut = null;
  DataSource localDS = null;
  DataSource remoteDS = null;
  Connection localConn = null;
  Connection remoteConn = null;
  String utName = null;
  String localDSName = null;
  String remoteDSName = null;
  String user = null;
  String pwd = null;
  String serviceURL = null;
  String objectName = null;
  EmployeeInfo localEmpInfo = null;

  private void setInSessionLookupContext () 
       throws NamingException
  {
    // NOTE: here we need to set env as 2-phase coord needs
    //       user/pwd to be set (branches is optional)
    Hashtable env = new Hashtable ();
    env.put (Context.SECURITY_PRINCIPAL, user);
    env.put (Context.SECURITY_CREDENTIALS, pwd);
    inSessionLookupctx = new InitialContext (env);
  }

  public void initialize (String user, String password, String serviceURL,
			  String objectName, String utName, String localDSName,
			  String remoteDSName)
       throws SQLError 
  { 
    try {
      // set the local variables
      this.user = user;
      this.pwd = password;
      this.objectName = objectName;
      this.utName = utName;
      this.localDSName = localDSName;
      this.remoteDSName = remoteDSName;
      this.serviceURL = serviceURL;

      // se up a ctx to lookup the local/in-session objects
      if (inSessionLookupctx == null)
	setInSessionLookupContext ();

      // lookup the usertransaction
      ut = (UserTransaction)inSessionLookupctx.lookup (utName);

      // get a connection to the local DB
      localDS = (OracleJTADataSource)inSessionLookupctx.lookup (localDSName);

      // get a connection to the local DB
      remoteDS = (OracleJTADataSource)inSessionLookupctx.lookup (remoteDSName);
    } catch (NamingException e) {
      e.printStackTrace ();
      throw new SQLError ("setUpDSConnection failed:" + e.toString ());
    }
  }

  private void getConnections ()
       throws SQLException
  {
    if (localDS == null)
      throw new SQLException ("local DataSource is NOT set correctly");
    if (remoteDS == null)
      throw new SQLException ("remote DataSource is NOT set correctly");
    if (user == null || pwd == null)
      throw new SQLException ("user/pwd is NOT set correctly");

    localDS.setURL ("jdbc:oracle:kprb:");
    localConn = localDS.getConnection ();
    System.out.println ("remoteDS.getURL: " + remoteDS.getURL ());
    remoteConn = remoteDS.getConnection (user, pwd);
  }

  private void startTrans () 
       throws SQLError 
  {
    try {
      if (ut == null)
	throw new SQLError ("startTrans: userTransaction is null");

      ut.begin ();
    } catch (Exception e) {
      throw new SQLError ("startTrans failed:" + e.toString ());
    }
  }

  private void commitTrans ()
       throws SQLError
  {
    try {
      ut.commit ();
    } catch (Exception e) {
      throw new SQLError ("commitTrans failed:" + e.toString ());
    }
  }

  public EmployeeInfo getLocalEmpInfo (String name)
       throws SQLError 
  {
    try {
      if (localConn== null)
	throw new SQLError ("getLocalEmpInfo: localConn is null");
      int empno = 0;
      double salary = 0.0;
      #sql { select empno, sal into :empno, :salary from emp
	            where ename = :name };
      System.out.println ("  Local (" + name + ", " + salary + ")");
      return new EmployeeInfo (name, empno, (float)salary);
    } catch (SQLException e) {
      e.printStackTrace ();
      throw new SQLError ("getRemoteEmpInfo SQLException: " + e.toString ());
    }
  }

  public EmployeeInfo getRemoteEmpInfo (String name)
       throws SQLError 
  {
    try {
      if (remoteConn== null)
	throw new SQLError ("getRemoteEmpInfo: remoteConn is null");
      int empno = 0;
      double salary = 0.0;

      PreparedStatement ps = remoteConn.prepareStatement
	("select empno, sal from emp where ename = ?");

      ps.setString (1, name);
      ResultSet rs = ps.executeQuery ();

      while (rs.next ())
      {
	empno = rs.getInt (1);
	salary = rs.getDouble (2);
      }
      System.out.println ("  Remote (" + name + ", " + salary + ")");
      return new EmployeeInfo (name, empno, (float)salary);
    } catch (SQLException e) {
      e.printStackTrace ();
      throw new SQLError ("getRemoteEmpInfo SQLException: " + e.toString ());
    }
  }

  public EmployeeInfo getEmployee (String name)
       throws SQLError
  {
    System.out.println ("getEmployee: begin");

    try {
      this.startTrans ();
      // get a connection to the local and remote DB
      this.getConnections ();
    } catch (SQLException e) {
      e.printStackTrace ();
      throw new SQLError ("getEmployee SQLError: " + e.toString ());
    }

    // get info for localEmployee = smith
    localEmpInfo = this.getLocalEmpInfo ("SMITH");

    // get info for the remote employee
    EmployeeInfo info = this.getRemoteEmpInfo (name);
    System.out.println ("  Remote (" + info.name + ", " + info.salary + ")");

    this.commitTrans ();
    System.out.println ("getEmployee: end");
    return info;
  }

  public void updateEmployee (EmployeeInfo empInfo) throws SQLError
  {
    System.out.println ("updateEmployee: begin");
    this.startTrans ();

    try {
      this.getConnections ();

      System.out.println ("  Before updating: ");
      this.getLocalEmpInfo ("SMITH");
      this.getRemoteEmpInfo ("SCOTT");

      localEmpInfo.salary -= 0.1 * localEmpInfo.salary;
      this.updateLocalEmployee (localEmpInfo);

      System.out.println ("  calling to update " + empInfo.name + " salary");
      updateRemoteEmployee (empInfo);

      System.out.println ("updateEmployee: After updating: ");
      this.getLocalEmpInfo ("SMITH");
      this.getRemoteEmpInfo ("SCOTT");
    } catch (SQLException e) {
      e.printStackTrace ();
      throw new SQLError ("updateEmployee SQLError: " + e.toString ());
    }

    this.commitTrans ();
    System.out.println ("updateEmployee: end");
  }

  public void updateLocalEmployee (EmployeeInfo empInfo) 
       throws SQLError
  {
    System.out.println ("updateLocalEmployee: begin");
    try {
      this.getLocalEmpInfo ("SMITH");

      #sql { update emp set ename = :(empInfo.name), sal = :(empInfo.salary)
                    where empno = :(empInfo.number) };

      System.out.println ("  after updating " + empInfo.name + " salary");
      this.getLocalEmpInfo (empInfo.name);
    } catch (SQLException e) {
      e.printStackTrace ();
      throw new SQLError (e.toString ()); 
    }

    System.out.println ("updateLocalEmployee: end");
  }

  public void updateRemoteEmployee (EmployeeInfo empInfo) 
       throws SQLError
  {
    System.out.println ("updateRemoteEmployee: begin");

    try {
      PreparedStatement ps = remoteConn.prepareStatement 
	("update emp set ename = ?, sal = ? where empno = ?");
      ps.setString (1, empInfo.name);
      ps.setDouble (2, empInfo.salary);
      ps.setInt (3, empInfo.number);
      ps.executeUpdate ();

      System.out.println ("  after updating " + empInfo.name + " salary");
      this.getRemoteEmpInfo (empInfo.name);
    } catch (SQLException e) {
      e.printStackTrace ();
      throw new SQLError (e.toString ()); 
    }

    System.out.println ("updateRemoteEmployee: end");
  }
}


Go to previous page
Go to beginning of chapter
Go to next page
Oracle
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.

Library

Solution Area

Contents

Index