Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Parameterized PersistenceContext in EJB 3.1
Parameterized PersistenceContext in EJB 3.1 [message #733664] Wed, 05 October 2011 10:57 Go to next message
Wladimir Kral is currently offline Wladimir KralFriend
Messages: 2
Registered: June 2011
Junior Member
I have an environment with a single application server and a single database for several different dealer environments. The application is for all the same and every dealer has its own database schema.

OS400 DB2 Datebase:
DEALERSCHEMA1.VEHICLETABLE
DEALERSCHEMA2.VEHICLETABLE
...

In the current solution there is a separate datasource and a separate EntityManagerFactory for each dealer preset with his schema.

Now I want to migrate the solution from Java SE to EJB 3.1.
To something like this sample below:
@Stateless
public class VehicleBean
{
    @PersistenceContext(unitName = "Dealer")
    private EntityManager     em;
	
    public Vehicle getVehicle(int dealerId, int vehicleId)
    {
        .. // ??? code to configure entity manager to use the database schema/datasource of the dealer
        return em.find(vehicleId);
    }
}


My ideas so far:


  1. Ignore dependency injection and use the approach as in Java SE. (drawback: I will not get the benefits of container managed persistence context and transactions)
  2. Unwrap ServerSession in Stateless Bean method and set current schema for the datasource. (drawback: makes the second level cache unusable since all entities from the different dealers will be mixed together)
  3. Use a SessionBroker with a separate ServerSession for each dealer. But I do not know how to use it in JPA/EJB context.


Can you help me to solve the problem?

Re: Parameterized PersistenceContext in EJB 3.1 [message #734059 is a reply to message #733664] Thu, 06 October 2011 14:48 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You could inject an @PersistenceUnit instead of the context, then you can create the Factory using your desired data source in the persistence unit properties map. The EntityManager can still be managed by JTA, it will bind automatically if create durring a transaction, otherwise you can call joinTransaction().


James : Wiki : Book : Blog : Twitter
Re: Parameterized PersistenceContext in EJB 3.1 [message #734062 is a reply to message #733664] Thu, 06 October 2011 14:48 Go to previous message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
You could inject an @PersistenceUnit instead of the context, then you can create the Factory using your desired data source in the persistence unit properties map. The EntityManager can still be managed by JTA, it will bind automatically if create durring a transaction, otherwise you can call joinTransaction().

--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Previous Topic:@EmbeddedId in conjunction with InheritanceType.JOINED does not generate PK-Field
Next Topic:DatabaseAccessor not connected and Eingabe/Ausgabe-Fehler
Goto Forum:
  


Current Time: Wed Apr 24 17:36:22 GMT 2024

Powered by FUDForum. Page generated in 0.02733 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top