12 Replies Latest reply on Sep 14, 2007 4:52 AM by olvin

    Dynamic data sources with EntityManager

    wiberto

      I have an application where the data source it uses is saved in the database. This is because each user can have its own data source.

      So UserA will have an entry in it's user information table that says to use data source jdbc/UserA.

      Lets assume that this entry is already configured in the app server itself.

      How do I tell the EntityManager at runtime to use that datasource.

      If this was a static configuration I know I will have the persistence unit defined in my persistence.xml file and I would set the annotation in my entity. Something like this

      @PersistenceContext(unitName="userAunit")
      private EntityManager em;
      




      So how do I make this generic? Do I need to override any EntityManager API to be able to select the unitName at run time? Also, can I create persistence units on the fly?

      Thanks in advanced,

      Jose

        • 1. Re: Dynamic data sources with EntityManager
          fnordinuk

          I have the same database setup that I'm trying to get EJB3 to work with. Any help on this one would be much appreciated.

          • 2. Re: Dynamic data sources with EntityManager
            pmuir

            You can't put runtime parameters into annotations so you'll have to access the PC programmatic ally. The neatest way I can see to do this is to use a Seam manager component to look up and inject the correct PC based upon who is logged in.

            • 3. Re: Dynamic data sources with EntityManager
              wiberto

              Any samples out there on how to create a Seam manager component?

              • 4. Re: Dynamic data sources with EntityManager
                wiberto

                That's what I thought, that it would have to be programatically since annotations are static in the source code. I'm fine with that.

                • 5. Re: Dynamic data sources with EntityManager
                  pmuir

                  Most of Seam is implemented using manager components. e.g. MailSession.

                  • 6. Re: Dynamic data sources with EntityManager
                    wiberto

                    How do I then go about having Seam use my component? Do I have to do anything special in the components.xml file?

                    • 7. Re: Dynamic data sources with EntityManager
                      pmuir

                      No.

                      • 8. Re: Dynamic data sources with EntityManager
                        wiberto

                        I was looking over at the documentation for the components and came across this:

                        
                        <component name="customerDatabase"
                         class="org.jboss.seam.persistence.ManagedPersistenceContext">
                         <property name="persistenceUnitJndiName">java:/customerEntityManagerFactory</property>
                         </component>
                        


                        Can I then create my own persistenceContext that gets some information about the user that is logged in probably through the Identity object?

                        Then in the class that needs it, how would I tell it use use this persistence manager? Can I pass a name attribute to the @PersistenceContext annotation?

                        • 9. Re: Dynamic data sources with EntityManager
                          wiberto

                          I think I got this figured out now!!!

                          I was able to create my own persistence context as I stated in the previous post, and I'm then creating my own entitymanager factory. there I point to the the persistence unit I want to use.

                          Now the issue I have is that I want to pass overrides to the entitymanager factory. My persistence.xml looks like this:

                          <persistence-unit name="userbased" transaction-type="RESOURCE_LOCAL">
                           <provider>org.hibernate.ejb.HibernatePersistence</provider>
                           <class>com.numbersinsight.data.customer.User</class>
                           <properties>
                           <property name="hibernate.dialect"
                           value="org.hibernate.dialect.MySQLDialect" />
                           <property name="hibernate.connection.driver_class"
                           value="com.mysql.jdbc.Driver" />
                           <property name="hibernate.connection.username" value="numins" />
                           <property name="hibernate.connection.password" value="num6482bersi" />
                           <property name="hibernate.connection.url"
                           value="jdbc:mysql://localhost:3306/numins" />
                           <property name="hibernate.hbm2ddl.auto" value="none" />
                           <property name="hibernate.show_sql" value="true" />
                           <property name="hibernate.use_sql_comments" value="true" />
                           </properties>
                           <non-jta-data-source>java:jdbc/numins</non-jta-data-source>
                           </persistence-unit>
                          


                          The connection is always made using the data source deifnintion. If I take that out the application will not deploy complaining about not having a data source defined. Do you know of a way to not have that data source defined and have it use the properties?

                          This way it will make it easy to load the properties and configure the connection properties at runtime based on the user.

                          • 10. Re: Dynamic data sources with EntityManager
                            olvin

                            Anybody has experimented a solution for that ? And a concrete example ?

                            Thanks,

                            Olivier

                            • 11. Re: Dynamic data sources with EntityManager
                              wiberto

                              I will post what I did to solve this problem in my blog soon. I'll post again when I have it out there.

                              Thanks,

                              Jose

                              • 12. Re: Dynamic data sources with EntityManager
                                olvin

                                Hello wiberto,

                                Could you post your solution ? Or only the idea ?

                                Thanks,

                                Olivier