Hello.
I'm facing a problem implementing a Multi Tenant application with Hibernate 4.1.7 and EJB using GlassFish 3.1.2.
There's currently a class that implements org.hibernate.context.spi.CurrentTenantIdentifierResolver which identifies the logged in tenant fetching the logged in user with this code:
Code:
@Override
public String resolveCurrentTenantIdentifier() {
try {
SessionContext context = ServiceLocator.locate(SessionEjbContext.class).getSessionContext();
String userName = context.getCallerPrincipal().getName();
AppLogger.info("current tenant identifier [" + userName + "]");
return userName;
} catch (Throwable e) {
AppLogger.info("erro lookup");
}
return "default";
}
The SessionContext.class is implemented as such
Code:
@Stateful
@SessionScoped
public class SessionEjbContext {
@Resource
private SessionContext sessionContext;
public SessionContext getSessionContext() {
return sessionContext;
}
}
The id is then provided to the class that extends org.hibernate.service.jdbc.connections.spi.AbstractMultiTenantConnectionProvider which implements the method ConnectionProvider selectConnectionProvider(String tenantIdentifier) and must return a c3p0 ConnectionProvider object to use.
The real problem is this:
When the user user1 autheticates the system, the db connection is resolved ok.
But when the user2 authenticates, the method ConnectionProvider selectConnectionProvider(String tenantIdentifier) receives as parameter user1, and not user2, leading us to select the same database as user1.
The EntityManager is implemented with the PersistenceContextType.EXTENDED annotation, is that causing the problem?
Any help is appreciated. If you need more details to help me out here, let me know.