Multiple EntityManagers with different persistence units
pi4630 Aug 31, 2011 4:36 AMI've defined two persistence units in my persistence.xml: each one with its own data source (one points to an Oracle database on which I do reads, the other points to the HsqlDB I use to write).
Entity beans are mapped (by annotations) correctly. In my SLSB I inject two distinct EntityManagers, that refer to the relative name of the persistence units.
In my business method, I do a db read using the EntityManager pointing to the OracleDB and then I call a private method, where I instantiate a new entity which corresponds to a table on my HsqlDB. I then pass this object to myHsqlDbEMan.persist() method at the end of the private method.
Soon after the methods are terminated (and the server is committing changes to the database), I get the following error:
{noformat}ERROR [org.hibernate.util.JDBCExceptionReporter] (WorkerThread#0[127.0.0.1:48655]) Could not enlist in transaction on entering meta-aware object!; - nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: 0:ffff7f000101:126a:4e5def22:c status: ActionStatus.ABORT_ONLY >); - nested throwable: (org.jboss.resource.JBossResourceException: Could not enlist in transaction on entering meta-aware object!; - nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: 0:ffff7f000101:126a:4e5def22:c status: ActionStatus.ABORT_ONLY >))
2011-08-31 10:22:49,884 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] (WorkerThread#0[127.0.0.1:48655]) Could not synchronize database state with session: org.hibernate.exception.GenericJDBCException: Cannot open connection{noformat}
How do I handle two different persistence contexts? I've tried to refactor my SB as Stateful and extend the persistence context of both persistence units, but that didn't work (returned same error). I also tried to refactor that writeToDb() to another SessionBean and use the HsqlDB context only there and invoke the method from my other bean, but that didn't work either.
I presumed that by using the different EntityManager, the AS would know about which context the method is in.
Thanks in advance,