This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Rollback Transaction in EJB2.0?

 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am developing small EJB CMP2.0 application. Now I have two tables in my database with one-to-one relation. I have Mapped one-to-one relatonship between my EJB's corresponding to those tables using EJB CMR. My contants of ejb-jar.xml & jbosscmp-jdbc.xml for same are as follows:-

ejb-jar.xml
=============================
<relationships >
<ejb-relation >
<ejb-relation-name>empmaster-to-usrmaster</ejb-relation-name>
<ejb-relationship-role> <ejb-relationship-role-name>empmaster-has-one-usrmaster</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<cascade-delete/>
<relationship-role-source >
<ejb-name>EmployeeMaster</ejb-name>
</relationship-role-source>
<cmr-field >
<cmr-field-name>userMaster</cmr-field-name>
</cmr-field>
</ejb-relationship-role>

<ejb-relationship-role >
<ejb-relationship-role-name>usrmaster-has-one-empmaster</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<cascade-delete/>
<relationship-role-source >
<ejb-name>UserMaster</ejb-name>
</relationship-role-source>
<cmr-field >
<cmr-field-name>employee</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
</relationships>
=============================

jbosscmp-jdbc.xml
=============================
<relationships>
<ejb-relation>
<ejb-relation-name>empmaster-to-usrmaster</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>empmaster-has-one-usrmaster</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>employeebeanid</field-name>
<column-name>userid</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>usrmaster-has-one-empmaster</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>userbeanid</field-name>
<column-name>employeeid</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
</ejb-relation>
</relationships>
=============================

All the things working fine.
Now Problem is that how to perform rollback if some problem will occur in inserting record in second bean after record is inserted in first bean. Currently I am using session bean for calling 'create' method on both the beans in realationship. So how to perform rollback in case of failure??

Please tell me.

Thanx
Prash
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your session bean, start a transaction (container managed is usually best) for the method which calls your entity beans. This transaction must be propagated into the entity beans, so these entity bean methods must have a trans attribute such as Required. If there is a failure which causes a RuntimeException to be thrown in either entity bean, the container will automatically rollback the transaction before the session bean method ends.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic