| Author |
Problem with CMT on Stateless Session beans
|
kumari Jain
Ranch Hand
Joined: Jul 28, 2004
Posts: 44
|
|
I have a stateless session bean called SessionCMTBean which uses Container Managed Transaction. There is only one business method which is exposed to the client through Remote interface called SessionCMT. This business method calls 2 methods of DAO to update 2 tables.I intentionally make the preparedStatement used in one DAO method to null so that one method succeeds and the other doesn't.But since these 2 methods are inside a single transaction ,so both of them shouldn't succeed.Hence updates to either of the tables shouldn't occur. Now the problem is that one method successfully updated database while other did not.There should infact be no updates occurring on either of these tables. Here is the code: public class SessionCMTBean implements SessionBean { ....... ..... public void addAllData(String empID,String empName,String stockID,String stockName) { stockDAO.addStock(stockID,stockName); stockDAO.addEmp(empID,empName,stockID); } } public class StockDAO { ...... ...... public void addStock(String stockID,String name) { Connection connection = datasource.getConnection(); String query = "Insert into jtaStock(stockid,description) values(?,?)"; PreparedStatement preparedStatement = connection.prepareStatement(query); preparedStatement.setString(1,stockID); preparedStatement.setString(2,name); preparedStatement.execute(); } public void addEmp(String empID,String name,String stockID) { Connection connection = datasource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement (query); preparedStatement = null; //Exception is thrown here preparedStatement.setString(1,empID); preparedStatement.setString(2,name); preparedStatement.setString(3,stockID); preparedStatement.execute(); } } ejb-jar.xml <assembly-descriptor> <container-transaction> <method> <ejb-name>SessionWithCMT</ejb-name> <method-name>addAllData</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> When the client calls business method addAllData(...) on the remote interface, addStock(..) method successfully inserts data in jtaStock table although addEmp(..) fails. Is there a problem with the code ??? regards, kumari
|
 |
Ugender Rekulampally
Ranch Hand
Joined: Nov 14, 2005
Posts: 130
|
|
Kumari, I don't see anything wrong with the code you provided for the cause you explained...But there must be some thing wrong with the DB insert even though another method in the same transaction throwing exception... What is the exception message you are getting for the second method? Thanks, Ugender
|
 |
Sajid Moinuddin
Ranch Hand
Joined: Mar 19, 2005
Posts: 85
|
|
|
Which DB are you using?...i faced a similar kind of problem caused by the db which didn't support transaction.
|
 |
kumari Jain
Ranch Hand
Joined: Jul 28, 2004
Posts: 44
|
|
hi Ugender, thanks for replying,this is the error i get on weblogic console. java.lang.NullPointerException at com.home.dao.StockDAO.addEmp(Unknown Source) at com.home.sessionbeans.cmt.SessionCMTBean.addAllData(Unknown Source) at com.home.sessionbeans.cmt.SessionWithCMT_d0r3pu_EOImpl.addAllData(Se sionWithCMT_d0r3pu_EOImpl.java:46) at com.home.sessionbeans.cmt.SessionWithCMT_d0r3pu_EOImpl_WLSkel.invoke Unknown Source) at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:477) at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServer ef.java:108) at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:420) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Authenticat dSubject.java:353) at weblogic.security.service.SecurityManager.runAs(SecurityManager.java 144) at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.ja a:415) at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteReques .java:30) at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197) at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170) Given below is the method from which exception is thrown public void addEmp(String empID,String name,String stockID) { Connection connection = datasource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement (query); preparedStatement = null; //Exception is thrown here preparedStatement.setString(1,empID); preparedStatement.setString(2,name); preparedStatement.setString(3,stockID); preparedStatement.execute(); } regards, kumari
|
 |
kumari Jain
Ranch Hand
Joined: Jul 28, 2004
Posts: 44
|
|
hi Sajid thanks to you too for replying. Regarding the DB,i'm using MySQL 5.0 When i set up the datasource from the weblogic console,i additionally specified 2 options in 'Global Transaction Options' section. 1.Honor Global Transactions : true 2.Emulate Two-Phase Commit for non-XA Driver : yes Inspite of this do you think ,the 2 method calls wouldn't run inside a single transaction ? regards, kumari
|
 |
Ugender Rekulampally
Ranch Hand
Joined: Nov 14, 2005
Posts: 130
|
|
Kumari, That was the expected exception. I think some reason the method <method-name>addAllData</method-name> is not in the transaction. There is no way if this method is in transaction and updating AddStock and ignoring AddEmp() method's run time exception. can you make sure that you mentioned your transaction type as Container when you define your bean in ejb-jar? like <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> if you did the above code also in your ejb-jar.xml, then I dont know what is wrong here... Thanks, Ugender
|
 |
satheesh krishnaswamy
Ranch Hand
Joined: Mar 17, 2004
Posts: 137
|
|
Hi, I agree with Ugender. If you dont specify the <transaction-type> as Container,the default will be Bean.Hence due to this, the container would have failed to execute the methods in Transaction. Please try what ugender has proposed and please let us know you are still in problem.
|
Regards,<br /> <br />Satheesh.K<br />-------------<br />SCJP 1.4<br />SCWCD 1.4
|
 |
kumari Jain
Ranch Hand
Joined: Jul 28, 2004
Posts: 44
|
|
hi all, i have checked ejb-jar.xml.It has the value for transaction type as Container.I'm unable to figure out what else is causing the problem. 1. Anyways this is the complete ejb-jar.xml <?xml version="1.0"?> <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'> <ejb-jar> <enterprise-beans> <session> <ejb-name>SessionWithCMT</ejb-name> <home>com.home.sessionbeans.cmt.SessionCMTHome</home> <remote>com.home.sessionbeans.cmt.SessionCMT</remote> <ejb-class>com.home.sessionbeans.cmt.SessionCMTBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> <resource-ref> <res-ref-name>TestPool</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>SessionWithCMT</ejb-name> <method-name>addAllData</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> 2. This is the complete weblogic-ejb-jar.xml <?xml version="1.0"?> <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>SessionWithCMT</ejb-name> <jndi-name>Session_With_CMT</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> regards, kumari
|
 |
Ugender Rekulampally
Ranch Hand
Joined: Nov 14, 2005
Posts: 130
|
|
Kumari, did you find a solution yet? if so, please post what was wrong... Thanks, Ugender
|
 |
kumari Jain
Ranch Hand
Joined: Jul 28, 2004
Posts: 44
|
|
|
Nope,i haven't got the solution as yet ugender
|
 |
Herman Schelti
Ranch Hand
Joined: Jul 17, 2006
Posts: 387
|
|
hi guys, have you tried setting autocommit to false: connection.setAutoCommit(false); Herman
|
 |
kumari Jain
Ranch Hand
Joined: Jul 28, 2004
Posts: 44
|
|
i still get the same error even after adding the following line: connection.setAutoCommit(false); regards, kumari
|
 |
Herman Schelti
Ranch Hand
Joined: Jul 17, 2006
Posts: 387
|
|
hi kumari Jain, Another idea: -do both your inserts in the addStock() methode, using only 1 connection object (with autocommit set to false). -add something silly to your code to make 100% you are using this new version, like: preparedStatement.setString(2,name+"_test_1"); Herman
|
 |
 |
|
|
subject: Problem with CMT on Stateless Session beans
|
|
|