This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes How to Rollback Transaction in EJB2.0? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to Rollback Transaction in EJB2.0?" Watch "How to Rollback Transaction in EJB2.0?" New topic
Author

How to Rollback Transaction in EJB2.0?

Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186
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


********Deserve Before You Desire********
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Rollback Transaction in EJB2.0?
 
Similar Threads
cascade-delete doesn't work
one-to-one relationship
relationships using composite keys
is there anything wrong here ?
CMR configuration confusion