• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction handling in stateless session bean.

 
Pankaj R.
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just go with a TxDatasoure and keep your grubby hands off the UserTransaction unless you are using BMT and you should be fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic