2

I'm working on a persistence layer using jpa 2.1.

Multitenancy is a requirements for my application so I need to be able to change my datasource dynamically at runtime.

From what I saw in many posts I tried to use this way:

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

and the Persistence properties are set by these lines:

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);

But now I want to user che UserTransaction manager in order to handle complex transactions.

So I wrote this code:

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();

But I get this exception:

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

I'm working on WildFly9.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alex
  • 1,515
  • 2
  • 22
  • 44

0 Answers0