7 Replies Latest reply on Jul 14, 2009 5:33 PM by Jean Luc

    VERY simple multiple datasources

    Gerald Anderson Newbie

      Greetings all,


        I have a project that requires me to use multiple datasources.  I've done a fair amount of searching but my example is MUCH more simplistic than all of the other stuff I'm seeing out there (ie, XA stuff, etc).  I figure there must be a very simple way of doing is so thought I'd ask.


        The situation is that I have my Seam app database, I also have a database that drives my external portal.  Finally, I'm authenticating externally to both of these databases (LDAP via CAS).  ALL I need to do is when a user logs in, if he's not in the Seam database simply do a single query to the portal database to retrieve the user's info.  That's it : /


        I realize I need to create another datasource in components.xml, and create an entity for the portal db table, but how do I tell the entity to use the 2nd datasource?  Am I way off base with how simple it is?


      Thanks!
      Gerald

        • 1. Re: VERY simple multiple datasources
          Sylvain Catudal Newbie

          Hi Gerald,
          I did something similar for a project I'm working on.  I added a no-tx-datasource in my datasource file and than added another persistence unit in my persistence.xml file.  This new persistence unit is not managed by seam because I don't need it to be.  I think it's the same for you.  Then you just need to have you persistence context injected by the container like this :


               @PersistenceContext( unitName = "whateverYouNameIt" )
               EntityManager whateverYouNameIt;
          



          Note that for the container to inject the persistence context, the class must be an EJB.


          Hope this helps.


          Sylvain

          • 2. Re: VERY simple multiple datasources
            Gerald Anderson Newbie

            Sylvain,


              Thanks very much for your response!  Unfortunately the Seam app is a mature WAR app, not EJB.  Anyway to do something similar in that situation?  It seems you are right on the money as to what I'm after though.


            Gerald

            • 3. Re: VERY simple multiple datasources
              Sylvain Catudal Newbie

              I don't have time to test it, but here is what I would try:


              Add a no-tx-datasource datasource in whatever-ds.xml :


                   <no-tx-datasource>
                        <jndi-name>fooDatasource</jndi-name>
                        <connection-url>...</connection-url>
                        <driver-class>...</driver-class>
                        <user-name>...</user-name>
                        <password>...</password>
                   </no-tx-datasource>
              
              



              Add a persistence unit in persistence.xml :


                 <persistence-unit name="foo">
                    <provider>org.hibernate.ejb.HibernatePersistence</provider>
                    <jta-data-source>java:/fooDatasource</jta-data-source>
                    <properties>
                       <property name="hibernate.dialect" value="..."/>
                       <property name="hibernate.show_sql" value="false"/>
                       <property name="hibernate.format_sql" value="false"/>
                       <property name="jboss.entity.manager.factory.jndi.name" 
                                 value="java:/fooEntityManagerFactory"/>
                    </properties>
              
                 </persistence-unit>
              



              And finally add a new managed persistence context in component.xml :


                   <persistence:managed-persistence-context
                        name="fooEntityManager" auto-create="true" 
                              persistence-unit-jndi-name="java:/fooEntityManagerFactory"  />
              
              



              You should now be able to have seam inject an EntityManager with this :


                   @In
                   EntityManager fooEntityManager;
              



              Hoping I'm not misleading you! ;)


              Sylvain

              • 4. Re: VERY simple multiple datasources
                Gerald Anderson Newbie

                With some minor tweaking due to my specific configuration, that was it ; )


                Thanks!


                Gerald

                • 5. Re: VERY simple multiple datasources
                  Arbi Sookazian Master

                  What are the differences between using <local-tx-datasource> vs. <no-tx-datasource> in foo-ds.xml?


                  Does this cause JPA not to use the cache, for example?  Or is it a performance optimization in the case of using JDBC/sprocs?

                  • 6. Re: VERY simple multiple datasources
                    Sylvain Catudal Newbie

                    local-tx-datasource is Transactional while no-tx-datasource is not transactional.


                    If you need to have both connection transactional, you'll need to read a bit because I haven't done that yet.


                    Here is a link where you can get more info : Configuring JDBC DataSources


                    Sylvain

                    • 7. Re: VERY simple multiple datasources
                      Jean Luc Apprentice

                      You need to select the datasource according to the nature of the data source. That is, select <no-tx-datasource> if the data source is not transactional (such as an LDAP source or something using file system operations, for instance).