Hi All,

I've just completed the EJB section of the J2EE tutorial and thought I'd
write my own bean-managed persistence entity bean just to make sure I'd
understood everything.

Anyway, I've written my own "EmployeeApp" which accesses a table
"employee":

create table employee (id VARCHAR(3) CONSTRAINT pk_employee PRIMARY KEY,
firstname VARCHAR(24), lastname VARCHAR(24), position VARCHAR(24),
salary NUMERIC(10,2), isDirector VARCHAR(1));


The EmployeeApp contains:

Bean
Home Interface
Remote Interface
Client

classes and they are pretty much the same as the SavingsAccount example
in the tutorial.  I've deployed it exactly the same way using the
"deploytool" that comes with the tutorial but I keep getting an
exception when I try to call create().

I've since updated the client to only do a findByPrimaryKey and then a
getPosition() [retrieves the position from the Employee class] but again
I get the Rollback exception.  The findByPrimaryKey method seems to
execute OK but the getPosition() throws the RollbackException.

I include the dd, client, interfaces and bean code along with the output
I'm getting and would really appreciate any help.  Sorry about the long
e-mail but I thought I'd include everything to help with diagnosis.


Many thanks,

Dan King.
[EMAIL PROTECTED]



OUTPUT:

C:\JAVA\j2eetutorial\examples\ears>set APPCPATH=EmployeeAppClient.jar

C:\JAVA\j2eetutorial\examples\ears>runclient -client EmployeeApp.ear
-name Emplo
yeeClient -textauth
Initiating login ...
Username = null
Enter Username:guest
Enter Password:guest123
Binding name:`java:comp/env/ejb/SimpleEmployee`
A
B
C
D
E
F
Caught an exception.
java.rmi.ServerException: RemoteException occurred in server thread;
nested exception is:    java.rmi.RemoteException: Transaction aborted
(possibly due to transaction time out).; nested exception is:
javax.transaction.RollbackException; nested
exception is: javax.transaction.RollbackException
java.rmi.RemoteException: Transaction aborted (possibly due to
transaction time out).; nested exception is:
javax.transaction.RollbackException; nested exception is:
javax.transaction.RollbackException
javax.transaction.RollbackException
        <<no stack trace available>>
Unbinding name:`java:comp/env/ejb/SimpleEmployee`
C:\JAVA\j2eetutorial\examples\ears>



DD:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise
JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>

