1 Reply Latest reply on Jan 20, 2012 4:43 AM by tmarques

    Multiple Datasources - When starting JBoss the entities are created in all datasources

    tmarques

      Hi guys,

       

      I am facing a problem, and i "google it" for some time and i not found any solution.

       

      Due to logistic issues i have to distribute the tables in different data bases. When i run the application Server, Jboss tries to create all entities in the different databases.

       

      Software:

      Jboss 5.1.0

      JBoss ESB 4.9

       

      Please view the details:

       

      postgres-ds.xml:

       

      <datasources>

                <local-tx-datasource>

                          <jndi-name>FirstDS</jndi-name>

                          <connection-url>jdbc:postgresql://localhost:5432/application

                          </connection-url>

                          <driver-class>org.postgresql.Driver</driver-class>

                          <security-domain>database-access</security-domain>

                          <check-valid-connection-sql>select 1</check-valid-connection-sql>

                          <metadata>

                                    <type-mapping>PostgreSQL</type-mapping>

                          </metadata>

                </local-tx-datasource>

       

                <local-tx-datasource>

                          <jndi-name>SecondDS</jndi-name>

                          <connection-url>jdbc:postgresql://localhost:5432/general

                          </connection-url>

                          <driver-class>org.postgresql.Driver</driver-class>

                                    <security-domain>database</security-domain>

                          <check-valid-connection-sql>select 1</check-valid-connection-sql>

                          <metadata>

                                    <type-mapping>PostgreSQL</type-mapping>

                          </metadata>

                </local-tx-datasource>

      </datasources>

       

       

      persistence.xml

       

      <persistence 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"

        version="1.0">

       

        <persistence-unit name="first" transaction-type="JTA">

        <jta-data-source>java:/FirstDS</jta-data-source>

        <properties>

        <property name="hibernate.hbm2ddl.auto" value="create" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

        <property name="hibernate.show_sql" value="false" />

        </properties>

        </persistence-unit>

       

        <persistence-unit name="second"

        transaction-type="JTA">

        <jta-data-source>java:/SecondDS</jta-data-source>

        <properties>

        <property name="hibernate.hbm2ddl.auto" value="create" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

        <property name="hibernate.show_sql" value="true" />

        </properties>

        </persistence-unit>

      </persistence>

       

       

      Entities:

      @Entity

      @Table(name = "table1")

      @PersistenceContext(unitName = "first")

      public class Table1 implements Serializable {...}

       

      @Entity

      @Table(name = "table2")

      @PersistenceContext(unitName = "second")

      public class Table2 implements Serializable {...}

       

       

      For this case i expect that :

      • table first only exist in FirstDS and
      • table second only exists in SecondDS.

       

      But when i consult log i see that JBoss is trying to create in the diferent data sources the two tables.

       

      Any one can help?

       

      Thanks

      TM

       

       

       


        • 1. Re: Multiple Datasources - When starting JBoss the entities are created in all datasources
          tmarques

          Hi guys.

           

          After some tests i found a solution. I leave the solution found:

           

           

          I split the postgres-ds.xml in two:

          • first-ds.xml
          • second-ds.xml

           

          I split the persistence.xml in two and when i build the .sar files with ant i compile the following information:

          • firstPersistence.sar
            • persistence.xml(firstt)
            • entities with @PersistenceContext(unitName = "first")
          • secondPersistence.sar
            • persistence.xml(second)
            • entities with @PersistenceContext(unitName = "second")

           

          I dont know if is the best solution,but it works

          If anyone have a best solution or know this solution give problems, please give your feedback.

           

           

          Thanks

          TM