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: 3897
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:
 
 
 
How to synchronize Session Bean state with the transactions E-mail
User Rating: / 5
PoorBest 

A session Bean class can implement Session Synchronization interface only if bean needs to synchronize its state with the transactions. It has three methods.

afterBegin(): The afterBegin method notifies a session Bean instance that a new transaction has started, and that the subsequent business methods on the instance will be invoked in the context of the transaction.

afterCompletion(boolean committed): The afterCompletion method notifies a session Bean instance that a transaction commit protocol has completed, and tells the instance whether the transaction has been committed or rolled back.

beforeCompletion(): The beforeCompletion method notifies a session Bean instance that a transaction is about to be committed.

The following example will be more useful:

import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EJBObject;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.SessionSynchronization;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class synBean implements SessionBean, SessionSynchronization {
    
    protected int count = 0;        //  state of the bean
    protected int newcount = 0;   // value inside Transaction, not committed.
    protected String clientUser = null;
    protected SessionContext sessionContext = null;
    
    public void  ejbCreate(String userid) {
        count = 0;
        newcount = count;
        clientUser = userid;
    }
    
    public void ejbActivate() {
    }
    
    public void ejbPassivate() {
    }
    
    public void ejbRemove() {
    }
    
    public void setSessionContext(SessionContext sessionContext) {
        this.sessionContext = sessionContext;
    }
    
    public void afterBegin() {
        newcount = count;
    }
    
    public void beforeCompletion() {
        // We can access the bean environment everywhere in the bean,
        // for example here!
        try {
            InitialContext ictx = new InitialContext();
            String value = (Stringictx.lookup("java:comp/env/property1");
            // value should be the one defined in ejb-jar.xml
        catch (NamingException e) {
            throw new EJBException(e);
        }
    }
    
    public void afterCompletion(boolean committed) {
        if (committed) {
            count = newcount;
        else {
            newcount = count;
        }
    }
    
    public void write(int i) {
        newcount = newcount + i;
        return;
    }
    
    public int read() {
        return newcount;
    }
}

 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.