File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Transaction handling in stateless session bean. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Transaction handling in stateless session bean." Watch "Transaction handling in stateless session bean." New topic
Author

Transaction handling in stateless session bean.

Pankaj R.
Greenhorn

Joined: Sep 10, 2003
Posts: 2
Hi Friends
I am new to EJB . I need help for handling transaction in stateless session bean, I am explaning the situation.
I have 5 Containar managed EB's and from stateless session bean I am calling create method of these EB's. Now suppose in two tables records gets successfully inserted and say there is some problem while inserting record in 3rd table through 3rd EB, so this whole transaction should get rollback.
The <transaction-type> for session bean and all EB's is in ejb-jar.xml as follows:
<transaction-type>Container</transaction-type>
and <trans-attribute> for session bean and all EB's is :
<trans-attribute>Required</trans-attribute>
And I am using UserTransaction interface for handling transaction but the problem I am facing is if record inserted successfully in first two tables from respective EB and say prob in inserting record from 3rd EB so I am calling userTransaction's rollback method but its not rolling back. The data inserted in first two tables is already commited, it rollback only 3rd table data. It should not happen, My requirement is if the record is inserted in all 5 tables successfully then only transaction should get commited otherwise rollback.
Can you please guide me about this situation, how to handle transaction?
Thankx in advance.
Regards
Pankaj Raverkar
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Nuc Soft,
Thanks for joining JavaRanch, but could you take a quick look at the naming policy and edit your profile accordingly.
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
And I am using UserTransaction interface for handling transaction but the problem I am facing is if record inserted successfully in first two tables from respective EB and say prob in inserting record from 3rd EB so I am calling userTransaction's rollback method but its not rolling back. The data inserted in first two tables is already commited, it rollback only 3rd table data. It should not happen, My requirement is if the record is inserted in all 5 tables successfully then only transaction should get commited otherwise rollback.
First off, if you are using CMT (Container Managed Transactions) then you should not be directly accessing the UserTransaction. All of your transaction control will be handled using the EJBContext. For example, to rollback the current transaction you would call EJBContext.setRollbackOnly().
Secondly, check your Application Server documentation to make sure you have everything set up properly for transactioning. For example, in WebLogic the number one mistake people make with regards to transactioning is to use a standard Datasource instead of a TxDatasource. WebLogic doesn't complain, it just doesn't rollback the database modifications...
[ September 10, 2003: Message edited by: Chris Mathews ]
Pankaj R.
Greenhorn

Joined: Sep 10, 2003
Posts: 2
Originally posted by Chris Mathews:
And I am using UserTransaction interface for handling transaction but the problem I am facing is if record inserted successfully in first two tables from respective EB and say prob in inserting record from 3rd EB so I am calling userTransaction's rollback method but its not rolling back. The data inserted in first two tables is already commited, it rollback only 3rd table data. It should not happen, My requirement is if the record is inserted in all 5 tables successfully then only transaction should get commited otherwise rollback.
First off, if you are using CMT (Container Managed Transactions) then you should not be directly accessing the UserTransaction. All of your transaction control will be handled using the EJBContext. For example, to rollback the current transaction you would call EJBContext.setRollbackOnly().
Secondly, check your Application Server documentation to make sure you have everything set up properly for transactioning. For example, in WebLogic the number one mistake people make with regards to transactioning is to use a standard Datasource instead of a TxDatasource. WebLogic doesn't complain, it just doesn't rollback the database modifications...
[ September 10, 2003: Message edited by: Chris Mathews ]

Sorry for not mentioning the server I am using, I am using WebLogic 6.1 application server. And I am using standard DataSource, so if I change it to TxDataSource then what else I need to setup for handling transaction. Can you please tell me in detail.
Thankx
Regards
Pankaj Raverkar
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Just go with a TxDatasoure and keep your grubby hands off the UserTransaction unless you are using BMT and you should be fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transaction handling in stateless session bean.