I am using glassfish server with multiple jdbc connection pools. I want to have EntityManagerFactories for every jdbc connection.
Using the code
Map props = new HashMap();
props.put(PersistenceUnitProperties.JTA_DATASOURCE, <my jdbc datasource>);
Persistence.createEntityManagerFactory(<persistence unit name>, props);
Works fine but I cannot combine it with the UserTransation to have transaction begin and commit, rollback. Furthermore entityManager.getTrasnaction().begin, commit, rollback don’t work (I get records persisted in the database when an error occurs).
Things work great when I use UserTrasnaction and EntityManager injected with
@Resource
UserTransaction utx1;
@PersistenceContext(unitName = <persistence unit name>)
private EntityManager em1;
With the injection the utx1.begin(), utx1.commit() controls the entityManager perfectly! But the problem is that I cannot use my multiple jdbc connection pools that i have set up on my server.
So my question is: Can I do programmatically what this injection does?
@Resource
UserTransaction utx1;
@PersistenceContext(unitName = <persistence unit name>)
private EntityManager em1;
Thanx!
I have tried this, but it doesnt work
@Resource
UserTransaction utx1;
@PersistenceContext(
properties = {@PersistenceProperty(name=PersistenceUnitProperties.JTA_DATASOURCE, value="ORCLH_MARMA")},
unitName = "MINLO")
private EntityManager em1;
You should use CDI @Qualifiers and EntityProducers to do that, and use the persistence.xml file to define your multiple persistence-units (with the same entities), provided they are prepared to do that.
First things first: define as many datasource qualifiers as needed
and a second one
Then create a EntityManager producer using proper datasources:
In your persistence.xml, define both persistence units for the same entities, but with different names and data-sources:
Then, in your EJBs or CDI beans, inject the desired entity manager to perform your ops:
Be aware that
@Transactional
operations may not propagate instantly betweenEntityManager
s if they share the same database, except if you tune caches properly.I don't know why you need to use different users for each datasource, so I suggest you check if you aren't incurring in a xyproblem.