7 Replies Latest reply on Oct 7, 2009 2:58 PM by Mita Bhushan

    Seam with Multiple Datasources

    Mita Bhushan Newbie

      I am tring to use multiple datasources in a seam project





      xx-ds.xml is 
      
      <datasources>
         
         <local-tx-datasource>
            <jndi-name>hrtrackerDatasource</jndi-name>
            <connection-url>jdbc:mysql://xx:3306/hrtracker</connection-url>
            <driver-class>com.mysql.jdbc.Driver</driver-class>
            <user-name>xx</user-name>
            <password>xx</password>
         </local-tx-datasource>
      
         
         <local-tx-datasource>
          <jndi-name>employeeDatasource</jndi-name>
          <connection-url>jdbc:as400://xxx/medlib/mnuusr</connection-url>
          <driver-class>com.ibm.as400.access.AS400JDBCDriver</driver-class>
          <user-name>xx</user-name>
          <password>xx</password>  
         </local-tx-datasource>
            
      
      
      persistence-dev.xml 
      
       <persistence-unit name="hrtracker">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/hrtrackerDatasource</jta-data-source>
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
               <property name="hibernate.hbm2ddl.auto" value="validate"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/hrtrackerEntityManagerFactory"/>
               <property name="hibernate.default_catalog" value="hrtracker"/>
            </properties>
         </persistence-unit>
         
          <persistence-unit name="employee" >
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/employeeDatasource</jta-data-source>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.DB2400Dialect" />
                <property name="hibernate.hbm2ddl.auto" value="validate"/>
                <property name="hibernate.show_sql" value="true"/>
                <property name="hibernate.format_sql" value="true"/>
                <property name="jboss.entity.manager.factory.jndi.name" value="java:/employeeEntityManagerFactory"/>
                <property name="hibernate.default_catalog" value="xxxx"/>
                <property name="hibernate.default_schema" value="PRODDTA"/>      
            </properties>
         </persistence-unit>
            
      
      
      components.xml 
      
      
      <persistence:managed-persistence-context name="entityManager"
                                           auto-create="true"
                            persistence-unit-jndi-name="java:/hrtrackerEntityManagerFactory"/>   
                            
         <persistence:managed-persistence-context name="employeeManager"
                         auto-create="true"
                            persistence-unit-jndi-name="java:/employeeEntityManagerFactory"/>                           
      
            
      
      
      I used following pattern for class files 
      EmployeeList.java 
      
      
      package com.hrtracker.session;
      
      import javax.persistence.EntityManager;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityQuery;
      
      import com.hrtracker.entity.*;
      
      @Name("employeeList")
      public class EmployeeList extends EntityQuery {
       .................
         @Override
          protected String getPersistenceContextName() {
             return "employeeManager";    
          }
      }
            
      
      
      
      Employee.java 
      
      
      package com.med.stryker.hrtracker.entity;
      ...
      
      @Entity                                                                                             
                                                                                                          
                    
      @Table(name="F0101")                                                                    
      public class Employee implements java.io.Serializable
      {
           @In
           EntityManager employeeManager;
      
       ...............
      }
            
      
      The error I am getting is 
      
      
      ObjectName: persistence.units:ear=hrtracker.ear,unitName=employee
        State: FAILED
        Reason: javax.persistence.PersistenceException: org.hibernate.HibernateException: Missing table: audit_military
        I Depend On:
          jboss.jca:service=DataSourceBinding,name=employeeDatasource
      
      ObjectName: persistence.units:ear=hrtracker.ear,unitName=hrtracker
        State: FAILED
        Reason: javax.persistence.PersistenceException: org.hibernate.HibernateException: Missing table: F0101
        I Depend On:
          jboss.jca:service=DataSourceBinding,name=hrtrackerDatasource
      
      *Here audit_military is in hrtrackerDatasource and F0101 is in employeeDatasource*
      
      
      Stacktrace is 
      
      
      com.sun.facelets.FaceletViewHandler handleRenderException
      SEVERE: Error Rendering View[/home.xhtml]
      java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:/hrtrackerEntityManagerFactory
           at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBindi
      ng(ManagedPersistenceContext.java:247)
           at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManager(ManagedPersistenceContext
      .java:79)
           at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager(ManagedPersistenceContext.
      java:108)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
           at java.lang.reflect.Method.invoke(Unknown Source)
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
           at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
           at org.jboss.seam.Component.callComponentMethod(Component.java:2082)
           at org.jboss.seam.Component.unwrap(Component.java:2108)
           at org.jboss.seam.Component.getInstance(Component.java:1887)
           at org.jboss.seam.Component.getInstance(Component.java:1840)
           at org.jboss.seam.Component.getInstance(Component.java:1834)
           at org.jboss.seam.framework.Controller.getComponentInstance(Controller.java:263)
           at org.jboss.seam.framework.PersistenceController.getPersistenceContext(PersistenceController.java:
      20)
           at org.jboss.seam.framework.EntityQuery.getEntityManager(EntityQuery.java:153)
           at org.jboss.seam.framework.EntityQuery.validate(EntityQuery.java:37)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
           at java.lang.reflect.Method.invoke(Unknown Source)
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
           at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
           at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:38)
           at org.jboss.seam.util.Work.workInTransaction(Work.java:40)
           at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:32)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
           at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
           at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)      
      



      Please help!

        • 1. Re: Seam with Multiple Datasources
          David Serres Newbie

          Hi! I have the same problem!
          You can resolve it?
          Thanks!

          • 2. Re: Seam with Multiple Datasources
            Mark Wigmans Newbie
            Without knowing what the exact cause of your problem is, I can tell you that:
            - I my current application I have it running (two datasources);
            - I see that in my case I define property   hibernate.session_factory_name as well, in my case:
                        <property name="hibernate.session_factory_name" value="SessionFactory1" />

            Maybe this helps
            • 3. Re: Seam with Multiple Datasources
              David Serres Newbie

              Thanks for you reply Mark.
              I tried to put his property and the problem persist.



              My defined datasources



              <datasources>
                 
                 <local-tx-datasource>
                    <jndi-name>iuDatasource</jndi-name>
                    <use-java-context>true</use-java-context>
                    <connection-url>jdbc:oracle:thin:@sagitario:1521:orcl</connection-url>
                    <driver-class>oracle.jdbc.OracleDriver</driver-class>
                    <user-name>xxx</user-name>
                    <password>xxx</password>
                 </local-tx-datasource>
                  
                  <local-tx-datasource>
                    <jndi-name>grDatasource</jndi-name>
                    <use-java-context>true</use-java-context>
                    <connection-url>jdbc:oracle:thin:@sagitario:1521:orcl</connection-url>
                    <driver-class>oracle.jdbc.OracleDriver</driver-class>
                    <user-name>xxx</user-name>
                    <password>xxx</password>
                 </local-tx-datasource>
              </datasources>




              persistence.xml...



              <persistence-unit name="iu">
                    <provider>org.hibernate.ejb.HibernatePersistence</provider>
                    <jta-data-source>java:/iuDatasource</jta-data-source>
                    <properties>
                         <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
                       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
                       <property name="hibernate.hbm2ddl.auto" value="validate"/>
                       <property name="hibernate.show_sql" value="true"/>
                       <property name="hibernate.format_sql" value="true"/>
                       <property name="hibernate.session_factory_name" value="iuSessionFactory" />
                       <property name="jboss.entity.manager.factory.jndi.name" value="java:/iuEntityManagerFactory"/>
                    </properties>
                 </persistence-unit>
                 
                 <persistence-unit name="gr">
                    <provider>org.hibernate.ejb.HibernatePersistence</provider>
                    <jta-data-source>java:/grDatasource</jta-data-source>
                    <properties>
                         <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
                       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
                       <property name="hibernate.hbm2ddl.auto" value="validate"/>
                       <property name="hibernate.show_sql" value="true"/>
                       <property name="hibernate.format_sql" value="true"/>
                        <property name="hibernate.session_factory_name" value="grSessionFactory" />
                       <property name="jboss.entity.manager.factory.jndi.name" value="java:/grEntityManagerFactory"/>
                    </properties>
                 </persistence-unit>



              components.xml....




              <persistence:managed-persistence-context name="entityManager" auto-create="true"
                                    persistence-unit-jndi-name="java:/iuEntityManagerFactory"/>
              
               <persistence:managed-persistence-context name="GRentityManager" auto-create="true"
                                    persistence-unit-jndi-name="java:/grEntityManagerFactory"/>




              When validating the schema, is a sequence that belongs to the wrong db...

              • 4. Re: Seam with Multiple Datasources
                David Serres Newbie

                This is my exception...




                ObjectName: persistence.units:ear=iu-ear.ear,unitName=gr
                  State: FAILED
                  Reason: javax.persistence.PersistenceException: org.hibernate.HibernateException: Missing sequence or table: SEC_ATRIBUTO
                  I Depend On:
                    jboss.jca:service=DataSourceBinding,name=grDatasource



                The SECATRIBUTO sequence belongs to iuDataSource...
                I don't know what happens

                • 5. Re: Seam with Multiple Datasources
                  Mark Wigmans Newbie

                  Are you sure it works with 1 datasource, because it could also mean that you told JPA/Hibernate to generate the primary key and in that in your particular case you need a table/sequence.

                  • 6. Re: Seam with Multiple Datasources
                    Alex Cougarman Apprentice

                    Is there some documentation on this because we'd like to do the same thing and don't know where to start. Thanks.

                    • 7. Re: Seam with Multiple Datasources
                      Mita Bhushan Newbie

                      I got it to work for my example above along with xx-ds.xml, persistence.xml, components.xml by adding


                      Employee.java


                      @Entity                                                                                                                                                                                                  
                      @Table(name="F0101")                                                                    
                      public class Employee implements java.io.Serializable
                      {
                           @In
                           EntityManager employeeManager;
                      
                       ...............
                      }
                      



                      EmployeeList.java


                      @Name("employeeList")
                      public class EmployeeList extends EntityQuery {
                       .................
                         @Override
                          protected String getPersistenceContextName() {
                             return "employeeManager";    
                          }
                      }
                      



                      EmployeeHome.java


                      @Name("employeeHome")
                      public class EmployeeHome extends EntityHome<Employee> {
                      
                      @Override
                          protected String getPersistenceContextName() {
                             return "employeeManager";    
                          }
                      }