EJB 3 transactions and synchonization
Using EJB 3, two threads will try to run this code (that is inside a
stateless session bean). Both threads are able to pass through the if
statement and print out the statement.
userTransaction.begin(); //userTransaction is of class UserTransaction
myEntity = entityManager.find(MyEntity.class, id); //entityManager is of
class EntityManager
if (myEntity.getStatus() != "DONE") {
myEntity.setStatus("DONE");
entityManager.merge(myEntity);
System.out.println("Only one thread should be running this");
}
userTransaction.commit();
I've tried setting the transaction isolation level to serializable with no
success:
org.hibernate.Session session = (org.hibernate.Session)
entityManager.getDelegate();
session.connection().setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
Maybe I'm using the wrong approach.
No comments:
Post a Comment