I think that when I create a new instance,setEntityContext() will run once in EJBbean. In your sample code,you disconnect the database session in ejbCreate(). If I want to remove the row in the table,I must connect the database again. I think that it'll reduce the efficiency of program. If I don't disconnect the database session in ejbCreate(),I'll get the same result as before. Maybe I misundertand your issue. Thank you levels -----原始邮件----- 发件人: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]代表 Craig Day 发送时间: 2000年12月30日 18:49 收件人: jBoss 主题: Re: [jBoss-User] 答复: [jBoss-User] why ejbcreate doesn't = ?gb2312?B?IGluc2VydCByb3dzIGludG8gdGFibGU/? At 18:30 30/12/00, you wrote: >Please tell me how to cache the data source. >But I don't know why EJB can't delete the row from table account. >Thank you. You can do it in setEntityContext. Heres some sample code: public class CategoryBean extends CategoryInfo implements EntityBean { private transient boolean isDirty = false; private EntityContext context; private DataSource ds = null; private static final String dsName = "java:/craigd"; /** * Set the entity context for this bean and lookup the DataSource. */ public void setEntityContext(final EntityContext context) throws EJBException, RemoteException { log("in setEntityContext()"); this.context = context; try { InitialContext ic = new InitialContext(); ds = (DataSource)ic.lookup(dsName); } catch (Exception e) { throw new EJBException("Unable to get datasource:" + e.getMessage()); } } and then use it like this: public String ejbCreate(String categoryID, String categoryName) throws RemoteException, CreateException { Connection conn = null; PreparedStatement pstmt = null; log("in ejbCreate()"); try { conn = ds.getConnection(); etc. etc. etc. } catch (SQLException e) { throw new CreateException("ejbCreate: " + e.getMessage()); } finally { try { pstmt.close(); } catch (Exception ex) { } try { conn.close(); } catch (Exception ex) { } } } cheers craig >And I find if I start a session to oracle by scott/tiger through >sql*plus,then start the test client, >the application will freeze. > >levels > >-----原始邮件----- >发件人: [EMAIL PROTECTED] >[mailto:[EMAIL PROTECTED]]代表 Jay Walters >发送时间: 2000年12月30日 2:49 >收件人: 'jBoss' >主题: RE: [jBoss-User] why ejbcreate doesn't insert rows into table? > > >I don't believe you should be allocating a database connection in the >ejbActivate. You can cache the data source so you don't have to do the JNDI >lookup everytime. It sounds like in the end it's doing the right thing, >inserting 4 records. I would try and allocate the connection from within >the transaction and close it before the end. I am guessing this will get >things working as you expect. > >Cheers >Jay Walters > >-----Original Message----- >From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] >Sent: Friday, December 29, 2000 6:23 AM >To: [EMAIL PROTECTED] >Subject: [jBoss-User] why ejbcreate doesn't insert rows into table? > > > I'm using JBoss 2.0 and oracle8.0.6.I config as BMP.When I use client >access EJB,the program just check one inserted row.But in fact,I inert 5 >rows and remove 1 row.But when I access oracle through sql*net,there is only >one row.After I turn off JBoss,I access oracle through >sql*net again,and find that there is 4 rows int the table. I don't know how >to solve this problem. Please Help me. Thank you. > >___________________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"; > > 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 { > > if (balance < 0.00) { > throw new CreateException > ("A negative initial balance is not allowed."); > } > > try { > insertRow(id, firstName, lastName, balance); > } catch (Exception ex) { > throw new EJBException("ejbCreate: " + > ex.getMessage()); > } > > 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(); > } catch (Exception ex) { > throw new EJBException("Unable to connect to database. " + > ex.getMessage()); > } > } > > 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(); > System.out.println("insert"+id); > } > > 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 > >________________________________________________HOME >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.util.Collection; >import java.rmi.RemoteException; >import javax.ejb.*; > >public interface AccountHome extends EJBHome { > > public Account create(String id, String firstName, > String lastName, double balance) > throws RemoteException, CreateException; > > public Account findByPrimaryKey(String id) > throws FinderException, RemoteException; > > public Collection findByLastName(String lastName) > throws FinderException, RemoteException; > > public Collection findInRange(double low, double high) > throws FinderException, RemoteException; >} > >____________________________________________________REMOTE >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 javax.ejb.EJBObject; >import java.rmi.RemoteException; > >public interface Account extends EJBObject { > > public void debit(double amount) > throws InsufficientBalanceException, RemoteException; > > public void credit(double amount) > throws RemoteException; > > public String getFirstName() > throws RemoteException; > > public String getLastName() > throws RemoteException; > > public double getBalance() > throws RemoteException; >} > >_________________________________________TEST >CLIENT______________________________________________________________ >/* > * > * 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.util.*; >import javax.naming.Context; >import javax.naming.InitialContext; >import javax.rmi.PortableRemoteObject; > >public class AccountClient { > > public static void main(String[] args) { > // Set up the naming provider; this may not always be necessary, >depending > // on how your Java system is configured. > System.setProperty("java.naming.factory.initial", > "org.jnp.interfaces.NamingContextFactory"); > System.setProperty("java.naming.provider.url", > "localhost:1099"); > > try { > Context initial = new InitialContext(); > Object objref = initial.lookup("MyAccount"); > > AccountHome home = > (AccountHome)PortableRemoteObject.narrow(objref, > AccountHome.class); > > Account duke = home.create("123", "Duke", "Earl", 0.00); > duke.credit(88.50); > duke.debit(20.25); > double balance = duke.getBalance(); > System.out.println("balance = " + String.valueOf(balance)); > duke.remove(); > > Account joe = home.create("836", "Joe", "Jones", 0.00); > joe.credit(34.55); > Account jones = home.findByPrimaryKey("836"); > jones.debit(2.00); > balance = jones.getBalance(); > System.out.println("balance = " + String.valueOf(balance)); > > Account pat = home.create("456", "Pat", "Smith", 0.00); > pat.credit(44.77); > Account john = home.create("730", "John", "Smith", 0.00); > john.credit(19.54); > Account mary = home.create("268", "Mary", "Smith", 0.00); > mary.credit(100.07); > > Collection c = home.findByLastName("Smith"); > Iterator i=c.iterator(); > > while (i.hasNext()) { > Account account = (Account)i.next(); > String id = (String)account.getPrimaryKey(); > double amount = account.getBalance(); > System.out.println(id + ": " + String.valueOf(amount)); > } > > c = home.findInRange(20.00, 99.00); > i=c.iterator(); > > while (i.hasNext()) { > Account account = (Account)i.next(); > String id = (String)account.getPrimaryKey(); > double amount = account.getBalance(); > System.out.println(id + ": " + String.valueOf(amount)); > } > > } catch (InsufficientBalanceException ex) { > System.err.println("Caught an InsufficientBalanceException: " > + ex.getMessage()); > > } catch (Exception ex) { > System.err.println("Caught an exception." ); > ex.printStackTrace(); > } > } >} > >_________________________________________________ejb-jar.xml________________ >___________________________ ><?xml version="1.0" encoding="Cp1252"?> > ><ejb-jar> > <description>J2EE EJB Examples Application</description> > <display-name>J2EE Examples</display-name> > <enterprise-beans> > <entity> > <display-name>jBoss AccountJAR application</display-name> > <ejb-name>AccountBean</ejb-name> > <home>com.sasgz.ejb.account.AccountHome</home> > <remote>com.sasgz.ejb.account.Account</remote> > <ejb-class>com.sasgz.ejb.account.AccountEJB</ejb-class> > <persistence-type>Bean</persistence-type> > <prim-key-class>java.lang.String</prim-key-class> > <reentrant>False</reentrant> > <resource-ref> > <res-ref-name>jdbc/AccountDB</res-ref-name> > <res-type>javax.sql.DataSource</res-type> > <res-auth>Container</res-auth> > </resource-ref> > </entity> > </enterprise-beans> > <assembly-descriptor> > <container-transaction> > <method> > <ejb-name>AccountBean</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>AccountBean</ejb-name> > <method-intf>Remote</method-intf> > <method-name>credit</method-name> > <method-params> > <method-param>double</method-param> > </method-params> > </method> > <trans-attribute>Required</trans-attribute> > </container-transaction> > <container-transaction> > <method> > <ejb-name>AccountBean</ejb-name> > <method-intf>Remote</method-intf> > <method-name>debit</method-name> > <method-params> > <method-param>double</method-param> > </method-params> > </method> > <trans-attribute>Required</trans-attribute> > </container-transaction> > <container-transaction> > <method> > <ejb-name>AccountBean</ejb-name> > <method-intf>Remote</method-intf> > <method-name>getBalance</method-name> > <method-params /> > </method> > <trans-attribute>Required</trans-attribute> > </container-transaction> > <container-transaction> > <method> > <ejb-name>AccountBean</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>double</method-param> > </method-params> > </method> > <trans-attribute>Required</trans-attribute> > </container-transaction> > <container-transaction> > <method> > <ejb-name>AccountBean</ejb-name> > <method-intf>Remote</method-intf> > <method-name>getFirstName</method-name> > <method-params /> > </method> > <trans-attribute>Required</trans-attribute> > </container-transaction> > </assembly-descriptor> ></ejb-jar> > >________________________________jboss.xml___________________________________ >_________________ ><?xml version="1.0" encoding="Cp1252"?> > ><jboss> > <secure>false</secure> > <container-configurations /> > <resource-managers> > <resource-manager res-class="org.jboss.ejb.deployment.JDBCResource"> > <res-name>jdbc/AccountDB</res-name> > <res-jndi-name>OracleORCLPool</res-jndi-name> > </resource-manager> > </resource-managers> > <enterprise-beans> > <entity> > <ejb-name>AccountBean</ejb-name> > <jndi-name>MyAccount</jndi-name> > <configuration-name></configuration-name> > </entity> > </enterprise-beans> ></jboss> > >_________________________client error message____________________________ >balance = 68.25 >Caught an exception. >javax.ejb.ObjectNotFoundException: Row for id 836 not found. > at >sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteC >all.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(Unkno >wn Source) > at org.jboss.ejb.plugins.jrmp.interfaces.HomeProxy.invoke(HomeProxy. >java:221) > at $Proxy0.findByPrimaryKey(Unknown Source) > at com.sasgz.ejb.account.AccountClient.main(AccountClient.java:43) > >_______________________JBoss message________________________________ >[Bean Cache] Passivated overaged bean AccountBean with id = nullN?n?壏?呵 >yb曹(瓾?笔&N嫥叉靣笡z6璨爽z?~X >+?鄓?r墜娝GzZc(瓾?笔& > > >-- >-------------------------------------------------------------- >To subscribe: [EMAIL PROTECTED] >To unsubscribe: [EMAIL PROTECTED] >List Help?: [EMAIL PROTECTED] >ininining-ing-dogs.com >List Help?: [EMAIL PROTECTED] >N?n?壏?呵yb曹(瓾?笔&N嫥叉靣笡z6璨爽z簙X >+?鄓?r墜娝GzZc(瓾?笔&媻 -- -------------------------------------------------------------- To subscribe: [EMAIL PROTECTED] To unsubscribe: [EMAIL PROTECTED] List Help?: [EMAIL PROTECTED] �˛���m�ۢ�.���X�� +�)�v�,r����칻�&ލ�,����ߖ+-�x��b��ޖ�� +�)�v�,r�
