Hi all,
I am trying to imitate simultaneous access to data, using EJB 3.0 under JBoss 4.0.5GA.
Stateless session bean has following method:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public String fieldInc(int id) {
EntityManager em = null;
String result;
try {
InitialContext ctx = new InitialContext();
EntityManagerFactory emf = (EntityManagerFactory) ctx.lookup("java:/EntityManagerFactory");
em = emf.createEntityManager();
Foo f = em.find(Foo.class, id); // Foo is Entity bean, it represents table in DB under MySQL 5.0.24a
if(f == null) {
em.close();
return "No entity found by given id("+id+")";
}
f.setSomefield(f.getSomefield()+1);
em.persist(f);
result = "new field value: "+f.getSomefield();
em.close();
return result;
} catch(Exception e) {
if(em != null) {
em.close();
}
return e.toString();
}
}
for(int i=0;i<100;i++){
sess.fieldInc(1);
}
okay,
for(int i=0;i<100;i++){
synchronized(SimultanServlet.class){
sess.fieldInc(1);
}
}