1

I m getting mad with Jboss WildFly9 with JPA and JTA. In my project requirements i have to implement multitenancy so i have to change dynamically datasource inside my persistence.xml.

Using jee approach this is not possibile so someone suggest me to use the classic:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("idelivery"); 
EntityManager em = emf.createEntityManager();

So till now it's working, i can create on by myself the enetitymanager and i can set jpa properties in a hashmap (included datasource).

Now i want to use JTA at least to handle transaction using transaction manager.

So these are the properties i set by code:

Properties properties = new Properties(); 
properties.put ("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
properties.put("javax.persistence.provider", "org.hibernate.jpa.HibernatePersistenceProvider"); 
properties.put("javax.persistence.transactionType", "JTA");
properties.put("javax.persistence.jtaDataSource", dataSourcePath); 

Transaction type now is JTA. So i expect that i can use some code like:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("idelivery"); 
EntityManager em = emf.createEntityManager();
MyEntity exUser= new MyEntity();
try{
Context context = new InitialContext();
UserTransaction userTransaction = (UserTransaction)context.lookup("java:comp/UserTransaction");
userTransaction.begin();
em.persist(exUser);
userTransaction.commit();

Of course this code doesn't work at all as Hibernate rises an exception:

java.lang.NullPointerException at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus()

Telling me being not able to join a transaction at the creation of entity manager moment.

So ... how can i respect my project requirements... creating my own persistence with my dynamic datasouce and at the same time using the Transaction Manager?

Alex
  • 1,515
  • 2
  • 22
  • 44

2 Answers2

1

Hibernate has its own solution for multi-tenancy. This is not part of the JPA standard, but it's compatible with and largely orthogonal to it.

It does work with managed persistence units and is compatible with JTA.

I've used the SCHEMA strategy successfully on WildFly 8.2.0.Final and 9.0.1.Final.

You just need to implement two helper classes and configure them in your persistence.xml.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
0

If you can tell before hand how many datasources you would require then you can implement some kind of contextual selection of your entity managers by using CDI producer pattern.

Define all the possible datasources in your persistence.xml and then using some kind of producer singleton factory class to inject them based on their persistence unit.

Create a producer method which selects the correct entity manager based on your current context.

Then in your ejb or cdi beans get an instance of entitymanager through CDI injection

@Inject private EntityManager em

maress
  • 3,533
  • 1
  • 19
  • 37
  • do you think is it possible to develope what you wrote using a javaSE entity manager creation? I mean Persistence.createEntityManagerFactory ("name",Properties)? this because in the properties i can insert my datasource at runtime. And most of all can i attach this entity manager to the UserTransaction JTA? – Alex Oct 05 '15 at 15:38
  • With javase, you dont need to go through such complicated scenarios, since you will have to manage the transactions manually anyway. my suggestion in this answer is if you still want the container to manage your transactions. – maress Oct 05 '15 at 21:06
  • I m looking for a feasible solution... javase allow me to dynamically set the datasource. I can build a custom entity manager object and inject it by hk2 cdi framework... the last step is to handle transaction and would be nice if i can use the jta transaction manager... no way to make this possible? – Alex Oct 05 '15 at 21:12
  • One for sure, you still have to perform bean managed transaction. So you can just use the begin, commit and rollback of the ```UserTransaction``` – maress Oct 05 '15 at 21:19