4 Replies Latest reply on Nov 12, 2007 7:52 AM by pmuir

    multiple data sources

    gstacey

      A new project I am working on requires multiple data sources and switching between them programmatically. These data sources contain the same entites, but different data. I set up some of the basics of the project with one datasource and a managed persistence context and it works great, but I'm having trouble with the multiple sources part. I've done a bit of searching through docs and forums, but I haven't found any answers so far.

      My questions:

      1) Do I need to declare a persistence unit in my persistence.xml for each of these data sources?
      In the long run I'd like to be able to add a new datasource without having to redeploy the application, so it would be nice to avoid this.

      2) How can i programmatically set a managed persistence context in a bean?
      The persistence:managed-persistence-context component seems tied to a specific persistence unit via the factory jndi name in the persistence.xml. I couldn't do simple injection then because I would be tied to that unit right? I tried passing in an EntityManager instead of injecting it and I could retrieve data but I got 'no active transaction' errors when I actually tried to do anything, so it seems the conversation piece wasn't working.

      I'm looking into overriding EntityManagerFactory to possibly create the correct Entitymanager on login (when the source is chosen) and put it into the session at that point. Does this seem like a viable approach?

      Any help appreciated.

      Thanks,
      Greg

        • 1. Re: multiple data sources

          I just posted a very similar question in the EJB forum. If you don't get any response here, look over there. Hopefully someone will be able to point us in the right direction.

          • 2. Re: multiple data sources
            pmuir

             

            "gstacey" wrote:
            I'm looking into overriding EntityManagerFactory to possibly create the correct Entitymanager on login (when the source is chosen) and put it into the session at that point. Does this seem like a viable approach?


            I would put the correct EntityManagerFactory into the session scope and then keep you peristence context in the conversation scope as normal.

            • 3. Re: multiple data sources
              gstacey

              I'm just now revisiting this topic (got pulled onto a higher priority project), and another concept / question occurred to me.

              Since the entities are the same for all the datasources do I really need multiple persistence units? i.e. the only difference is the underlying connection right? Could I just get the delegate and change the Connection object at runtime?

              It seems, on the surface, like this should work... if the connection is bound to the EntityManager and it doesn't change connections and the EntityManager is bound to the Seam session then changing the connection once, when the proper source is determined should work.

              Can anyone comment on the feasibility of this?

              • 4. Re: multiple data sources
                pmuir

                Normally you bind the connection to the PU programmatically in persistence.xml - but if your underlying JPA impl allows you to safely change the connection object then there is no problem in Seam (this isn't something offered by JPA afaik, I would ask in the Hibernate forum, assuming you use Hibernate).