Jump to: navigation, search

Difference between revisions of "Talk:EclipseLink/Examples/JPA/Multitenant"

(New page: I'd love to see an example of how this would look end-to-end with a container managed environment - i.e., how would you incorporate multitenancy into a managed bean (JSF, EJB etc.) current...)
 
(posted example)
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
  
 
I suppose you could inject a @PersistenceUnit instead, but then how would you manage the EntityManager lifecycle?  @PersistenceContext does it all for you.  If you do something like call EntityManagerFactory.createEntityManager(map) in a @PostConstruct method, when do you close it?  How does it participate in transactions? etc.
 
I suppose you could inject a @PersistenceUnit instead, but then how would you manage the EntityManager lifecycle?  @PersistenceContext does it all for you.  If you do something like call EntityManagerFactory.createEntityManager(map) in a @PostConstruct method, when do you close it?  How does it participate in transactions? etc.
 +
 +
:If you set the tenant in the persistence.xml (persistence unit) then there is nothing required.  If you are using a shared EntityManagerFactory for all tenants, then you need to set the tenant per transaction.  This could be done using setProperty() on the EntityManager as the first operation of the transaction.  It could also be done using Session events.  I agree a Java EE example would be useful.
 +
::[[User:James.sutherland.oracle.com|James.sutherland.oracle.com]] 12:44, 4 October 2011 (UTC)
 +
 +
:I tried setting the tenant with session.setProperty() in the "postAcquireClientSession" callback - is that the right way to do it?
 +
::--[[User:Wrschneider.gmail.com|Wrschneider.gmail.com]] 19:46, 13 October 2011 (UTC)
 +
 +
James - can you please look at this example to see if this is the right approach?  If so, I could add it to the wiki page.
 +
<source lang="java">
 +
public class TenantListener extends SessionEventAdapter implements SessionEventListener {
 +
public static final String TENANT_PROPERTY = "eclipselink.tenant-id";
 +
 +
@Override
 +
public void postAcquireClientSession(SessionEvent event) {
 +
Session session = event.getSession();
 +
session.setProperty(TENANT_PROPERTY, **value**);
 +
}
 +
</source>
 +
 +
Register this listener in persistence.xml with the property "eclipselink.session-event-listener" and inject the @PersistenceContext as usual.
 +
 +
Does that sound right to you? Thanks!
 +
:--[[User:Wrschneider.gmail.com|Wrschneider.gmail.com]] 02:08, 22 December 2011 (UTC)

Latest revision as of 21:08, 21 December 2011

I'd love to see an example of how this would look end-to-end with a container managed environment - i.e., how would you incorporate multitenancy into a managed bean (JSF, EJB etc.) currently using @PersistenceContext?

I suppose you could inject a @PersistenceUnit instead, but then how would you manage the EntityManager lifecycle? @PersistenceContext does it all for you. If you do something like call EntityManagerFactory.createEntityManager(map) in a @PostConstruct method, when do you close it? How does it participate in transactions? etc.

If you set the tenant in the persistence.xml (persistence unit) then there is nothing required. If you are using a shared EntityManagerFactory for all tenants, then you need to set the tenant per transaction. This could be done using setProperty() on the EntityManager as the first operation of the transaction. It could also be done using Session events. I agree a Java EE example would be useful.
James.sutherland.oracle.com 12:44, 4 October 2011 (UTC)
I tried setting the tenant with session.setProperty() in the "postAcquireClientSession" callback - is that the right way to do it?
--Wrschneider.gmail.com 19:46, 13 October 2011 (UTC)

James - can you please look at this example to see if this is the right approach? If so, I could add it to the wiki page.

public class TenantListener extends SessionEventAdapter implements SessionEventListener {
public static final String TENANT_PROPERTY = "eclipselink.tenant-id";
 
@Override
public void postAcquireClientSession(SessionEvent event) {
	Session session = event.getSession();
	session.setProperty(TENANT_PROPERTY, **value**);
}

Register this listener in persistence.xml with the property "eclipselink.session-event-listener" and inject the @PersistenceContext as usual.

Does that sound right to you? Thanks!

--Wrschneider.gmail.com 02:08, 22 December 2011 (UTC)