java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4094
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Using custom number of transactions in a method E-mail
User Rating: / 1
PoorBest 

With container-managed transaction beans, you are limited to a single transaction per method. You would like to group some important sections of code into a single method but use multiple transactions within the entire method. You need transaction control like this if you have a method that contains critical sections of code that cannot be broken into separate methods for CMT control. This enables you to roll back a small section of code and exit the method without executing the remaining method. This applies only to session beans, because entity beans must always use container-managed transactions.

In order to enable custom transactions it is necessary change session bean to manage its own transactions by altering its XML descriptor, like in the sample below:

...
<session>
  <ejb-name>SomeSession</ejb-name>
  <home>SomeSessionHome</home>
  <remote>SomeSession</remote>
  <ejb-class>SomeSessionBean</ejb-class>

  <session-type>Stateful</session-type>

  <!-- enable bean-managed transactions: -->
  <transaction-type>Bean</transaction-type>
</session>
...

Here is bean method:

public void someMethod() {

  UserTransaction transaction = null;

  // the first transaction:
  try {
    transaction = ejbContext.getUserTransaction();
    transaction.begin();

    // perform code...

    transaction.commit();
  catch (Exception e) {
    //need to rollback?
    if (transaction != null) {
      transaction.rollback();
    }
  }

  // the second transaction:
  try {
    transaction = ejbContext.getUserTransaction();
    transaction.begin();

    // perform code...

    transaction.commit();
  catch (Exception e) {
    //need to rollback?
    if (transaction != null) {
      transaction.rollback();
    }
  }

  // and so on ...
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.