Win a copy of Java Challengers this week in the Java in General forum!

Tiger Liu

Greenhorn
+ Follow
since Nov 19, 2002
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tiger Liu

I change my Bean as following:
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 String dbName = "java:comp/env/jdbc/MyOracle";
private SessionContext sessionContext;
private DataSource ds ;

public void addrecord(String id, String firstname, String lastname) {
try {
insertEntry(ds,id, firstname, lastname);
} catch (Exception ex) {
throw new EJBException("add record: " +
ex.getMessage());
}
}
public void selectRecord(String id) {
try {
selectEntry(ds,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 {
ds = null ;
System.out.println("connection is closed ...") ;
} catch (Exception 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 {
ds = null ;
System.out.println("connection is closed ...") ;
} catch (Exception 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();
ds = (DataSource) ic.lookup(dbName);
System.out.println("connection is opened ...") ;
}
/* 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(DataSource ds,String id, String first, String last){
PreparedStatement prepStmt = null ;
Connection cn = null ;
try{
cn = ds.getConnection() ;
String insertStatement =
"insert into tbltest values ( ? , ? , ? )";
prepStmt =
cn.prepareStatement(insertStatement);
prepStmt.setString(1, id) ;
prepStmt.setString(2, first) ;
prepStmt.setString(3, last) ;
prepStmt.executeUpdate();
prepStmt.close();
}catch(SQLException ex){
System.out.println("execute inset sql fail: " + ex.getMessage() ) ;
ex.printStackTrace() ;
}finally{
try{
if(prepStmt != null)
prepStmt.close() ;
if(cn != null)
cn.close() ;
}catch (SQLException ex) {
ex.printStackTrace() ;
}
}
}
private void selectEntry(DataSource ds,String id){
Connection cn = null ;
PreparedStatement prepStmt = null ;
ResultSet rs = null ;
try{
cn = ds.getConnection() ;
String deleteStatement =
"select id, firstname, lastname from tbltest where id = ?";
prepStmt = cn.prepareStatement(deleteStatement);
prepStmt.setString(1, id) ;
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){
ex.printStackTrace() ;
System.out.println("sql execute select fail: " + ex.getMessage() ) ;
}finally{
try{
if(rs != null)
rs.close() ;
if(prepStmt != null)
prepStmt.close() ;
if(cn != null)
cn.close() ;
}catch (SQLException ex) {
ex.printStackTrace() ;
}
}
}
}
=================================================
=================================================
but when i run it again , the following error message occurs:
[02.11.22 17:26:49:919 CST] 2bb97b12 Server A WSVR0023I:服务器 Default Server 为电子商务开放
[02.11.22 17:26:59:871 CST] 5d6abb10 SystemOut U set session context load ...
[02.11.22 17:26:59:881 CST] 5d6abb10 SystemOut U ejb create load ...
[02.11.22 17:27:00:262 CST] 5d6abb10 SystemOut U connection is opened ...
com.ibm.ejs.cm.exception.TransactionAbortedException: JTS/JTA transaction has been aborted
at com.ibm.ejs.cm.pool.ConnectO.enlist(ConnectO.java:707)
at com.ibm.ejs.cm.pool.ConnectO.enlist(ConnectO.java:373)
at com.ibm.ejs.cm.pool.ConnectO.preInvoke(ConnectO.java:1300)
at com.ibm.ejs.cm.proxy.Proxy.__preInvoke(Proxy.java:113)[02.11.22 17:27:03:356 CST] 5d6abb10 ConnectO X CONM6016E:未能加入事务。抛出新的 (TransactionAbortedException)
at com.ibm.ejs.cm.proxy.Proxy.__preInvoke(Proxy.java:98)
at com.ibm.ejs.cm.proxy.OracleConnectionProxy.prepareStatement(OracleConnectionProxy.java:150)
at hello.StateSession1Bean.selectEntry(StateSession1Bean.java:150)
at hello.StateSession1Bean.selectRecord(StateSession1Bean.java:37)
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)
[02.11.22 17:27:03:766 CST] 5d6abb10 SystemOut U sql execute select fail: JTS/JTA transaction has been aborted
=================================================
=================================================
and the Client throws exception is :
Initializing bean access.
Initializing bean lookup...
Initializing bean narrow...
Succeeded initializing bean access.
Succeeded create remote object.
Failed initializing bean access.CORBA TRANSACTION_ROLLEDBACK 0 No; nested exception is:
org.omg.CORBA.TRANSACTION_ROLLEDBACK: com.ibm.websphere.csi.CSITransactionRolledbackException:
at com.ibm.ejs.csi.TranStrategy.commit(TranStrategy.java:194)
at com.ibm.ejs.csi.TranStrategy.postInvoke(TranStrategy.java:67)
at com.ibm.ejs.csi.TransactionControlImpl.postInvoke(TransactionControlImpl.java:411)
at com.ibm.ejs.container.EJSContainer.postInvoke(EJSContainer.java:2368)
at hello.EJSRemoteStatefulStateSession1.selectRecord(EJSRemoteStatefulStateSession1.java:60)
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)
minor code: 0 completed: No
at com.ibm.ejs.container.EJSContainer.postInvoke(EJSContainer.java:2407)
at hello.EJSRemoteStatefulStateSession1.selectRecord(EJSRemoteStatefulStateSession1.java:60)
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)
minor code: 0 completed: No
=================================================
=================================================
please tell me how to correct it, thank you very much!
18 years ago
Because I use Jbuider+WebsphereAES4.0 integration, the Websphere4.0 only supports the J2EE1.2(EJB1.1), but I program the stateful Bean through J2EE1.3 tutorial, perhaps this is the problem I think, please give me some advice, thank u very much!
18 years ago
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?
18 years ago
I have already repaired it successfully.
The server-cfg.xml file only leaves a half of info, so I paste another half info into it from backup file, then it works smoothly!
18 years ago
So badlly! When i config the data source from administrate web page, some conflict occurs. As result, when i restart the app server, it cann't work at all! how should i do?
18 years ago