This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help me for stateful session bean please!

 
Tiger Liu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic