Hi,
I'm actually trying to use the spring data framework to use the repository mecanism with a multidatabase system running with an external (Atomikos) JTA transactionManager.
Here is my applicationContext.xml configuration file :
After havinig done this, I declared two repositories :Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <context:property-placeholder location="classpath*:spring/*.properties" /> <context:component-scan base-package="com.company.app" /> <tx:jta-transaction-manager /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="users" /> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> </bean> </property> </bean> <bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="other" /> <property name="dataSource" ref="dataSource2" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> </bean> </property> </bean> <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="XA1DBMS" /> <property name="xaDataSource" ref="db1" /> <property name="poolSize" value="3" /> </bean> <bean id="db1" class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" lazy-init="true"> <property name="pinGlobalTxToPhysicalConnection" value="true" /> <property name="user" value="root" /> <property name="password" value="pass" /> <property name="url" value="jdbc:mysql://localhost:3306/users" /> </bean> <bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="XA2DBMS" /> <property name="xaDataSource" ref="db2" /> <property name="poolSize" value="3" /> </bean> <bean id="db2" class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" lazy-init="true"> <property name="pinGlobalTxToPhysicalConnection" value="true" /> <property name="user" value="root" /> <property name="password" value="pass" /> <property name="url" value="jdbc:mysql://localhost:3306/users2" /> </bean> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="false" /> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.J2eeUserTransaction"> <property name="transactionTimeout" value="300" /> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" depends-on="atomikosTransactionManager,atomikosUserTransaction"> <property name="transactionManager" ref="atomikosTransactionManager" /> <property name="userTransaction" ref="atomikosUserTransaction" /> <property name="allowCustomIsolationLevels" value="true" /> </bean> <jpa:repositories base-package="com.company.repository" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"> <jpa:repository id="userDetailRepository" /> </jpa:repositories> <jpa:repositories base-package="com.company.app.repository" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory2"> <jpa:repository id="otherRepository" /> </jpa:repositories> </beans>
andCode:import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.querydsl.QueryDslPredicateExecutor; import com.company.app.entities.UserDetail; public interface UserDetailRepository extends JpaRepository<UserDetail, Integer>, QueryDslPredicateExecutor<UserDetail> { List<UserDetail> findByUserName(String userName); }
And my test class is :Code:import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import com.company.app.entities.Other; public interface OtherRepository extends JpaRepository<Other, Integer> { List<Other> findByName(String name); }
After having executed my test, I noticed 2 strange results:Code:import java.util.List; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; import com.company.app.entities.Other; import com.company.app.repository.OtherRepository; /** * Unit test for simple App. */ @ContextConfiguration("classpath:spring/applicationContext.xml") public class OtherTest extends AbstractTransactionalJUnit4SpringContextTests { @Autowired OtherRepository otherRepository; @Test public void database2Test() { Other aOther = new Other(); aOther.setName("TestUser"); aOther = otherRepository.save(aOther); System.out.println("id = " + aOther.getId()); List<Other> other = otherRepository.findByName("TestUser"); for (Other other2 : other) { System.out.println("other = " + other2.getId() + " - " + other2.getName()); } } }
- The Log :
So the save(Entity entity) method wasn't executed.Code:id = null Hibernate: select other0_.id as id4_, other0_.name as name4_ from Other other0_ where other0_.name=?
- The database :
When I look to my databases (users and users2), I noticed that the 2 entities (Other and UserDetail) have been added inside both databases.
I guess that all the repositories have been associated to my 2 datasources.
I don't know what I have missed in my configuration, or maybe I should annotate my repositories to associate them to each persistenceUnit.
Can anyone help?
Thanks


Reply With Quote
