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?