Meaningless Drivel is fun!*
The moose likes EJB and other Java EE Technologies and the fly likes Stateless EJB and transactions..pls help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Stateless EJB and transactions..pls help" Watch "Stateless EJB and transactions..pls help" New topic
Author

Stateless EJB and transactions..pls help

tanner
Greenhorn

Joined: Oct 30, 2001
Posts: 5
Simple Question I have a standalone client calling a stateless session bean with container managed transaction. In the bean I have 2 database inserts in a method to SQL 2000 database. The second insert is for a table that does not exists so as per my understanding the transaction should be rolled back and neither of the updates should take place. What I am finding is the first insert takes place even though the second insert is throwing an error!! PLS HELP!!! Below is my EJB code. public class TranTestEJB implements SessionBean { SessionContext ctx; public void ejbActivate() {} public void ejbPassivate() {} public void ejbCreate() {} public void ejbRemove() {} public void setSessionContext(SessionContext ctx) { this.ctx = ctx; } public void runTest() { String url = "jdbc dbc:Queue"; String user = "sa"; String password = ""; String sql; String sql2; Connection connection; Statement statement; try{ //ctx.getUserTransaction().begin(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection = DriverManager.getConnection(url,user,password); statement = connection.createStatement(); sql = "Insert into Description values ('HA')"; statement.executeUpdate(sql); sql2 = "Insert into AAAXXX values ('HB')"; statement.executeUpdate(sql2); ().commit(); } catch (Exception e) { throw new EJBException(e.getMessage); } finally { sql = null; sql2 = null; statement = null; connection = null; } } } My deployment descriptor has the following code <ejb-jar> <enterprise-beans> <session> <ejb-name>TranTest</ejb-name> <home>test.TranTestHome</home> <remote>test.TranTest</remote> <ejb-class>test.TranTestEJB</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>TranTest</ejb-name> <method-name>*</method-name> </method> <trans-attribute>RequiresNew</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> PLS Help.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Well, first of all you're not telling us which EJB server you're using, but I can make a pretty good guess that should apply for most servers.
It's happening because you're obtaining the connection from the DriverManager, and not from a DataSource. For the EJB server to manage transactions the Transaction manager must be aware of the resource managers (e.g. connections) so it can tell them how to commit/rollback the transactions.
The only way it can do so is if you obtain your JDBC connections from the DataSource implemention provided by your vendor. I suggest you take a look at some of the example code in Richard Monson-Haefel's book.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
tanner
Greenhorn

Joined: Oct 30, 2001
Posts: 5
Thanks Kyle, really appreciate your reply. I was working on weblogic server. Will try the datasource method and get back if I run into anu problem.
Thanks
tanner
Greenhorn

Joined: Oct 30, 2001
Posts: 5
I created a datasource in weblogic server and used this datasource in code. I am able to rollback transactions by calling ctx.setRollbackOnly() in the catch block in the code.
The catch block code is as follows:
catch (Exception e)
{
ctx.setRollbackOnly();
throw new EJBException(e.getMessage());
}
Is this standard behaviour to call ctx.setRollbackOnly() in the catch block? Will creating the EJBException not rollback the transaction?
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
I believe so, yes. I know that in WebSphere that a SQL Exception will automatically mark the transaction as being rolled back, but that for all other exceptions you have to mark it yourself. I'd have to read the spec to see if it's part of the standard (I suppose it is) but it certainly sounds right...
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateless EJB and transactions..pls help