The following is the code of Bean class:
package hello;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class StateSession1Bean implements SessionBean {
private Connection con;
private
String dbName = "java:comp/env/jdbc/MyOracle";
private SessionContext sessionContext;
public void addrecord(String id, String firstname, String lastname) {
try {
insertEntry(id, firstname, lastname);
} catch (Exception ex) {
throw new EJBException("add record: " +
ex.getMessage());
}
}
public void selectRecord(String id) {
try {
selectEntry(id) ;
}catch(Exception ex) {
throw new EJBException("add record: " +
ex.getMessage());
}
}
public void ejbCreate() {
System.out.println("ejb create load ...") ;
try {
makeConnection();
} catch (Exception ex) {
throw new EJBException("Unable to connect to database. " +
ex.getMessage());
}
}
public void ejbRemove() {
System.out.println("ejb remove load ...") ;
try {
con.close();
System.out.println("connection is closed ...") ;
} catch (SQLException ex) {
throw new EJBException("ejbRemove: " + ex.getMessage());
}
}
public void ejbActivate() {
System.out.println("ejb activate load ...") ;
try {
makeConnection();
} catch (Exception ex) {
throw new EJBException("ejbActivate Exception: " + ex.getMessage());
}
}
public void ejbPassivate() {
System.out.println("ejb passivate load ...") ;
try {
con.close();
System.out.println("connection is closed ...") ;
} catch (SQLException ex) {
throw new EJBException("ejbPassivate Exception: " + ex.getMessage());
}
}
public void setSessionContext(SessionContext sessionContext) {
System.out.println("set session context load ...") ;
this.sessionContext = sessionContext;
}
/*********************** Database Routines *************************/
private void makeConnection() throws NamingException, SQLException {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
con = ds.getConnection();
System.out.println("connection is opened ...") ;
// testConnection() ;
}
private void testConnection() {
try{
String deleteStatement =
"select id, firstname, lastname from tbltest";
PreparedStatement prepStmt =
con.prepareStatement(deleteStatement);
ResultSet rs = prepStmt.executeQuery();
System.out.println("sql execute : " + deleteStatement) ;
while (rs.next()) {
System.out.println("the record is : " + rs.getString(1) + "-" + rs.getString(2) + "-" + rs.getString(3));
}
}catch(SQLException ex){
System.out.println("sql
test exception " + ex.getMessage() ) ;
}
}
private void insertEntry(String id, String first, String last)
throws SQLException{
String insertStatement =
"insert into tbltest values ( ? , ? , ? )";
PreparedStatement prepStmt =
con.prepareStatement(insertStatement);
prepStmt.setString(1, id) ;
prepStmt.setString(2, first) ;
prepStmt.setString(3, last) ;
prepStmt.executeUpdate();
prepStmt.close();
}
private void selectEntry(String id)
throws SQLException{
String deleteStatement =
"select id, firstname, lastname from tbltest where id = ?";
PreparedStatement prepStmt =
con.prepareStatement(deleteStatement);
prepStmt.setString(1, id) ;
ResultSet rs = prepStmt.executeQuery();
System.out.println("sql execute : " + deleteStatement) ;
while (rs.next()) {
System.out.println("the record is : " + rs.getString(1) + "-" + rs.getString(2) + "-" + rs.getString(3));
}
}
}
====================================================================================================
When I invoke the business method in client, the error message is :
-- Initializing bean access.
-- Succeeded initializing bean access.
-- Execution time: 3524 ms.
-- Calling create()
-- Succeeded: create()
-- Execution time: 2163 ms.
-- Return value from create(): IOR:0000000000000029524d493a68656c6c6f2e537461746553657373696f6e313a3030 ...
-- Calling addrecord(01, 23232, 22332)
java.rmi.ServerException: ; nested exception is:
com.ibm.ejs.container.UncheckedException: ; nested exception is:
javax.ejb.EJBException: add record: Connection is closed
com.ibm.ejs.container.UncheckedException: ; nested exception is:
javax.ejb.EJBException: add record: Connection is closed
javax.ejb.EJBException: add record: Connection is closed
-- Failed: addrecord(01, 23232, 22332)
====================================================================================================
The error message from server :
[02.11.22 14:23:13:713 CST] 4f712513 SystemOut U set session context load ...
[02.11.22 14:23:13:933 CST] 4f712513 SystemOut U
ejb create load ...
[02.11.22 14:23:16:036 CST] 4f712513 SystemOut U connection is opened ...
[02.11.22 14:23:16:166 CST] 4f712513 ExceptionUtil X CNTR0020E:在 bean BeanId(HelloWorld.jar#HelloWorld.jar#StateSession1, f1aa65e834) 上处理方法 selectRecord 时发生非应用程序异常:javax.ejb.EJBException: add record: Connection is closed
at hello.StateSession1Bean.selectRecord(StateSession1Bean.java:39)
at hello.EJSRemoteStatefulStateSession1.selectRecord(EJSRemoteStatefulStateSession1.java:49)
at hello._EJSRemoteStatefulStateSession1_Tie._invoke(_EJSRemoteStatefulStateSession1_Tie.java:137)
at com.ibm.CORBA.iiop.ExtendedServerDelegate.dispatch(ExtendedServerDelegate.java:506)
at com.ibm.CORBA.iiop.ORB.process(ORB.java:2294)
at com.ibm.CORBA.iiop.OrbWorker.run(OrbWorker.java:185)
at com.ibm.ejs.oa.pool.ThreadPool$PooledWorker.run(ThreadPool.java:95)
at com.ibm.ws.util.CachedThread.run(ThreadPool.java:122)
====================================================================================================
The following is the DD which is built by Jbuilder:
<?xml version="1.0" encoding="UTF-8"?>
< !DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" "
http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">
<ejb-jar id="ejb-jar_ID">
<enterprise-beans>
<session id="Session_1">
<ejb-name>StateSession1</ejb-name>
<home>hello.StateSession1Home</home>
<remote>hello.StateSession1</remote>
<ejb-class>hello.StateSession1Bean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
<resource-ref id="ResourceRef_1_1">
<description />
<res-ref-name>jdbc/MyOracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Application</res-auth>
</resource-ref>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction id="MethodTransaction_1">
<method>
<ejb-name>StateSession1</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
====================================================================================================
I found that before the server invokes the addrecord, it doesn't invoke the ejbActivate mothod, but the connect is colsed, why?