6 Replies Latest reply on Apr 13, 2007 2:15 AM by Carsten Hoehne

    Multiple datasources?

    Marcos Hermida Newbie

      Hi all!
      How can I use two different datasources in seam?
      I've tried to add both datasources in files myproject-ds.xml and persistence.xml and bind my entities using the @PersistenceUnits annotation, but it didn't work.
      When i deploy the application it seems that it overlooks the @PersistenceUnits annotation and tries to bind the entities to both datasources and it fails because it only find the corresponding table in one datasource, so, at the end i get the following message:

      --- MBeans waiting for other MBeans ---
      ObjectName: persistence.units:ear=plantilla.ear,jar=plantilla.jar,unitName=estacions
      State: FAILED
      Reason: javax.persistence.PersistenceException: org.hibernate.HibernateException: Missing table: Imaxes
      I Depend On:
      jboss.jca:service=DataSourceBinding,name=estacionsDatasource

      Any ideas?
      Thanx!!

        • 1. Re: Multiple datasources?
          Eric Ray Novice

          datasource...

          <datasources>
          
           <local-tx-datasource>
           <jndi-name>PostgresDS1</jndi-name>
           <connection-url>jdbc:postgresql://localhost:5432/db1</connection-url>
           <driver-class>org.postgresql.Driver</driver-class>
           <user-name>db1</user-name>
           <password>db1</password>
           <max-pool-size>35</max-pool-size>
           <blocking-timeout-millis>60000</blocking-timeout-millis>
           <metadata>
           <type-mapping>PostgreSQL 8.0</type-mapping>
           </metadata>
           </local-tx-datasource>
          
           <local-tx-datasource>
           <jndi-name>PostgresDS2</jndi-name>
           <connection-url>jdbc:postgresql://localhost:5432/db2</connection-url>
           <driver-class>org.postgresql.Driver</driver-class>
           <user-name>db2</user-name>
           <password>db2</password>
           <max-pool-size>35</max-pool-size>
           <blocking-timeout-millis>60000</blocking-timeout-millis>
           <metadata>
           <type-mapping>PostgreSQL 8.0</type-mapping>
           </metadata>
           </local-tx-datasource>
          
          </datasources>
          



          persistence.xml...
          <persistence version="1.0"
          xmlns="http://java.sun.com/xml/ns/persistence"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
          http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
          
           <persistence-unit name="db1-PU" transaction-type="JTA">
           <provider>org.hibernate.ejb.HibernatePersistence</provider>
           <jta-data-source>java:/PostgresDS1</jta-data-source>
           <properties>
           <property name="show_sql" value="true"/>
           <property name="hibernate.hbm2ddl.auto" value="update"/>
           <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
           <property name="jboss.entity.manager.jndi.name" value="java:/DB1EntityManager"/>
           <property name="jboss.entity.manager.factory.jndi.name" value="java:/DB1EntityManagerFactory"/>
           </properties>
           </persistence-unit>
          
           <persistence-unit name="db2-PU" transaction-type="JTA">
           <provider>org.hibernate.ejb.HibernatePersistence</provider>
           <jta-data-source>java:/PostgresDS2</jta-data-source>
           <properties>
           <property name="show_sql" value="true"/>
           <property name="hibernate.hbm2ddl.auto" value="update"/>
           <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
           <property name="jboss.entity.manager.jndi.name" value="java:/DB2EntityManager"/>
           <property name="jboss.entity.manager.factory.jndi.name" value="java:/DB2EntityManagerFactory"/>
           </properties>
           </persistence-unit>
          </persistence>
          


          Source code...
           @PersistenceContext(unitName="db1-PU")
           private EntityManager db1EntityManager;
          
           @PersistenceContext(unitName="db2-PU")
           private EntityManager db2EntityManager;
          


          Notice the <jta-data-source> value in the persistence.xml matches the <jndi-name> in the datasource. Then the source code annotates two different instances of an EntityManager with the appropriate PersistenceContext

          • 2. Re: Multiple datasources?
            Marcos Hermida Newbie

            Ok!!!!!!

            Thanks for the reply, it works fine!!

            • 3. Re: Multiple datasources?
              Carsten Hoehne Master

              I have followed the advise, but still having the problem in the first post.
              My entities are bound to both datasources.
              I do not have a clue what i have missed:-(

              [qoute]bind my entities using the @PersistenceUnits annotation

              • 4. Re: Multiple datasources?
                Carsten Hoehne Master

                Sorry, tried again.
                I have followed the advise, but still having the problem in the first post.
                My entities are bound to both datasources.
                I do not have a clue what i have missed:-(

                bind my entities using the @PersistenceUnits annotation

                Perhaps this is the key. But in which way i must anotate my entities?
                Ciao,
                Carsten

                • 5. Re: Multiple datasources?
                  Carsten Hoehne Master

                  marcosH
                  would you be so kind to share your solution with me?

                  • 6. Re: Multiple datasources?
                    Carsten Hoehne Master

                    marcosH
                    would you be so kind to share your solution with me?