I have an existing JavaEE app using
- eclipselink 2.4 (JPA 2.0)
- glassfish 3.1
- EJB 3.0
- PostgreSQL 9.1
I want to add multi-tenancy support and i should be able to change the schema at runtime.
Basically; i will have a different schema per tenant with the same types of tables in it. this was a business decision for data isolation (SaaS - shared DB, seperate schema)
i want the application to share the same EntityManagerFactory because creating it for every DB operation is very expensive and when i am creating EntityManagers for every single DB transaction, i want to pass tenant-discriminator (schema) and JDBC_USER.
it seems that eclipselink added this support at 2.4 and i went through this example
http://wiki.eclipse.org/EclipseLink/DesignDocs/Multi-Tenancy/TablePerTenant
i have added these annotations in my Model classes
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type = TenantTableDiscriminatorType.SCHEMA,
contextProperty = "eclipselink.tenant-id")
and in my @Stateless bean, there should be a way of passing in EMF
@Inject
private EntityManagerFactory emf ;
EntityManager em = emf.createEntityManager();
so my question is :
How can i inject the shared EMF into my EJB beans ?
I am having a hard time getting this part working
// Shared EMF
EntityManager em = createEntityManager(MULTI_TENANT_PU);
em.getTransaction().begin();
em.setProperty(EntityManagerProperties.MULTITENANT_PROPERTY_DEFAULT, "gpelleti");
for this line;
EntityManager em = createEntityManager(MULTI_TENANT_PU);
- Should 'createEntityManager' not be called through EMF
- what is 'MULTI_TENANT_PU' type ?
thanks in advance