Problem with Threads and Transactional context
zerok Jul 16, 2007 5:34 PMHi guys!
I've done the same post on Seam's, because I thought that would be a good place to put the question together with others posts with similar problem. As it is indeed an EJB3 related problem, I make the same post here too. So please don't be mad with me because this problem is taking away my sleep and may cost my job.
Here is the post:
---------------------
I'm using threads inside a stateless bean and I cannot persist anything without having that annoying message that states I can only use transaction manager inside a transactional context.
I've tried to make a busy-wait on my stateless bean so I can assure my bean will be alive until the very moment my thread needs the entityManager but when the execution reaches the entityManeger.merge( object ) an exception is risen.
The story:
I have a jboss-4.0.5.GA with an upgrade on my original Javassist. Now I have javassist-3.4 due to issues with threads.
My application needs to perform a fork in execution flow split in three parts. Both of them are created as threads and are observed in normal-fashion-way observer pattern, where the Stateless Bean is the observer.
I have a hunch that my bean life-cycle ends before my first thread finishes, so the entityManager looses it transactional context.
Can anybody point a blueprint for creating a structure to support such a business demand?
Here some parts of my code so you can place yourself. This code comes with only one thread. The original one has three, all like this one.
@TransactionManagement(TransactionManagementType.CONTAINER)
public @Stateless(name = "TempoRealRN") class TempoRealRNBean extends IntegratorCommonBehavior implements TempoRealRN, Observer {
@EJB(beanName = "GenericoDAO")
GenericoDAO genericoDAO;
@PersistenceContext private EntityManager entityManager;
private static final Log log = LogFactory.getLog(TempoRealRNBean.class);
...
public void processar(CommonController controladorComum) throws IntegratorException {
super.setEntityManager(this.entityManager);
tCorFrente = new ThreadCorFrente(controladorComum, this, log, dto, nomeThreadFrente, grupoFrente);
tCorFrente.addObserver(this);
tCorFrente.start();
}// ends of: public void processar(...) ....
/*
* receives notification from thread state change and executes the proper action to acomplish bussiness rule goals.
*
*/
public void update(Object o) {
//perform some state tests
//then try to persist.
//at this point an exception is risen
this.entityManager.merge( object );
}//ends public void update(Object o)
}//ends class
The exception risen is:
--------------------------
17:01:47,480 ERROR [STDERR] Caused by: javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
17:01:47,480 ERROR [STDERR] at org.jboss.ejb3.entity.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:150)
17:01:47,480 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManager.merge(TransactionScopedEntityManager.java:180)
17:01:47,480 ERROR [STDERR] at wm.integrator.negocio.temporeal.impl.TempoRealRNBean.persistirFrente(TempoRealRNBean.java:485)
It would be great if some good soul could enlight me regarding on how to use the proper pattern for observing threads and how I can make this persistence work.
Thank you all just for being patient enough to read this extensive post.
I apologize myself for any English grammar misuse or misspell as English is not my 1st language. I'm from Brazil.