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

Single-Phase Commit JTA Transaction Example

Employee.IDL

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

  exception SQLError {
    wstring message;
  };

  interface Employee {
    void setUpDSConnection (in wstring dsName) raises (SQLError);
    EmployeeInfo getEmployee (in wstring name) raises (SQLError);
    void updateEmployee (in EmployeeInfo name) 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 jdbcURL objectName " + 
			  "user password userTxnName dataSrcName");
      System.exit (1);
    }
    String sessiiopURL = args [0];
    String jdbcURL = args [1];
    String objectName = args [2];
    String user = args [3];
    String password = args [4];
    String utName = args [5];
    String dsName = args [6];

    // lookup usertransaction object in the namespace
    UserTransaction ut = lookupUserTransaction (user, password, 
						jdbcURL, utName);

    // lookup employee object in the namespace
    Employee employee = lookupObject (user, password, sessiiopURL, objectName);
    EmployeeInfo info;

    // for (int ii = 0; ii < 10; ii++) 
    // {
    // start a transaction
    ut.begin ();

    // set up the DS on the server
    employee.setUpDSConnection (dsName);

    // retrieve the info
    info = employee.getEmployee ("SCOTT");
    System.out.println ("Before Update: " + info.name +"  " + info.salary);

    // change the salary and update it
    System.out.println ("Increase by 10%");
    info.salary += (info.salary * 10) / 100;
    employee.updateEmployee (info);    

    // commit the changes
    ut.commit ();

    // NOTE: you can do this before the commit of the previous transaction
    // (without starting a txn) then it becomes part of the first 
    // global transaction.
    // start another transaction to retrieve the updated info
    ut.begin ();

    // Since, you started a new transaction, the DS needs to be
    // enlisted with the 'new' transaction. Hence, setup the DS on the server
    employee.setUpDSConnection (dsName);

    // try to retrieve the updated info
    info = employee.getEmployee ("SCOTT");
    System.out.println ("After Update: " + info.name +"  " + info.salary);

    // commit the seond transaction
    ut.commit ();
  }

  private static UserTransaction lookupUserTransaction (String user,
							String password,
							String jdbcURL,
							String utName)
  {
    UserTransaction ut = null;
    try {
      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(jdbc_accessURLContextFactory.CONNECTION_URL_PROP, jdbcURL);
      Context ic = new InitialContext (env);

      DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver ());
      
      ut = (UserTransaction)ic.lookup ("jdbc_access:/" + utName);
    } catch (NamingException e) {
      e.printStackTrace ();
    } catch (SQLException e) {
      e.printStackTrace ();
    }
    return ut;
  }

  private static Employee lookupObject (String user, String password,
					 String sessiiopURL, String objectName)
  {
    Employee emp = null;
    try {
      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);

      System.out.println ("Trying to lookup: " + sessiiopURL + objectName);
      emp = (Employee)ic.lookup (sessiiopURL + objectName);
    } catch (NamingException e) {
      e.printStackTrace ();
    }
    return emp;
  }
}

EmployeeServer.sqlj

package employeeServer;

import employee.*;

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

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.naming.NamingException;

public class EmployeeImpl 
       extends _EmployeeImplBase 
{
  Context ic = null;
  DataSource ds = null;
  Connection conn = null;
  
  private void setInSessionLookupContext ()
       throws NamingException
  {
    Hashtable env = new Hashtable ();
    env.put (Context.INITIAL_CONTEXT_FACTORY,
	     "oracle.aurora.namespace.InitialContextFactoryImpl");	
    ic = new InitialContext (env);
    // ic = new InitialContext ();
  }

  public void setUpDSConnection (String dsName) 
       throws SQLError 
  { 
    try { 
      if (ic == null) 
	setInSessionLookupContext ();

      // get a connection to the local DB
      ds = (DataSource)ic.lookup (dsName);

      // get a connectoin to the local DB
     conn = ds.getConnection ();
    } catch (NamingException e) {
      e.printStackTrace ();
      throw new SQLError ("setUpDSConnection failed:" + e.toString ());
    } catch (SQLException e) {
      e.printStackTrace ();
      throw new SQLError ("setUpDSConnection failed:" + e.toString ());
    }
  }

  public EmployeeInfo getEmployee (String name) 
       throws SQLError 
  {
    try {
      if (conn == null)
	throw new SQLError ("getEmployee: conn is null");

      int empno = 0;
      double salary = 0.0;
      #sql { select empno, sal into :empno, :salary from emp
	            where ename = :name };
      return new EmployeeInfo (name, empno, (float)salary);
    } catch (SQLException e) {
      throw new SQLError (e.getMessage ());
    }
  }

  public void updateEmployee (EmployeeInfo employee) 
       throws SQLError 
  {
    if (conn == null)
      throw new SQLError ("updateEmployee: conn is null");

    try {
      #sql { update emp set ename = :(employee.name), sal = :(employee.salary)
	            where empno = :(employee.number) };
    } catch (SQLException e) {
      throw new SQLError (e.getMessage ());
    }
  }
}


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