Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] eclipselink 2.4 TABLE_PER_TENANT shared EMF

Hi Guy,

so from my understanding; even in your second example, the EMF is gonna be shared and i will be connecting to the DB with different users by passing the properties to the EM.

i will try both and update the post with the results

thanks a lot,
Justin

On 8/21/2012 1:24 PM, Guy Pelletier wrote:
Hi Justin,

The JDBC_USER and JDBC_PASSWORD properties are not dynamic properties and will have no effect on an injected EM (you should see a log warning)

You will be already logged in at this point and your example:

@PersistenceContext(name="multi-tenant-pu")
protected EntityManager em ;
schema = dynamically getting this ;
em.getTransaction().begin();
em.setProperty(EntityManagerProperties.MULTITENANT_PROPERTY_DEFAULT, "schema");

Will work if the database is provisioned to have access to schema1.report and schema2.report etc. with the persistence unit login credential (specified in your persistence.xml)

Otherwise you will have to use the following architecture

@PersistenceUnit(name="multi-tenant-pu")
protected EntityManagerFactory emf ;
user = dynamically getting this ;
password = dynamically getting this ;
schema = dynamically getting this ;

em = emf.createEntityManager(properties)

where properties will contain the dynamic properties (user, password, schema etc.).

Cheers,
Guy


On 21/08/2012 3:21 PM, Justin Joe wrote:
Hi,

I am trying to make a container-managed app an application-managed app because of the multi-tenancy requirement.
Before i was being able to inject the EM because the app was being deployed to every customer's machine separately  and i was using JTA connection pools through sun-glassfish.

but now i will have just one app instance and all the tenants will be using it by logging in with their own credentials.

so my question is :
Can i keep using container-managed transactions via TENANT_PER_TABLE solution and have connection-pools ?
i thought i can't and used
transaction-type = "RESOURCE_LOCAL" instead

my DB is like this: (tables in different schemas are the same)

DB1:
  schema1 (tenant1)
      report
      ........
  schema2 (tenant2)
      report
      ........
 

if i can use this in my @Stateless EJB Bean classes, this would be great :
@PersistenceContext(name="multi-tenant-pu")
protected EntityManager em ;

schema = dynamically getting this ;

em.getTransaction().begin();
em.setProperty(EntityManagerProperties.MULTITENANT_PROPERTY_DEFAULT, "schema");
em.setProperty(EntityManagerProperties.JDBC_USER, "username");
em.setProperty(EntityManagerProperties.JDBC_PASSWORD, "password");

em.createNativeQuery
.....

i will try this and post the results anyways but do you think i am missing something ?

thanks in advance

On 8/21/2012 11:00 AM, Guy Pelletier wrote:
Hi Justin,

If you are injecting the EM why do you need a reference back to the EMF? You should just set the multitenant property on the EM? EMF's are shared by default.

With a non-shared EMF, you will need to ensure the following property is set to false in your persistence unit definition:

eclipselink.multitenant.tenants-share-emf

And you will also have to provide a unique session name though the following property:

eclipselink.session-name

Cheers,
Guy


On 21/08/2012 1:35 PM, Justin Joe wrote:
Hi Guy,

thanks for the answer.

the problem is actually injecting the PU. Because when I was using container-managed transactions and leaving the management to the app-server, i was going like this
@PersistenceContext
protected EntityManager em ;

EMF was being taken care of by the app-server but now i have to create or reference it somehow and i dont know how ?

and the second thing is that i tried to use non-shared EMF example too and set 'eclipse-tenant-id' to the schema that i am getting dynamically
when i debugged it, it seems like it is working but it is actually querying from the default schema 'public' even though i specify a different schema which the DB user has permission on.
so i can't get it working to query from different schemas basically.

any help at this point would be much appreciated

cheers,
Justin

On 8/21/2012 5:19 AM, Guy Pelletier wrote:
Hi Justin,

To answer your questions

1 - Yes, the EM should be created through the EMF. The sample code you are seeing is from our testing framework and the createEntityManager method is simply a wrapper to an emf.createEntityManager() call.

2 - MULTI_TENANT_PU is simply the name of the persistence unit since we have many different persistence units in our testing suites. If you are injecting the PU you don't need to worry about this.

Cheers,
Guy


On 21/08/2012 4:16 AM, Justin Joe wrote:
hi,

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);
  1. Should 'createEntityManager' not be called through EMF
  2. what is 'MULTI_TENANT_PU' type ?

thanks in advance




_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

--

Oracle
Guy Pelletier

ORACLE Canada, 45 O'Connor Street Suite 400 Ottawa, Ontario Canada K1P 1A4

Green Oracle Oracle is committed to developing practices and products that help protect the environment



_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


--

Oracle
Guy Pelletier

ORACLE Canada, 45 O'Connor Street Suite 400 Ottawa, Ontario Canada K1P 1A4

Green Oracle Oracle is committed to developing practices and products that help protect the environment



--

Oracle
Guy Pelletier

ORACLE Canada, 45 O'Connor Street Suite 400 Ottawa, Ontario Canada K1P 1A4

Green
              Oracle Oracle is committed to developing practices and products that help protect the environment



Back to the top