Dealing with synchronization

At this point you might wonder how does the container deals with synchronization ? that is, what happens if two client invoke the Singleton cuncurrently ? If you deploy the EJB as in the example above, each method of the Bean will be synchronized, that is, if there's a thread executing one method the other thread will wait for the first thread to release the lock.

That's much the same way as if you add mark a method as synchronized in standard Java SE.

You can however apply fine tuning locking strategies so that some methods are allowed to be executed cuncurrently and some not: see the modified example:
public class SingletonBean implements SingletonItf {
    int total;

    @PostConstruct
    private void startup() {
        System.out.println("Singleton inited!");
    }

    @Lock(LockType.WRITE)
    public void add(int i) {
        
        total += i;

    }

    @Lock(LockType.READ)
    public int getTotal() {
    
        return total;
    }
}
0
0
0
s2smodern

Related articles available on mastertheboss.com

EJB 3.0 tutorial : Session Beans

Enterprise JavaBeans (EJB) technology is a J2EE technology for de

JBoss MDB 3.0

Message-driven beans (MDBs) are stateless, server-side, transacti

JBoss EJB 3 BMT

In a Bean Managed Transaction, the code in the EJB or Message Dri

How do you keep your EJB callbacks separated from biz. methods?

JBoss daily recipe

How to set EJB timeout period ?

JBoss recipe of the day

How to add a Selector with MDB 3.0 ?

Message selectors allow an MDB to be more selective about the mes