Building Business Entity EJBs This chapter describes how to use entity Enterprise JavaBeans (EJBs) to encapsulate your business entities. This chapter explains what a business entity EJB is and what entity beans must contain. It also provides additional guidelines for creating entity beans, and for determining the entity beans needs of your applications.
This chapter describes how to use entity Enterprise JavaBeans (EJBs) to encapsulate your business entities.
Introducing Business Entity EJBs
Entity Bean Components
Additional Entity Bean Guidelines
Determining the Business Objects in Your Applications
represents data in a database.
supports transactions.
executes for multiple clients.
persists for as long as needed by all clients.
transparently survives server crashes.
Enterprise Bean class
Enterprise Bean home interface, javax.ejb.EJBHome
Enterprise Bean remote interface, javax.ejb.EJBObject
import java.rmi.*; import java.util.*; import javax.ejb.*; public class MyEntityBean implements EntityBean { // Entity Bean implementation. These methods must always included. public void ejbActivate() throws RemoteException { } public void ejbLoad() throws Remote Exception { } public void ejbPassivate() throws RemoteException { } public void ejbRemove() throws RemoteException{ } public void ejbStore() throws RemoteException{ } public void setEntityContext(EntityContext ctx) throws RemoteException { } public void unsetEntityContext() throuws RemoteException { } // other code omitted here.... }
private transient javax.ejb.EntityContext m_ctx = null; // These define the state of our bean private int m_quantity; private int m_totalSold;
The container passivates session beans after they are inactive for a specified (or default) time. This timeout value is set in the bean's property file. For more information, see Specifying Passivation Timeout.
public void ejbLoad() throws java.rmi.RemoteException { String itemId; DatabaseConnection dc = null; java.sql.Statement stmt = null; java.sql.ResultSet rs = null; itemId = (String) m_ctx.getPrimaryKey(); System.out.println("myBean: Loading state for item " + itemId); String query = "SELECT s.totalSold, s.quantity " + " FROM Item s " + " WHERE s.item_id = " + itemId; dc = new DatabaseConnection(); dc.createConnection(DatabaseConnection.GLOBALTX); stmt = dc.createStatement(); rs = stmt.executeQuery(query); if (rs != null) { rs.next(); m_totalSold = rs.getInt(1); m_quantity = rs.getInt(2); } } public void ejbStore() throws java.rmi.RemoteException { String itemId; itemId = (String) m_ctx.getPrimaryKey(); DatabaseConnection dc = null; java.sql.Statement stmt1 = null; java.sql.Statement stmt2 = null; System.out.println("myBean: Saving state for item = " + itemId); String upd1 = "UPDATE Item " + " SET quantity = " + m_quantity + " WHERE item_id = " + itemId; String upd2 = "UPDATE Item " + " SET totalSold = " + m_totalSold + " WHERE item_id = " + itemId; dc = new DatabaseConnection(); dc.createConnection(DatabaseConnection.GLOBALTX); stmt1 = dc.createStatement(); stmt1.executeUpdate(upd1); stmt1.close(); stmt2 = dc.createStatement(); stmt2.executeUpdate(upd2); stmt2.close(); }
public void setEntityContext(javax.ejb.EntityContext ctx) { m_ctx = ctx; }
public void unsetEntityContext() { m_ctx = null; }
public int ejbCreate() { string[] userinfo = {"User Name", "Encrypted Password"}; }
public String ejbFindByPrimaryKey(String key) throws java.rmi.RemoteException, javax.ejb.FinderException { //System.out.println("@@@ myBean.ejbFindByPrimaryKey key = " + key); return key; }
import javax.ejb.*; import java.rmi.*; public interface MyEntityBeanHome extends EJBHome { MyEntityBean create() throws CreateException, RemoteException; }
import javax.ejb.*; import java.rmi.*; public interface MyEntityBean extends EJBObject { // define business method methods here.... }
The same name.
The same number and type of arguments, and the same return type.
The same exceptions defined in the throws clause, except that in the remote interface definition, the throws clause must also include java.rmi.RemoteException.
public void ejbLoad() throws java.rmi.RemoteException { .... // Get the lock on the corresponding DB table try { java.sql.Connection dbConn = ds.getConnection(); String query = "SELECT accountNum, balance FROM accounts " + "WHERE customerId = ? FOR UPDATE"; prepStmt = dbConn.prepareStatement(query); prepStmt.setString(1, m_customerId); resultSet = prepStmt.executeQuery(); if ((resultSet != null) && resultSet.next()) { acctNum = resultSet.getInt(1); acctBalance = resultSet.getInt(2); } else { throw new RemoteException("Database error. " + "Couldn't find accout"); } } catch (java.sql.SQLException e) { throw new RemoteException("Database error. " + "Couldn't load account"); } finally { try { if (resultSet != null) resultSet.close(); if (prepStmt != null) prepStmt.close(); if (dbConn != null) dbConn.disconnect(); } catch (java.sql.SQLException e) { System.out.println("Unexpected exception while " + "closing resources"); } } }