Multiple datasources and persistence units
squirrel Sep 9, 2008 4:03 PMOkay, I'm trying to use a couple of datasources (and persistence units) in my seam application. I don't understand why this doesn't work - it seems to want to bind every entity to every datasource.
I'm using JBoss 4.2.2 and SEAM 2.0.2.SP1. I'm using the latest stable version of JBoss Tools (for Europa) to create the project and entities.
Start a new seam project, set up the jboss server and the first datasource using the tools wizard. Update the -ds.xml file with the second datasource, create the second persistence unit in persistence.xml, and add the context references to components.xml:
myProject-ds.xml:
<local-tx-datasource> <jndi-name>myProjectDatasource</jndi-name> <connection-url>jdbc:hsqldb:MyDB</connection-url> <driver-class>org.hsqldb.jdbcDriver</driver-class> <user-name>sa</user-name> <password></password> </local-tx-datasource> <local-tx-datasource> <jndi-name>otherDatasource</jndi-name> <connection-url>jdbc:jtds:sqlserver://sandbox0:1433/MyTestDB</connection-url> <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class> <user-name>jondoe</user-name> <password>password1</password> </local-tx-datasource>
persistence.xml:
<persistence-unit name="myProject"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/myProjectDatasource</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="jboss.entity.manager.factory.jndi.name" value="java:/myProjectEntityManagerFactory"/> </properties> </persistence-unit> <persistence-unit name="otherPU"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/otherDatasource</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/> <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:/otherDatasourceEntityManagerFactory"/> </properties> </persistence-unit>
components.xml:
<persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/myProjectEntityManagerFactory"/> <persistence:managed-persistence-context name="otherEntityManager" auto-create="true" persistence-unit-jndi-name="java:/otherDatasourceEntityManagerFactory"/>
Next, create 2 entities. Dog
will be in the secondary datasource, so it's annotated with
@PersistenceContext(name="otherPU")
The Person
entity is in the main datasource, and annotated thusly:
@PersistenceContext(name="myProject")
I've tried a number of variations of this configuration. The secondary datasource will not be used extensively. I tried using a non-jta-data-source and adding RESOURCELOCAL as the transaction type (as documented here), etc.
But just deploying the application with these settings fails due to this error:
09:29:00,349 WARN [ServiceController] Problem starting service persistence.units:ear=myProject-ear.ear,unitName=otherPU javax.persistence.PersistenceException: org.hibernate.HibernateException: Missing table: Person
As you can see, it is attempting to bind the Person
table to the otherPU
persistence unit, even though it's annotated to specify the main myProject
unit.
So what am I missing? Is there a way to do this? Any suggestions?
Thx... squirrel