Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Changing schema of ClassDescriptor in Container(how to reuse class and set table qualifier repeatedly with a different value)
Changing schema of ClassDescriptor in Container [message #1081718] Wed, 07 August 2013 15:46 Go to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 16
Registered: September 2011
Junior Member
I have a @PersistenceUnit/EntityManagerFactory in my Stateless EJB. I take that reference and create an EntityManger from it, where I go into the activeSession and call setTableQualifier (the db schema name) on certain class descriptors. I then ensure the EM is closed when I am done with it. I am asking, "is there a better way to do this". Am I going to incur wierdness when two user threads initialize an EM and change class descriptors at the same time?

Here is my code


public static EntityManager getEntityManager(EntityManagerFactory emf, String abpID)
{
abpID = abpID.toUpperCase();

EntityManager em = emf.createEntityManager();
Session session = ((JpaEntityManager) em.getDelegate()).getActiveSession();
Session activeSession = session.getActiveSession();
List<Class> tapClasses = Arrays.asList(TAP_CLASSES);
for (ClassDescriptor descriptor : activeSession.getDescriptors().values())
{
if (tapClasses.contains(descriptor.getJavaClass()))
{
for (DatabaseTable table : descriptor.getTables())
{
table.setTableQualifier(abpID);
}
}
}

return em;
}

ANY help is greatly appreciated.
Re: Changing schema of ClassDescriptor in Container [message #1082389 is a reply to message #1081718] Thu, 08 August 2013 14:02 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

All EntityManagers from the same EntityManagerFactory will share the same tableQualifier and descriptors, so this is not a good idea.

You should create a new EntityManagerFactory per schema. Unfortunately there is currently no persistence unit property for setting the schema (please log a bug for this), so you will need to set the tableQualifier using a SessionCustomizer. Normally multiple EntityManagerFactorys for the same persistence unit will share the same Session, so you will also need to set the persistence unit property "eclipselink.session-name" to be unique for each schema.

Another option is to have a different user for each schema that defaults to that schema, then just acquire the EntityManagerFactory with the different user.

You may also want to look into EclipseLink's multi tenant support,
http://www.eclipse.org/eclipselink/documentation/2.5/solutions/multitenancy003.htm#A1235913


James : Wiki : Book : Blog : Twitter
Re: Changing schema of ClassDescriptor in Container [message #1085281 is a reply to message #1082389] Mon, 12 August 2013 17:55 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 16
Registered: September 2011
Junior Member
First, thanks for the help.
But, I have some "severe" requirements, which are getting really ugly to handle in Eclipselink that should be relatively simple, I am just having loads of fun finding the "right" way to do this.

First, I am in a container using Stateless EJBs where the PersistenceContext/Unit is injected. I have to maintain transaction consistency with the container. Second, the schema (table qualifier) is not set on all entities, and I have to be able to set it "uniquely" for each user accessing the application.

What I tried to do with your code, is to set a ThreadLocal with the schema ID in the servlet, then connect to the Session bean and see the SchemaCustomizer fire and let me set the schema for THAT session. But as far as I can tell, the customizer is only firing once for eclipselink logging into the database. It never fires again until I reload the application! I am looking into the "Dynamic" classes, but I am limited to JPA1 in this container (WebLogic 10.3) so I have to be very careful that I don't grab JPA2 capabilities.

If anyone can provide some help, again, GREATLY APPRECIATED.
Thanks
Re: Changing schema of ClassDescriptor in Container [message #1085885 is a reply to message #1085281] Tue, 13 August 2013 13:50 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You cannot use an injected persistence unit if you need to change the schema, you will need to manage your own persistence unit (you can still use JTA).

You must set a unique "eclipselink.session-name" per schema to avoid getting the same persistence context.


James : Wiki : Book : Blog : Twitter
Re: Changing schema of ClassDescriptor in Container [message #1086012 is a reply to message #1085885] Tue, 13 August 2013 17:28 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 16
Registered: September 2011
Junior Member
I am doing that already. What I am looking for is the complete pattern to follow to achieve my overall needs. Do I have to manage my entire persistence unit (I only have one because everything is on one database) or just have a map of EntityManagerFactory? I current have created my own extension of PersistenceProvider, where I maintain a Map of EMFs by schema. Things are mostly working, but I get a LOT of ClassCastExceptions for the same class.

IAW, I am looking for a REALLY GOOD EXAMPLE to follow, maybe even some decent pseudo-code would be great. I appreciate all the snippets of do's and don't, but I need something more complete that all works together.
Re: Changing schema of ClassDescriptor in Container [message #1086037 is a reply to message #1086012] Tue, 13 August 2013 18:19 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 16
Registered: September 2011
Junior Member
I would like to jump back to something you said in your first response. I just verified that I CAN connect as the schema owner. This means that if I can manage my own PU, I would think that I could have a cache for each PU. I think I would still have to managed EMF's per PU, but I would not have to customize the session(descriptors). So I am "still" a little in the mode of "how" to do this correctly in a container, so that I am JTA consistent.

So I greedily await your responses.
Re: Changing schema of ClassDescriptor in Container [message #1087413 is a reply to message #1086037] Thu, 15 August 2013 15:55 Go to previous message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 16
Registered: September 2011
Junior Member
Just a followup. I found some code online that was very close to what I needed. I created my own subclass of PersistenceProvider, and am now succesfully creating new sessions for each schema.

Thanks
Previous Topic:Changing Tablename changes on-the-fly?
Next Topic:Need advice on setup Phone ManyToOne JoinColumns Employee Relationship
Goto Forum:
  


Current Time: Fri Apr 26 13:46:29 GMT 2024

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

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

Back to the top