Oracle8i Enterprise JavaBeans and CORBA Developer's Guide Release 8.1.5 A64683-01 |
|
employee.ejb
SessionBean employeeServer.EmployeeBean { BeanHomeName = "test/myEmployee"; RemoteInterfaceClassName = employee.Employee; HomeInterfaceClassName = employee.EmployeeHome; AllowedIdentities = { PUBLIC }; RunAsMode = CLIENT_IDENTITY; TransactionAttribute = TX_SUPPORTS; }
import employee.Employee; import employee.EmployeeHome; import employee.EmployeeInfo; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.jts.client.AuroraTransactionService; import oracle.aurora.jts.util.TS; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; public class Client { public static void main (String[] args) throws Exception { if (args.length != 4) { System.out.println ("usage: Client serviceURL objectName user password"); System.exit (1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; // create InitialContext 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 ("Initial Context set up"); // System.out.println ("begin ATS.init (" + ic + " " + serviceURL + ")"); // initialize the transaction service AuroraTransactionService.initialize (ic, serviceURL); System.out.println ("begin ic.lookup (" + serviceURL + objectName + ")"); // get handle to the employee object EmployeeHome employee_home = (EmployeeHome)ic.lookup (serviceURL + objectName); System.out.println ("begin employee_home.create ()"); Employee employee = employee_home.create (); // System.out.println ("begin TS.getTS ().getCurrent ().begin ()"); // get Control to the transaction TS.getTS ().getCurrent ().begin (); EmployeeInfo info = employee.getEmployee ("SCOTT"); // System.out.println ("Beginning salary = " + info.getSalary ()); System.out.println ("Beginning salary = " + info.salary); // do work on the info-object info.salary += (info.salary * 10) / 100; // info.giveRaise (10); // call update on the server-side employee.updateEmployee (info); System.out.println ("End salary = " + info.salary); TS.getTS ().getCurrent ().commit (true); } }
package employee; import java.rmi.*; public class EmployeeInfo implements java.io.Serializable { public String name = null; public int number = 0; public double salary = 0; } /* public EmployeeInfo () { } public EmployeeInfo (String name, int number, double salary) { this.name = name; this.number = number; this.salary = salary; } public String getName () { return name; } public int getEmpNumber () { return number; } public double getSalary () { return salary; } public void giveRaise (int percent) { salary += salary * percent/100; } } */
package employee; import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.sql.*; public interface Employee extends EJBObject { public EmployeeInfo getEmployee (String name) throws RemoteException; public void updateEmployee (EmployeeInfo employee) throws RemoteException; }
package employee; import javax.ejb.EJBHome; import javax.ejb.CreateException; import java.rmi.RemoteException; public interface EmployeeHome extends EJBHome { public Employee create () throws RemoteException, CreateException; }
package employeeServer; import employee.*; import javax.ejb.SessionBean; import javax.ejb.CreateException; import javax.ejb.SessionContext; import java.rmi.RemoteException; import java.sql.*; public class EmployeeBean implements SessionBean { // Methods of the Employee interface public EmployeeInfo getEmployee (String name) throws RemoteException { try { int empno = 0; double salary = 0.0; #sql { select empno, sal into :empno, :salary from emp where ename = :name }; EmployeeInfo info = new EmployeeInfo (); info.name = name; info.salary = salary; info.number = empno; return info; } catch (SQLException e) { // throw new SQLError (e.getMessage ()); } return null; } public void updateEmployee (EmployeeInfo employee) throws RemoteException { try { #sql { update emp set ename = :(employee.name), sal = :(employee.salary) where empno = :(employee.number) }; } catch (SQLException e) { // throw new SQLError (e.getMessage ()); } return; } // Methods of the SessionBean public void ejbCreate () throws RemoteException, CreateException {} public void ejbRemove() {} public void setSessionContext (SessionContext ctx) {} public void ejbActivate () {} public void ejbPassivate () {} }
SessionBean employeeServer.EmployeeBean { BeanHomeName = "test/myEmployee"; RemoteInterfaceClassName = employee.Employee; HomeInterfaceClassName = employee.EmployeeHome; AllowedIdentities = { PUBLIC }; RunAsMode = CLIENT_IDENTITY; TransactionAttribute = TX_SUPPORTS; }
import employee.*; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; public class Client { public static void main (String[] args) throws Exception { if (args.length != 5) { System.out.println ("usage: Client serviceURL objectName user password sessionsCount"); System.exit (1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; int sessionCount = Integer.parseInt (args[4]); // create InitialContext // Note: authentication is done per session in ClientThread Hashtable env = new Hashtable (); env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); Context ic = new InitialContext (env); // invoke different sessions using ClientThread for (int i = 0; i < sessionCount; i++) { String sessionName = new String (":session" + i); ClientThread ct = new ClientThread (ic, serviceURL, objectName, sessionName, user, password); System.out.println ("Starting ClientThread (" + sessionName + ")"); ct.start (); } } }
import employee.*; import oracle.aurora.jts.client.AuroraTransactionService; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.jndi.sess_iiop.SessionCtx; import oracle.aurora.AuroraServices.LoginServer; import oracle.aurora.client.Login; import oracle.aurora.jts.util.TS; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; public class ClientThread extends Thread { private Context ic = null; private String serviceURL = null; private String objectName = null; private String sessionName = null; private SessionCtx session = null; public ClientThread () {} public ClientThread (Context ic, String serviceURL, String objectName, String sessionName, String user, String password) { try { this.ic = ic; ServiceCtx service = (ServiceCtx)ic.lookup (serviceURL); this.session = (SessionCtx)service.createSubcontext (sessionName); System.out.println ("activating the " + sessionName + " in " + serviceURL); LoginServer login_server = (LoginServer)session.activate ("etc/login"); Login login = new Login (login_server); login.authenticate (user, password, null); this.serviceURL = serviceURL; this.sessionName = sessionName; this.objectName = objectName; } catch (Exception e) { e.printStackTrace (); } } public void run () { try { this.yield (); // Get handle to the TX-Factory AuroraTransactionService.initialize (ic, serviceURL + "/" + sessionName); // create an instance of an employee object in the session EmployeeHome employee_home = (EmployeeHome)ic.lookup (serviceURL + "/" + sessionName + objectName); Employee employee = employee_home.create (); System.out.println ("employee_home.create () DONE in " + sessionName); EmployeeInfo info = null; // start the transaction TS.getTS ().getCurrent ().begin (); // get the info about an employee // Note: lock is set on the row using 'for update' clause while select operation info = employee.getEmployeeForUpdate ("SCOTT"); System.out.println ("Beginning salary = " + info.salary + " in " + sessionName); // arbitrarily change the value of the salary, e.g. depending on sessionName if (sessionName.endsWith ("0")) { System.out.println ("10% Increase" + sessionName); info.salary += (info.salary * 10) / 100; } else if (sessionName.endsWith ("1")) { System.out.println ("20% Increase" + sessionName); info.salary += (info.salary * 20) / 100; } else { System.out.println ("30% Decrease" + sessionName); info.salary -= (info.salary * 30) / 100; } // try sleeping this-thread for a while before updating the info // Note: the other threads MUST wait (since selected with 'for update' clause) this.sleep (2000); // update the infomation in the transaction employee.updateEmployee (info); // get and print the info in the transaction // Note: doNOT use 'for update' here info = employee.getEmployee ("SCOTT"); System.out.println ("End salary = " + info.salary + " in " + sessionName); // commit the changes TS.getTS ().getCurrent ().commit (true); } catch (Exception e) { e.printStackTrace (); } } }
package employee; import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.sql.*; public interface Employee extends EJBObject { public EmployeeInfo getEmployee (String name) throws RemoteException; public EmployeeInfo getEmployeeForUpdate (String name) throws RemoteException; public void updateEmployee (EmployeeInfo employee) throws RemoteException; }
package employee; import javax.ejb.EJBHome; import javax.ejb.CreateException; import java.rmi.RemoteException; public interface EmployeeHome extends EJBHome { public Employee create () throws RemoteException, CreateException; }
package employee; import java.rmi.*; public class EmployeeInfo implements java.io.Serializable { public String name = null; public int number = 0; public double salary = 0; }
package employeeServer; import employee.*; import javax.ejb.SessionBean; import javax.ejb.CreateException; import javax.ejb.SessionContext; import java.rmi.RemoteException; import java.sql.*; public class EmployeeBean implements SessionBean { // Methods of the Employee interface public EmployeeInfo getEmployee (String name) throws RemoteException { try { int empno = 0; double salary = 0.0; #sql { select empno, sal into :empno, :salary from emp where ename = :name }; EmployeeInfo info = new EmployeeInfo (); info.name = name; info.salary = salary; info.number = empno; return info; } catch (SQLException e) { // throw new SQLError (e.getMessage ()); } return null; } public EmployeeInfo getEmployeeForUpdate (String name) throws RemoteException { try { int empno = 0; double salary = 0.0; #sql { select empno, sal into :empno, :salary from emp where ename = :name for update }; EmployeeInfo info = new EmployeeInfo (); info.name = name; info.salary = salary; info.number = empno; System.out.println ("name = " + name + " salary = " + salary); return info; } catch (SQLException e) { // throw new SQLError (e.getMessage ()); } return null; } public void updateEmployee (EmployeeInfo employee) throws RemoteException { try { #sql { update emp set ename = :(employee.name), sal = :(employee.salary) where empno = :(employee.number) }; } catch (SQLException e) { // throw new SQLError (e.getMessage ()); } return; } // Methods of the SessionBean public void ejbCreate () throws RemoteException, CreateException {} public void ejbRemove() {} public void setSessionContext (SessionContext ctx) {} public void ejbActivate () {} public void ejbPassivate () {} }
import employee.Employee; import employee.EmployeeHome; import employee.EmployeeInfo; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; public class Client { public static void main (String[] args) throws Exception { if (args.length != 4) { System.out.println ("usage: Client serviceURL objectName user password"); System.exit (1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; // create InitialContext 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 ("Initial Context set up"); // get handle to the employee object System.out.println ("begin ic.lookup (" + serviceURL + objectName + ")"); EmployeeHome employee_home = (EmployeeHome)ic.lookup (serviceURL + objectName); System.out.println ("begin employee_home.create ()"); Employee employee = employee_home.create (); EmployeeInfo info = employee.getEmployee ("SCOTT"); System.out.println ("Beginning salary = " + info.salary); // do work on the info-object info.salary += (info.salary * 10) / 100; // call update on the server-side employee.updateEmployee (info); System.out.println ("End salary = " + info.salary); } }
SessionBean employeeServer.EmployeeBean { BeanHomeName = "test/myEmployee"; RemoteInterfaceClassName = employee.Employee; HomeInterfaceClassName = employee.EmployeeHome; AllowedIdentities = { PUBLIC }; RunAsMode = CLIENT_IDENTITY; TransactionAttribute = TX_BEAN_MANAGED; }
package employee; import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.sql.*; public interface Employee extends EJBObject { public EmployeeInfo getEmployee (String name) throws RemoteException, SQLException; public void updateEmployee (EmployeeInfo employee) throws RemoteException, SQLException; }
package employee; import javax.ejb.EJBHome; import javax.ejb.CreateException; import java.rmi.RemoteException; public interface EmployeeHome extends EJBHome { public Employee create () throws RemoteException, CreateException; }
package employee; import java.rmi.*; public class EmployeeInfo implements java.io.Serializable { public String name = null; public int number = 0; public double salary = 0; public EmployeeInfo (String name, int number, double salary) { this.name = name; this.number = number; this.salary = salary; } }
package employeeServer; import employee.*; import javax.ejb.SessionBean; import javax.ejb.CreateException; import javax.ejb.SessionContext; import javax.jts.UserTransaction; import java.rmi.RemoteException; import java.sql.*; public class EmployeeBean implements SessionBean { SessionContext ctx; // Methods of the Employee interface public EmployeeInfo getEmployee (String name) throws RemoteException, SQLException { ctx.getUserTransaction ().begin (); int empno = 0; double salary = 0.0; #sql { select empno, sal into :empno, :salary from emp where ename = :name }; return new EmployeeInfo (name, empno, salary); } public void updateEmployee (EmployeeInfo employee) throws RemoteException, SQLException { #sql { update emp set ename = :(employee.name), sal = :(employee.salary) where empno = :(employee.number) }; ctx.getUserTransaction ().commit (); } // Methods of the SessionBean public void ejbCreate () throws RemoteException, CreateException {} public void ejbRemove() {} public void setSessionContext (SessionContext ctx) { this.ctx = ctx; } public void ejbActivate () {} public void ejbPassivate () {} }
import employee.Employee; import employee.EmployeeHome; import employee.EmployeeInfo; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; public class Client { public static void main (String[] args) throws Exception { if (args.length != 4) { System.out.println ("usage: Client serviceURL objectName user password"); System.exit (1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; // create InitialContext 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); // get handle to the employee object System.out.println ("begin ic.lookup (" + serviceURL + objectName + ")"); EmployeeHome employee_home = (EmployeeHome)ic.lookup (serviceURL + objectName); System.out.println ("begin employee_home.create ()"); Employee employee = employee_home.create (); EmployeeInfo info = employee.getEmployeeForUpdate ("SCOTT"); System.out.println ("Beginning salary = " + info.salary); // do work on the info-object info.salary += (info.salary * 10) / 100; // call update on the server-side employee.updateEmployee (info); // re-query for the info object EmployeeInfo newInfo = employee.getEmployee ("SCOTT"); System.out.println ("End salary = " + newInfo.salary); } }
// This the generic database work bean template SessionBean employeeServer.EmployeeBean { BeanHomeName = "test/myEmployee"; RemoteInterfaceClassName = employee.Employee; HomeInterfaceClassName = employee.EmployeeHome; AllowedIdentities = { PUBLIC }; RunAsMode = CLIENT_IDENTITY; TransactionAttribute = TX_BEAN_MANAGED; /* SessionTimeout = 10; StateManagementType = STATEFUL_SESSION; EnvironmentProperties { prop1 = value1; prop2 = "value two"; } public java.lang.String getEmployee () throws RemoteException, SQLException { TransactionAttribute = TX_BEAN_MANAGED; RunAsMode = CLIENT_IDENTITY; AllowedIdentities = { PUBLIC }; } public java.lang.String updateEmployee () throws RemoteException, SQLException { TransactionAttribute = TX_BEAN_MANAGED; RunAsMode = CLIENT_IDENTITY; AllowedIdentities = { PUBLIC }; } */ }
drop table log_table cascade constraints; create table log_table (when date, which number, who number, what varchar2(2000)); exit
package employee; import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.sql.*; public interface Employee extends EJBObject { public EmployeeInfo getEmployee (String name) throws RemoteException, SQLException; public EmployeeInfo getEmployeeForUpdate (String name) throws RemoteException, SQLException; public void updateEmployee (EmployeeInfo employee) throws RemoteException, SQLException; }
package employee; import javax.ejb.EJBHome; import javax.ejb.CreateException; import java.rmi.RemoteException; public interface EmployeeHome extends EJBHome { public Employee create () throws RemoteException, CreateException; }
package employee; import java.rmi.*; public class EmployeeInfo implements java.io.Serializable { public String name = null; public int number = 0; public double salary = 0; public EmployeeInfo (String name, int number, double salary) { this.name = name; this.number = number; this.salary = salary; } }
package employeeServer; import employee.*; import loggingServer.Logging; import loggingServer.LoggingHome; import javax.ejb.SessionBean; import javax.ejb.CreateException; import javax.ejb.SessionContext; import javax.jts.UserTransaction; import java.rmi.RemoteException; import java.sql.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.Hashtable; public class EmployeeBean implements SessionBean { SessionContext ctx; Logging logServer = null; // Methods of the Employee interface public EmployeeInfo getEmployee (String name) throws RemoteException, SQLException { ctx.getUserTransaction ().begin (); int empno = 0; double salary = 0.0; #sql { select empno, sal into :empno, :salary from emp where ename = :name }; ctx.getUserTransaction ().commit (); return new EmployeeInfo (name, empno, salary); } public EmployeeInfo getEmployeeForUpdate (String name) throws RemoteException, SQLException { ctx.getUserTransaction ().begin (); int empno = 0; double salary = 0.0; logServer.log ("EJB: getEmployeeForUpdate (" + name + ")"); #sql { select empno, sal into :empno, :salary from emp where ename = :name for update }; return new EmployeeInfo (name, empno, salary); } public void updateEmployee (EmployeeInfo employee) throws RemoteException, SQLException { logServer.log ("EJB: updateEmployee (" + employee.name + ")"); #sql { update emp set ename = :(employee.name), sal = :(employee.salary) where empno = :(employee.number) }; ctx.getUserTransaction ().commit (); } // Methods of the SessionBean public void ejbCreate () throws RemoteException, CreateException { try { // create InitialContext Hashtable env = new Hashtable (); env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); Context ic = new InitialContext (env); // Now, to create the loggingBean String objectName = new String ("/test/loggingService"); LoggingHome logBean_home = (LoggingHome)ic.lookup ("sess_iiop://thisServer" + objectName); logServer = logBean_home.create (); } catch (NamingException e) { e.printStackTrace (); } try { logServer.log ("EJB: Create Employee"); } catch (SQLException e) { e.printStackTrace (); } } public void ejbRemove () {} public void setSessionContext (SessionContext ctx) { this.ctx = ctx; } public void ejbActivate () {} public void ejbPassivate () {} }
package loggingServer; import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.sql.*; public interface Logging extends EJBObject { public void log (String message) throws RemoteException, SQLException; }
package loggingServer; import javax.ejb.*; import java.rmi.RemoteException; import java.sql.*; import oracle.aurora.rdbms.DbmsJava; import oracle.aurora.rdbms.Schema; public class LoggingBean implements SessionBean { SessionContext ctx; public void log (String message) throws RemoteException, SQLException { int ownerNumber = Schema.currentSchema ().ownerNumber (); // System.out.println ("ownerNumber = " + ownerNumber); // get the session-id int sessID = DbmsJava.sessionID (DbmsJava.USER_SESSION); #sql { insert into log_table (who, which, when, what) values (:ownerNumber, :sessID, sysdate, :message) }; } public void ejbCreate () throws RemoteException, CreateException {} public void ejbRemove () {} public void setSessionContext (SessionContext ctxArg) { ctx = ctxArg; } public void ejbActivate () {} public void ejbPassivate () {} }
package loggingServer; import javax.ejb.*; import java.rmi.RemoteException; public interface LoggingHome extends EJBHome { public Logging create () throws RemoteException, CreateException; }
// This the generic database work bean template SessionBean loggingServer.LoggingBean { BeanHomeName = "test/loggingService"; RemoteInterfaceClassName = loggingServer.Logging; HomeInterfaceClassName = loggingServer.LoggingHome; TransactionAttribute = TX_REQUIRES_NEW; RunAsMode = CLIENT_IDENTITY; AllowedIdentities = { PUBLIC }; EnvironmentProperties { prop1 = value1; prop2 = "value two"; } }