Announcement Announcement Module
Collapse
No announcement yet.
Spring Data + JTA Atomikos + Multidatabase configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data + JTA Atomikos + Multidatabase configuration

    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 :
    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>
    After havinig done this, I declared two repositories :

    Code:
    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
    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);
    
    
    }
    And my test class is :

    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());
    		}
    
    	}
    
    }
    After having executed my test, I noticed 2 strange results:

    - The Log :
    Code:
    id = null
    Hibernate: select other0_.id as id4_, other0_.name as name4_ from Other other0_ where other0_.name=?
    So the save(Entity entity) method wasn't executed.
    - 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

  • #2
    Hi again,

    Ok I fixed the first problem : actually I can insert into database with the save(entity) repository interface method.
    To do this, I noticed that I havent changed the "transaction-type" param of the "persistence-unit" node in my persistence.xml file from "RESOURCE_LOCAL" to "JTA". I also had to add some preperties. So my actual presistence.xml files looks like :

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.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_2_0.xsd">
    	<persistence-unit name="users" transaction-type="JTA">
    		<class>com.company.app.entities.UserDetail</class>
    		<properties>
    			<property name="zeroDateTimeBehavior" value="convertToNull" />
    			<!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
    			<property name="hibernate.transaction.manager_lookup_class"
    				value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />
    			<property name="hibernate.transaction.factory_class"
    				value="org.hibernate.transaction.CMTTransactionFactory" />
    		</properties>
    	</persistence-unit>
    	<persistence-unit name="other" transaction-type="JTA">
    		<class>com.company.app.entities.Other</class>
    		<properties>
    			<property name="zeroDateTimeBehavior" value="convertToNull" />
    			<!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
    			<property name="hibernate.transaction.manager_lookup_class"
    				value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />
    			<property name="hibernate.transaction.factory_class"
    				value="org.hibernate.transaction.CMTTransactionFactory" />
    		</properties>
    	</persistence-unit>
    </persistence>
    So now, my test methos log show :
    Code:
    Hibernate: insert into Other (name) values (?)
    id = 4
    Hibernate: select other0_.id as id4_, other0_.name as name4_ from Other other0_ where other0_.name=?
    other = 2 - TestUser
    other = 3 - TestUser
    other = 4 - TestUser
    Cool !!

    Nevertheless, I still have a duplicated db schema inside my 2 databases. I tries to manually set the list of entites fore each persistence-unit (<class>com.company.entity.....</class>) but this have changed nothing !

    Any Idea?

    Comment


    • #3
      OK solved !!

      I just excluded hibernate from "com.atomikos-transactions-hibernate3" dependency

      Code:
      <dependency>
      	<groupId>com.atomikos</groupId>
      	<artifactId>transactions-hibernate3</artifactId>
      	<version>${atomikos.version}</version>
      	<exclusions>
      		<exclusion>
      			<artifactId>hibernate</artifactId>
      			<groupId>org.hibernate</groupId>
      		</exclusion>
      	</exclusions>
      </dependency>
      Hope that helped!

      Comment

      Working...
      X