<ejb-jar>

  <display-name>EmployeeJAR</display-name>

  <enterprise-beans>

    <entity>

      <display-name>EmployeeEJB</display-name>

      <ejb-name>EmployeeEJB</ejb-name>

      <home>EmployeeHome</home>

      <remote>Employee</remote>

      <ejb-class>EmployeeBean</ejb-class>

      <persistence-type>Bean</persistence-type>

      <prim-key-class>java.lang.String</prim-key-class>

      <reentrant>False</reentrant>

      <security-identity>

        <description></description>

        <use-caller-identity></use-caller-identity>

      </security-identity>

      <resource-ref>

        <res-ref-name>jdbc/EmployeeDB</res-ref-name>

        <res-type>javax.sql.DataSource</res-type>

        <res-auth>Container</res-auth>

        <res-sharing-scope>Shareable</res-sharing-scope>

      </resource-ref>

    </entity>

  </enterprise-beans>

  <assembly-descriptor>

    <method-permission>

      <unchecked />

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getFirstName</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getPrimaryKey</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Home</method-intf>

        <method-name>findByFirstName</method-name>

        <method-params>

          <method-param>java.lang.String</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Home</method-intf>

        <method-name>getEJBMetaData</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Home</method-intf>

        <method-name>remove</method-name>

        <method-params>

          <method-param>java.lang.Object</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getEJBHome</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>changePosition</method-name>

        <method-params>

          <method-param>java.lang.String</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Home</method-intf>

        <method-name>getHomeHandle</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getLastName</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Home</method-intf>

        <method-name>remove</method-name>

        <method-params>

          <method-param>javax.ejb.Handle</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Home</method-intf>

        <method-name>findByPrimaryKey</method-name>

        <method-params>

          <method-param>java.lang.String</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getHandle</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Home</method-intf>

        <method-name>findSalaryInRange</method-name>

        <method-params>

          <method-param>java.math.BigDecimal</method-param>

          <method-param>java.math.BigDecimal</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Home</method-intf>

        <method-name>create</method-name>

        <method-params>

          <method-param>java.lang.String</method-param>

          <method-param>java.lang.String</method-param>

          <method-param>java.lang.String</method-param>

          <method-param>java.lang.String</method-param>

          <method-param>java.math.BigDecimal</method-param>

          <method-param>java.lang.String</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getSalary</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getDirectorStatus</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getPosition</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>changeSalary</method-name>

        <method-params>

          <method-param>java.math.BigDecimal</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>changeDirectorStatus</method-name>

        <method-params>

          <method-param>java.lang.String</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>remove</method-name>

        <method-params />

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Home</method-intf>

        <method-name>findByLastName</method-name>

        <method-params>

          <method-param>java.lang.String</method-param>

        </method-params>

      </method>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>isIdentical</method-name>

        <method-params>

          <method-param>javax.ejb.EJBObject</method-param>

        </method-params>

      </method>

    </method-permission>

    <container-transaction>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getPosition</method-name>

        <method-params />

      </method>

      <trans-attribute>Required</trans-attribute>

    </container-transaction>

    <container-transaction>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getDirectorStatus</method-name>

        <method-params />

      </method>

      <trans-attribute>Required</trans-attribute>

    </container-transaction>

    <container-transaction>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>changePosition</method-name>

        <method-params>

          <method-param>java.lang.String</method-param>

        </method-params>

      </method>

      <trans-attribute>Required</trans-attribute>

    </container-transaction>

    <container-transaction>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>changeDirectorStatus</method-name>

        <method-params>

          <method-param>java.lang.String</method-param>

        </method-params>

      </method>

      <trans-attribute>Required</trans-attribute>

    </container-transaction>

    <container-transaction>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getSalary</method-name>

        <method-params />

      </method>

      <trans-attribute>Required</trans-attribute>

    </container-transaction>

    <container-transaction>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getLastName</method-name>

        <method-params />

      </method>

      <trans-attribute>Required</trans-attribute>

    </container-transaction>

    <container-transaction>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>remove</method-name>

        <method-params />

      </method>

      <trans-attribute>Required</trans-attribute>

    </container-transaction>

    <container-transaction>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>getFirstName</method-name>

        <method-params />

      </method>

      <trans-attribute>Required</trans-attribute>

    </container-transaction>

    <container-transaction>

      <method>

        <ejb-name>EmployeeEJB</ejb-name>

        <method-intf>Remote</method-intf>

        <method-name>changeSalary</method-name>

        <method-params>

          <method-param>java.math.BigDecimal</method-param>

        </method-params>

      </method>

      <trans-attribute>Required</trans-attribute>

    </container-transaction>

  </assembly-descriptor>

</ejb-jar>


EJB:

import java.sql.*;
import javax.sql.*;
import java.util.*;
import java.math.*;
import javax.ejb.*;
import javax.naming.*;

public class EmployeeBean implements EntityBean {

        private String id;
        private String firstName;
        private String lastName;
        private String position;
        private BigDecimal salary;
        private String isDirector;
        private EntityContext context;
        private Connection con;
        private String dbName = "java:comp/env/jdbc/EmployeeDB";

        public void changeSalary(BigDecimal newSalary) {
                salary = newSalary;
        }

        public void changeDirectorStatus(String newDirectorStatus) {
                isDirector = newDirectorStatus;
        }

        public void changePosition(String newPosition) {
                position = newPosition;
        }

        public String getFirstName() {
                return firstName;
        }

        public String getLastName() {
                return lastName;
        }

        public String getPosition() {
                return position;
        }

        public BigDecimal getSalary() {
                return salary;
        }

        public String getDirectorStatus() {
                return isDirector;
        }

