I'm currently working with Java 17, Wildfly 25.0.1 and JPA over Hibernate 5.3.

I have an application that uses annotations and entities mapped to access a single database. I'm using a PersistenceUnit with a Datasource, and it works fine.

What I'm trying to achieve is programatically choosing different datasources, with the same PersistenceUnit, to access different copies of the same database for different customers. The number of customers can grow, so I cannot use multiple PU's.

So far, I've worked with an injected EntityManager such as this:

@PersistenceContext(unitName = PERSISTENCE_UNIT)
protected EntityManager     em;

I went in the direction of creating my own instances of EntityManager, so I tried injecting the EntityManagerFactory instead of the EntityManager itself:

@PersistenceUnit(unitName = PERSISTENCE_UNIT)
EntityManagerFactory        emf;

The originally injected EntityManager, when inspected, belongs to the class org.jboss.as.jpa.container.TransactionScopedEntityManager. However, when I create an EntityManager using the injected EntityManagerFactory, I receive an org.hibernate.internal.SessionImpl instead.

The point is that, though many things work fine, the behaviour in the application is somewhat different. It's a very big application with a lot of transactions, and sometimes data is not correct, etc.

Is there any way to create or ask the server for a TransactionScopedEntityManager? I've searched through Wildfly source code, but I can't find it.

Also, any other approach to achieve this goal would be appreciated.

Thanks and kind regards.

1

There are 1 best solutions below

0
On BEST ANSWER

You could achieve multitenancy by using Hibernate's Mulit tenant facilities by implementing a MultitenantConnectionProvider and a CurrentTenantIdentifierResolver that would need to have access to your Session / Transaction Context. Here's a tutorial on Baeldung.

Disclaimer: I'm not familiar with Wildfly, so maybe there's a native way to do it, similar to Springs RoutingDataSource (refer to XtremerBaumers comment in your question)