Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
public class MultiTenantJpaTransactionManager extends JpaTransactionManager {
@Inject
private CurrentTenantResolver tenantResolver;
@Override
protected void doBegin(final Object transaction, final TransactionDefinition definition) {
super.doBegin(transaction, definition);
final EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(getEntityManagerFactory());
final EntityManager em = emHolder.getEntityManager();
final Serializable tenantId = tenantResolver.getCurrentTenantId();
if (tenantId != null) {
em.setProperty("eclipselink.tenant-id", tenantId);
} else {
// some error handling here
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment