Skip to content

Instantly share code, notes, and snippets.

@kuhnroyal
Last active December 6, 2017 09:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuhnroyal/8bfff9b1e652a7ed06a1 to your computer and use it in GitHub Desktop.
Save kuhnroyal/8bfff9b1e652a7ed06a1 to your computer and use it in GitHub Desktop.
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