I modify my program according to your advice.
But errors occur.
I don't know what bring the errors.

On jboss server:
[AccountBean] Connected to Oracle!
[AccountBean] TRANSACTION ROLLBACK EXCEPTION:ejbCreate: null; nested exception i
s:
        javax.ejb.EJBException: ejbCreate: null
[AccountBean] javax.ejb.EJBException: ejbCreate: null
[AccountBean]   at com.sasgz.ejb.account.AccountEJB.ejbCreate(AccountEJB.java:75
)
[AccountBean]   at java.lang.reflect.Method.invoke(Native Method)
[AccountBean]   at org.jboss.ejb.plugins.BMPPersistenceManager.createEntity(BMPP
ersistenceManager.java:120)
[AccountBean]   at org.jboss.ejb.EntityContainer.createHome(EntityContainer.java
:441)
[AccountBean]   at java.lang.reflect.Method.invoke(Native Method)
[AccountBean]   at org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeHome
(EntityContainer.java:639)
[AccountBean]   at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invoke
Home(EntitySynchronizationInterceptor.java:160)
[AccountBean]   at org.jboss.ejb.plugins.EntityInstanceInterceptor.invokeHome(En
tityInstanceInterceptor.java:86)
[AccountBean]   at org.jboss.ejb.plugins.TxInterceptorCMT.invokeNext(TxIntercept
orCMT.java:135)
[AccountBean]   at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(Tx
InterceptorCMT.java:263)
[AccountBean]   at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxIntercept
orCMT.java:86)
[AccountBean]   at org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(Security
Interceptor.java:126)
[AccountBean]   at org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogIntercepto
r.java:106)
[AccountBean]   at org.jboss.ejb.EntityContainer.invokeHome(EntityContainer.java
:316)
[AccountBean]   at org.jboss.ejb.plugins.jrmp.server.JRMPContainerInvoker.invoke
Home(JRMPContainerInvoker.java:143)
[AccountBean]   at java.lang.reflect.Method.invoke(Native Method)
[AccountBean]   at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.jav
a:241)
[AccountBean]   at sun.rmi.transport.Transport$1.run(Transport.java:142)
[AccountBean]   at java.security.AccessController.doPrivileged(Native Method)
[AccountBean]   at sun.rmi.transport.Transport.serviceCall(Transport.java:139)
[AccountBean]   at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTranspor
t.java:443)
[AccountBean]   at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPT
ransport.java:643)
[AccountBean]   at java.lang.Thread.run(Thread.java:484)

on client:
Caught an exception.
java.rmi.ServerException: RemoteException occurred in server thread; nested exception 
is:
        javax.transaction.TransactionRolledbackException: ejbCreate: null; nested 
exception is:
        javax.ejb.EJBException: ejbCreate: null
javax.transaction.TransactionRolledbackException: ejbCreate: null; nested exception 
is:
        javax.ejb.EJBException: ejbCreate: null
javax.ejb.EJBException: ejbCreate: null
        at 
sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:245)

        at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:220)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:122)
        at 
org.jboss.ejb.plugins.jrmp.server.JRMPContainerInvoker_Stub.invokeHome(Unknown Source)
        at org.jboss.ejb.plugins.jrmp.interfaces.HomeProxy.invoke(HomeProxy.java:221)
        at $Proxy0.create(Unknown Source)
        at com.sasgz.ejb.account.AccountClient.main(AccountClient.java:34)

______________________________jboss.conf___________________________
<MLET CODE="org.jboss.jdbc.XADataSourceLoader" ARCHIVE="jboss.jar,oracle816jdbc.zip" 
CODEBASE="../../lib/ext/">
    <ARG TYPE="java.lang.String" VALUE="OracleORCLPool">
    <ARG TYPE="java.lang.String" VALUE="org.jboss.minerva.xa.XADataSourceImpl">
</MLET>

______________________________jboss.jcml____________________________
<mbean name="DefaultDomain:service=XADataSource,name=OracleORCLPool">
       <attribute name="Properties"></attribute>
       <attribute name="URL">jdbc:oracle:thin:@localhost:1521:orcl</attribute>
       <attribute name="GCMinIdleTime">1200000</attribute>
       <attribute name="JDBCUser">scott</attribute>
       <attribute name="MaxSize">0</attribute>
       <attribute name="Password">tiger</attribute>
       <attribute name="GCEnabled">false</attribute>
       <attribute name="InvalidateOnError">false</attribute>
       <attribute name="TimestampUsed">false</attribute>
       <attribute name="Blocking">true</attribute>
       <attribute name="GCInterval">120000</attribute>
       <attribute name="IdleTimeout">1800000</attribute>
       <attribute name="IdleTimeoutEnabled">false</attribute>
       <attribute name="LoggingEnabled">false</attribute>
       <attribute name="MaxIdleTimeoutPercent">1.0</attribute>
       <attribute name="MinSize">0</attribute>
     </mbean>

___________________________________Ejb class________________________
/*
 *
 * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved.
 *
 * This software is the proprietary information of Sun Microsystems, Inc.
 * Use is subject to license terms.
 *
 */
package com.sasgz.ejb.account;

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

public class AccountEJB implements EntityBean {

    private String id;
    private String firstName;
    private String lastName;
    private double balance;
    private EntityContext context;
    private Connection con;
    private String dbName = "java:comp/env/jdbc/AccountDB";

    private DataSource ds = null;

    public void debit(double amount)
       throws InsufficientBalanceException {

       if (balance - amount < 0) {
           throw new InsufficientBalanceException();
       }
       balance -= amount;
    }

    public void credit(double amount) {

       balance += amount;
    }

    public String getFirstName() {

       return firstName;
    }

    public String getLastName() {

       return lastName;
    }

    public double getBalance() {

       return balance;
    }

    public String ejbCreate(String id, String firstName,
       String lastName, double balance)
       throws CreateException {

       Connection conn = null;

       if (balance < 0.00) {
          throw new CreateException
             ("A negative initial balance is not allowed.");
       }

       try {

          conn =  ds.getConnection();

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

       finally {
             try { conn.close(); } catch (Exception ex) { }
       }

       this.id = id;
       this.firstName = firstName;
       this.lastName = lastName;
       this.balance = balance;

       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());
       }

      if (result.isEmpty()) {
         throw new ObjectNotFoundException("No rows found.");
      }
      else {
         return result;
      }
   }

   public Collection ejbFindInRange(double low, double high)
      throws FinderException {

      Collection result;

      try {
         result = selectInRange(low, high);

       } catch (Exception ex) {
           throw new EJBException("ejbFindInRange: " +
              ex.getMessage());
       }
      if (result.isEmpty()) {
         throw new ObjectNotFoundException("No rows found.");
      }
      else {
         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();
      InitialContext ic = new InitialContext();
      ds = (DataSource) ic.lookup(dbName);

      } catch (Exception ex) {
          throw new EJBException("Unable to connect to database. " +
             ex.getMessage());
      }
      System.out.println("Connected to Oracle!");
   }

   public void unsetEntityContext() {

      try {
         con.close();
      } catch (SQLException 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("ejbLoad: " +
              ex.getMessage());
       }
   }


   public void ejbPostCreate(String id, String firstName,
      String lastName, double balance) { }


/*********************** 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,
                           double balance) throws SQLException {

          String insertStatement =
                "insert into account values ( ? , ? , ? , ? )";
          PreparedStatement prepStmt =
                con.prepareStatement(insertStatement);

          prepStmt.setString(1, id);
          prepStmt.setString(2, firstName);
          prepStmt.setString(3, lastName);
          prepStmt.setDouble(4, balance);

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

   private void deleteRow(String id) throws SQLException {

      String deleteStatement =
            "delete from account 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 account 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 account 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 selectInRange(double low, double high)
      throws SQLException {

      String selectStatement =
            "select id from account " +
            "where balance between  ? and ?";
      PreparedStatement prepStmt =
            con.prepareStatement(selectStatement);

      prepStmt.setDouble(1, low);
      prepStmt.setDouble(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, balance " +
            "from account 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.balance = rs.getDouble(3);
         prepStmt.close();
      }
      else {
         prepStmt.close();
         throw new NoSuchEntityException("Row for id " + id +
            " not found in database.");
      }
   }


   private void storeRow() throws SQLException {

      String updateStatement =
            "update account set firstname =  ? ," +
            "lastname = ? , balance = ? " +
            "where id = ?";
      PreparedStatement prepStmt =
            con.prepareStatement(updateStatement);

      prepStmt.setString(1, firstName);
      prepStmt.setString(2, lastName);
      prepStmt.setDouble(3, balance);
      prepStmt.setString(4, id);
      int rowCount = prepStmt.executeUpdate();
      prepStmt.close();

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

} // AccountEJB
N�.n�+���n�,�ǫ�yb��(�H��� ��&N�����r��z6�ˬz�~X��
+�)�v�,r����GzZc�|(�H��� ��&

Reply via email to