package examples.ejb.sequence.oracle; import java.io.Serializable; import java.rmi.RemoteException; import java.rmi.Remote; import javax.ejb.*; import java.util.*; import java.sql.*; /** * OracleBean is an EntityBean. This EJBean illustrates: * * * @author Copyright (c) 1998 by WebLogic, Inc. All Rights Reserved. * @author Copyright (c) 1998-1999 by BEA WebXpress. All Rights Reserved. */ public class OracleBean implements EntityBean { final static boolean VERBOSE = true; // ----------------------------------------------------------------- // private variables private transient EntityContext ctx; private transient Properties props; // ----------------------------------------------------------------- // public container managed variables public Integer accountId; // also the primary Key public double salary; public static int lastKey = 0; public static int maxKey = 0; // ----------------------------------------------------------------- // EntityBean implementation /** * Returns the Primary Key identifying this EJBean. * * @return String Identification */ private String id() { return "" + System.identityHashCode(this) + ", PK = " + (String) ((ctx == null) ? "nullctx" : ((ctx.getPrimaryKey() == null ? "null" : ctx.getPrimaryKey().toString()))); } /** * This method is required by the EJB Specification, * but is not used by this example. * */ public void ejbActivate() { if (VERBOSE) System.out.println("ejbActivate (" + id() + ")"); } /** * This method is required by the EJB Specification, * but is not used by this example. * */ public void ejbPassivate() { if (VERBOSE) System.out.println("ejbPassivate (" + id() + ")"); } /** * Sets the EntityContext for the EJBean. * * @param ctx EntityContext */ public void setEntityContext(EntityContext ctx) { if (VERBOSE) System.out.println("setEntityContext (" + id() + ")"); this.ctx = ctx; props = ctx.getEnvironment(); } /** * Unsets the EntityContext for the EJBean. * */ public void unsetEntityContext() throws RemoteException { if (VERBOSE) System.out.println("unsetEntityContext (" + id() + ")"); this.ctx = null; props = null; } /** * This method is required by the EJB Specification, * but is not used by this example. * */ public void ejbLoad() { if (VERBOSE) System.out.println("ejbLoad: (" + id() + ")"); } /** * This method is required by the EJB Specification, * but is not used by this example. * */ public void ejbStore() { if (VERBOSE) System.out.println("ejbStore (" + id() + ")"); } /** * This method is required by the EJB Specification, * but is not used by this example. * * @exception javax.ejb.RemoveException * if the EJBean does not allow removing the EJBean */ public void ejbRemove() throws RemoveException { if (VERBOSE) System.out.println("ejbRemove (" + id() + ")"); } /** * This method corresponds to the create method in the home interface * "OracleHome.java". * The parameter sets of the two methods are identical. When the client calls * OracleHome.create(), the container (which in WebLogic EJB is * also the home) allocates an instance of this EJBean and * calls OracleBean.ejbCreate(). *

* For container-managed persistence, ejbCreate() returns * a void, unlike the case of bean-managed * persistence, where it returns a primary key. * * @param accountID Integer Account ID * @param initialSalary double Initial salary * @exception javax.ejb.CreateException * if there is a problem creating the bean */ public void ejbCreate(Integer accountId, double initialSalary) throws CreateException { if (VERBOSE) System.out.println("OracleBean.ejbCreate( id = " + System.identityHashCode(this) + ", PK = " + accountId + ", " + "initial salary = $ " + initialSalary + ")"); this.accountId = accountId; this.salary = initialSalary; } /** * This method corresponds to the create method in the home interface * "OracleHome.java". * The parameter sets of the two methods are identical. When the client calls * OracleHome.create(), the container (which in WebLogic EJB is * also the home) allocates an instance of this bean and * calls OracleBean.ejbCreate(). *

* For bean-managed persistence, ejbCreate() returns * a primary key, unlike the case of container-managed * persistence, where it returns a void. *

* Unlike the ejbCreate(Integer accountId, double initialSalary), * this version creates a new AccountID using getNewAccountId(). * * @param initialSalary double Initial Salary * @exception javax.ejb.CreateException * if there is a problem getting an account ID */ public void ejbCreate(double initialSalary) throws CreateException { try { ejbCreate(getNewAccountId(), initialSalary); } catch (Exception e) { throw new CreateException (e.getMessage()); } } /** * This method gets the next account ID. If there are no * more IDs available in the current range, it goes to the * database and retreives a new range of IDs. * The range is set by a parameter in the Deployment Descriptor. * The current range of available IDs is stored in * lastKey and maxKey. * * @return Integer Next account ID * @exception Exception * if there is a problem getting an account ID */ private Integer getNewAccountId() throws Exception { if (VERBOSE) System.out.println("getNewAccountId(); lastKey: " + lastKey + " maxKey: " + maxKey); if (lastKey >= maxKey) { // Go to database and set new values for lastKey and maxKey if (VERBOSE) System.out.println("lastKey >= maxKey: getting new range of accountIds"); Connection connect = null; Statement statement = null; ResultSet results = null; int range = getRange(); String alterSeq = "alter sequence " + getSequenceName() + " increment by "; try { connect = getConnection(); statement = connect.createStatement(); statement.execute(alterSeq + new Integer(range).toString()); statement.executeQuery("select " + getSequenceName() + ".nextval from DUAL"); results = statement.getResultSet(); if ((results != null) && (results.next())) { maxKey = results.getInt(1); lastKey = maxKey - range; } else { throw new CreateException ("ejbCreate: sequence failed to return a value"); } } catch (SQLException sqe) { throw new CreateException (sqe.getMessage()); } finally { if (statement != null) statement.close(); if (results != null) results.close(); connect.close(); } } return new Integer(++lastKey); } /** * This method is required by the EJB Specification, * but is not used by this example. * * @param accountID Integer Account Identification * @param initialSalary double Initial Salary */ public void ejbPostCreate(Integer accountId, double initialSalary) { if (VERBOSE) System.out.println("ejbPostCreate (" + id() + ")"); } /** * This method is required by the EJB Specification, * but is not used by this example. * * @param initialSalary double Initial Salary */ public void ejbPostCreate(double initialSalary) { if (VERBOSE) System.out.println("ejbPostCreate (" + id() + ")"); } // Application defined methods /** * Returns current salary. * * @return double Salary */ public double salary() { return salary; } /** * Returns account ID. * * @return Integer Account ID */ public Integer accountId() { return accountId; } /** * Forces static initialization of weblogic.jdbc.jts.Driver * */ static { new weblogic.jdbc.jts.Driver(); // force static initialization } /** * Returns the name of the table used for generating IDs * * @return String Name of table for generating IDs */ private String getIdTableName() { return (String) props.get("idTable"); } /** * Returns the Oracle sequence name. * * @return String Oracle sequence name */ private String getSequenceName() { return (String) props.get("sequenceName"); } /** * Returns the number of unique ids to be allocated each time * * @return String Range of ids */ private int getRange() { return new Integer((String) props.get("creationRange")).intValue(); } /** * Returns value of a property listed in the "jdbc" property of the * "persistentStoreProperties" entry of the deployment descriptor. * * @param propertyName String Name of property to retrieve * @return String Property value */ private String getJdbcProperty(String propertyName) { Hashtable ht; ht = (Hashtable)props.get("persistentStoreProperties"); if (ht != null) { String psType = (String)ht.get("persistentStoreType"); if (psType != null && psType.equalsIgnoreCase("jdbc")) { ht = (Hashtable)ht.get(psType); if (ht != null) return (String)ht.get(propertyName); } } return null; } /** * Returns a connection from the database pool defined in the * deployment descriptor for the bean. * * @return Connection Database connection * @exception java.sql.SQLException * if there is an error getting a Connection */ private Connection getConnection() throws SQLException { return DriverManager.getConnection("jdbc:weblogic:jts:" + getJdbcProperty("poolName")); } }