        public String ejbCreate(String id, String firstName, String
lastName,
                String position, BigDecimal salary, String isDirector)
                throws CreateException {

                if (salary.signum() == -1) {
                        throw new CreateException
                                ("Can't make someone work and take their
money too!");
                }

                try {
                        insertRow(id, firstName, lastName, position,
salary, isDirector);
                } catch (Exception ex) {
                        throw new EJBException("ejbCreate:
"+ex.getMessage());
                }

                this.id=id;
                this.firstName=firstName;
                this.lastName=lastName;
                this.position=position;
                this.salary=salary;
                this.isDirector=isDirector;

                return id;
        }

        public String ejbFindByPrimaryKey(String primaryKey)
                throws FinderException {

                boolean result;

                try {
                        result = selectByPrimaryKey(primaryKey);
                } catch (Exception ex) {
                        throw new EJBException("ejbFindByPrimaryKey: " +
                                ex.getMessage());
                }

                if (result) {
                        return primaryKey;
                } else {
                        throw new ObjectNotFoundException
                                ("Row for id " + primaryKey + " not
found.");
                }
        }

        public Collection ejbFindByLastName(String lastName)
                throws FinderException {

                Collection result;

                try {
                        result = selectByLastName(lastName);
                } catch (Exception ex) {
                        throw new EJBException("ejbFindByLastName " +
                                ex.getMessage());
                }
                return result;
        }

        public Collection ejbFindByFirstName(String firstName)
                throws FinderException {

                Collection result;

                try {
                        result = selectByFirstName(firstName);
                } catch (Exception ex) {
                        throw new EJBException("ejbFindByFirstName " +
                                ex.getMessage());
                }
                return result;
        }

        public Collection ejbFindSalaryInRange(BigDecimal low,
BigDecimal high)
                throws FinderException {

                Collection result;

                try {
                        result = selectSalaryInRange(low, high);
                } catch (Exception ex) {
                        throw new EJBException("ejbFindSalaryInRange: "
+
                        ex.getMessage());
                }
                return result;
        }

        public void ejbRemove() {
                try {
                        deleteRow(id);
                } catch (Exception ex) {
                        throw new EJBException("ejbRemove: " +
ex.getMessage());
                }
        }

        public void setEntityContext(EntityContext context) {
                this.context = context;
                try {
                        makeConnection();
                } catch (Exception ex) {
                        throw new EJBException("setEntityContext: " +
ex.getMessage());
                }
        }

        public void unsetEntityContext() {
                try {
                        con.close();
                } catch (Exception ex) {
                        throw new EJBException("unsetEntityContext: " +
                                ex.getMessage());
                }
        }

        public void ejbActivate() {
                id = (String)context.getPrimaryKey();
        }

        public void ejbPassivate() {
                id = null;
        }

        public void ejbLoad() {
                try {
                        loadRow();
                } catch (Exception ex) {
                        throw new EJBException("ejbLoad: " +
ex.getMessage());
                }
        }

        public void ejbStore() {
                try {
                        storeRow();
                } catch (Exception ex) {
                        throw new EJBException("ejbStore: " +
ex.getMessage());
                }
        }

        public void ejbPostCreate(String id, String firstName, String
lastName,
                String position, BigDecimal salary, String isDirector) {
        }

/*********************** Database Routines *************************/
        private void makeConnection() throws NamingException,
SQLException {

                InitialContext ic = new InitialContext();
                DataSource ds = (DataSource) ic.lookup(dbName);
                con = ds.getConnection();
        }

        private void insertRow (String id, String firstName, String
lastName,
                String position, BigDecimal salary, String isDirector)

                throws SQLException {

                String insertStatement = "insert into employee values (
? , ? , ? , ? , ? , ? )";

                PreparedStatement prepStmt =
con.prepareStatement(insertStatement);

                prepStmt.setString(1, id);
                prepStmt.setString(2, firstName);
                prepStmt.setString(3, lastName);
                prepStmt.setString(4, position);
                prepStmt.setBigDecimal(5, salary);
                prepStmt.setString(6, isDirector);

                prepStmt.executeUpdate();
                prepStmt.close();
        }

        private void deleteRow(String id) throws SQLException {
                String deleteStatement = "delete from employee where id
= ? ";

                PreparedStatement prepStmt =
con.prepareStatement(deleteStatement);

                prepStmt.setString(1,id);
                prepStmt.executeUpdate();
                prepStmt.close();
        }

        private boolean selectByPrimaryKey(String primaryKey)
                throws SQLException {

                String selectStatement = "select id from employee where
id = ? ";

                PreparedStatement prepStmt =
con.prepareStatement(selectStatement);

                prepStmt.setString(1,primaryKey);

                ResultSet rs = prepStmt.executeQuery();
                boolean result = rs.next();
                prepStmt.close();
                return result;
        }

        private Collection selectByLastName(String lastName) throws
SQLException {
                String selectStatement = "select id from employee where
lastname = ? ";

                PreparedStatement prepStmt =
con.prepareStatement(selectStatement);

                prepStmt.setString(1,lastName);

                ResultSet rs = prepStmt.executeQuery();
                ArrayList a = new ArrayList();

                while (rs.next()) {
                        String id = rs.getString(1);
                        a.add(id);
                }

                prepStmt.close();
                return a;
        }

        private Collection selectByFirstName(String firstName) throws
SQLException {
                String selectStatement = "select id from employee where
firstname = ? ";

                PreparedStatement prepStmt =
con.prepareStatement(selectStatement);

                prepStmt.setString(1, firstName);

                ResultSet rs = prepStmt.executeQuery();
                ArrayList a = new ArrayList();

                while (rs.next()) {
                        String id = rs.getString(1);
                        a.add(id);
                }

                prepStmt.close();
                return a;
        }


        private Collection selectSalaryInRange(BigDecimal low,
BigDecimal high)
                throws SQLException {

                String selectStatement =
                        "select id from employee where balance between ?
and ? ";

                PreparedStatement prepStmt =
con.prepareStatement(selectStatement);

                prepStmt.setBigDecimal(1, low);
                prepStmt.setBigDecimal(2, high);

                ResultSet rs = prepStmt.executeQuery();
                ArrayList a = new ArrayList();

                while (rs.next()) {
                        String id = rs.getString(1);
                        a.add(id);
                }

                prepStmt.close();
                return a;
        }

        private void loadRow() throws SQLException {
                String selectStatement =
                        "select firstname, lastname, position, salary,
isDirector " +
                        "from employee where id = ? ";

                PreparedStatement prepStmt =
con.prepareStatement(selectStatement);

                prepStmt.setString(1, this.id);

                ResultSet rs = prepStmt.executeQuery();

                if (rs.next()) {
                        this.firstName = rs.getString(1);
                        this.lastName = rs.getString(2);
                        this.position = rs.getString(3);
                        this.salary = rs.getBigDecimal(4);
                        this.isDirector = rs.getString(5);
                        prepStmt.close();
                } else {
                        prepStmt.close();
                        throw new NoSuchEntityException("Row for id " +
id +
                                " not found in dataBase.");
                }
        }

        private void storeRow() throws SQLException {
                String updateStatement =
                        "update savingsaccount set firstname = ?," +
                        "lastname = ?," + "position = ?," + "salary =
?," +
                        "isDirector = ? where id = ?";

                PreparedStatement prepStmt =
con.prepareStatement(updateStatement);

                prepStmt.setString(1, firstName);
                prepStmt.setString(2, lastName);
                prepStmt.setString(3, position);
                prepStmt.setBigDecimal(4, salary);
                prepStmt.setString(5, isDirector);
                prepStmt.setString(6, id);

                int rowCount = prepStmt.executeUpdate();
                prepStmt.close();

                if (rowCount == 0) {
                        throw new EJBException("Storing row for id " +
id + "failed.");
                }
        }
} // EmployeeBean end



HOME INTERFACE:

import java.util.Collection;
import java.math.BigDecimal;
import java.rmi.RemoteException;
import javax.ejb.*;

public interface EmployeeHome extends EJBHome {

        public Employee create(String id, String firstName, String
lastName,
                                        String position, BigDecimal
salary, String isDirector)
                throws RemoteException, CreateException;

        public Employee findByPrimaryKey(String id)
                throws FinderException, RemoteException;

        public Collection findByFirstName(String firstName)
                throws FinderException, RemoteException;

        public Collection findByLastName(String lastName)
                throws FinderException, RemoteException;

        public Collection findSalaryInRange(BigDecimal low, BigDecimal
high)
                throws FinderException, RemoteException;
}


REMOTE INTERFACE:


import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.math.BigDecimal;

public interface Employee extends EJBObject {

        public String getFirstName()
                throws RemoteException;

        public String getLastName()
                throws RemoteException;

        public BigDecimal getSalary()
                throws RemoteException;

        public String getDirectorStatus()
                throws RemoteException;

        public String getPosition()
                throws RemoteException;

        public void changeSalary(BigDecimal newSalary)
                throws RemoteException;

        public void changeDirectorStatus(String newDirectorStatus)
                throws RemoteException;

        public void changePosition(String newPosition)
                throws RemoteException;
}

CLIENT:

import java.util.*;
import java.math.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

public class EmployeeClient {

        public static void main(String[] args) {

                try {

                        System.out.println("A");


                        Context initial = new InitialContext();

                        System.out.println("B");

                        Object objref =
initial.lookup("java:comp/env/ejb/SimpleEmployee");

                        System.out.println("C");


                        EmployeeHome home =
(EmployeeHome)PortableRemoteObject.narrow(objref,

EmployeeHome.class);

                        System.out.println("D");



                        BigDecimal K38K = new BigDecimal("38000.00");

                        //Employee matt = home.create("002", "Matt",
"King", "Engineer", K38K, "1");

                        //System.out.println("Matt King inserted to
dB");


                        System.out.println("E");


                        Employee dan = home.findByPrimaryKey("001");

                        System.out.println("F");


                        System.out.println("Got dan : position = " +
dan.getPosition());


                        System.out.println("G");


                        System.exit(0);

                } catch (Exception ex) {
                        System.err.println("Caught an exception.");
                        ex.printStackTrace();
                }
        }
}


===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to