我们正在考虑分发我们的交易。我们实施了一个小项目作为测试人员,以确保它适合其他项目。
首先我们通过注释和java类设置配置并且它工作得很好但我们想尝试基于.XML的配置以避免创建太多的配置类。
我们正在使用JUnit进行测试,而不是在服务器中部署的适当的Web应用程序,甚至在加载实体管理器,事务配置和注入@Services时似乎工作正常我们在调用@中的方法时会收到以下错误存储库:
11:50:17.401 [main] DEBUG c.atomikos.icatch.imp.CoordinatorImp - Coordinator 172.18.3.125.tm0000100096 entering state: ACTIVE
11:50:17.401 [main] INFO c.a.i.imp.CompositeTransactionImp - setRollbackOnly() called for transaction 172.18.3.125.tm0000100096
11:50:17.401 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactoryA'
11:50:17.401 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactoryB'
11:50:17.401 [main] DEBUG o.s.t.jta.JtaTransactionManager - Participating transaction failed - marking existing transaction as rollback-only
11:50:17.401 [main] DEBUG o.s.t.jta.JtaTransactionManager - Setting JTA transaction rollback-only
11:50:17.401 [main] DEBUG c.a.i.imp.BaseTransactionManager - getCompositeTransaction() returning instance with id 172.18.3.125.tm0000100096
11:50:17.401 [main] DEBUG c.a.i.imp.BaseTransactionManager - getCompositeTransaction() returning instance with id 172.18.3.125.tm0000100096
11:50:17.401 [main] DEBUG c.atomikos.icatch.imp.CoordinatorImp - Coordinator 172.18.3.125.tm0000100096 entering state: ACTIVE
11:50:17.401 [main] INFO c.a.i.imp.CompositeTransactionImp - setRollbackOnly() called for transaction 172.18.3.125.tm0000100096
java.lang.NullPointerException
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:91)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
at sun.reflect.GeneratedMethodAccessor7.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:388)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:541)
at com.sun.proxy.$Proxy24.createEntityManager(Unknown Source)
at org.springframework.orm.jpa.EntityManagerFactoryUtils.doGetTransactionalEntityManager(EntityManagerFactoryUtils.java:282)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:254)
at com.sun.proxy.$Proxy36.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:410)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:416)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:401)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy38.save(Unknown Source)
at demo.service.StoreServiceImpl.store(StoreServiceImpl.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy41.store(Unknown Source)
at demo.EmployeeRepositoryTest.saveBoth(EmployeeRepositoryTest.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
11:50:17.411 [main] DEBUG o.s.t.jta.JtaTransactionManager - Initiating transaction rollback
11:50:17.411 [main] DEBUG c.a.i.imp.BaseTransactionManager - getCompositeTransaction() returning instance with id 172.18.3.125.tm0000100096
11:50:17.411 [main] DEBUG c.a.i.imp.BaseTransactionManager - getCompositeTransaction() returning instance with id 172.18.3.125.tm0000100096
11:50:17.411 [main] DEBUG c.atomikos.icatch.imp.CoordinatorImp - Coordinator 172.18.3.125.tm0000100096 entering state: ABORTING
11:50:17.411 [main] DEBUG c.atomikos.icatch.imp.CoordinatorImp - Coordinator 172.18.3.125.tm0000100096 entering state: TERMINATED
11:50:17.411 [main] DEBUG c.atomikos.icatch.imp.CoordinatorImp - Coordinator 172.18.3.125.tm0000100096 : stopping timer...
11:50:17.411 [main] DEBUG c.atomikos.icatch.imp.CoordinatorImp - Coordinator 172.18.3.125.tm0000100096 : disposing statehandler TERMINATED...
11:50:17.411 [main] DEBUG c.atomikos.icatch.imp.CoordinatorImp - Coordinator 172.18.3.125.tm0000100096 : disposed.
11:50:17.411 [main] DEBUG c.a.i.imp.CompositeTransactionImp - Ignoring error during event callback
java.lang.IllegalStateException: Transaction no longer active
at com.atomikos.icatch.imp.TxTerminatedStateHandler.rollbackWithStateCheck(TxTerminatedStateHandler.java:106) ~[transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CompositeTransactionImp.doRollback(CompositeTransactionImp.java:237) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CompositeTerminatorImp.rollback(CompositeTerminatorImp.java:123) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CompositeTransactionImp.rollback(CompositeTransactionImp.java:346) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CompositeTransactionImp.entered(CompositeTransactionImp.java:373) [transactions-3.9.3.jar:na]
at com.atomikos.finitestates.FSMImp.notifyListeners(FSMImp.java:188) [atomikos-util-3.9.3.jar:na]
at com.atomikos.finitestates.FSMImp.setState(FSMImp.java:279) [atomikos-util-3.9.3.jar:na]
at com.atomikos.icatch.imp.CoordinatorImp.setState(CoordinatorImp.java:416) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CoordinatorImp.setStateHandler(CoordinatorImp.java:273) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CoordinatorStateHandler.rollbackFromWithinCallback(CoordinatorStateHandler.java:769) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.ActiveStateHandler$7.doRollback(ActiveStateHandler.java:319) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CoordinatorStateHandler.rollbackWithAfterCompletionNotification(CoordinatorStateHandler.java:832) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.ActiveStateHandler.rollbackWithAfterCompletionNotification(ActiveStateHandler.java:49) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.ActiveStateHandler.rollback(ActiveStateHandler.java:314) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CoordinatorImp.rollback(CoordinatorImp.java:741) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.TransactionStateHandler.rollback(TransactionStateHandler.java:185) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.TransactionStateHandler.rollbackWithStateCheck(TransactionStateHandler.java:203) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CompositeTransactionImp.doRollback(CompositeTransactionImp.java:237) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CompositeTerminatorImp.rollback(CompositeTerminatorImp.java:123) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.imp.CompositeTransactionImp.rollback(CompositeTransactionImp.java:346) [transactions-3.9.3.jar:na]
at com.atomikos.icatch.jta.TransactionImp.rollback(TransactionImp.java:217) [transactions-jta-3.9.3.jar:na]
at com.atomikos.icatch.jta.TransactionManagerImp.rollback(TransactionManagerImp.java:448) [transactions-jta-3.9.3.jar:na]
at com.atomikos.icatch.jta.UserTransactionImp.rollback(UserTransactionImp.java:118) [transactions-jta-3.9.3.jar:na]
at org.springframework.transaction.jta.JtaTransactionManager.doRollback(JtaTransactionManager.java:1048) [spring-tx-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:849) [spring-tx-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:826) [spring-tx-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.test.context.transaction.TransactionContext.endTransaction(TransactionContext.java:125) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:224) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:297) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:90) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163) [spring-test-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) [.cp/:na]
11:50:17.411 [main] INFO c.a.i.imp.CompositeTransactionImp - rollback() done of transaction 172.18.3.125.tm0000100096
11:50:17.411 [main] INFO o.s.t.c.t.TransactionContext - Rolled back transaction for test context [DefaultTestContext@2606b8 testClass = EmployeeRepositoryTest, testInstance = demo.EmployeeRepositoryTest@831e35, testMethod = saveBoth@EmployeeRepositoryTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@346762 testClass = EmployeeRepositoryTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
11:50:17.411 [main] DEBUG o.s.t.c.s.DirtiesContextTestExecutionListener - After test method: context [DefaultTestContext@2606b8 testClass = EmployeeRepositoryTest, testInstance = demo.EmployeeRepositoryTest@831e35, testMethod = saveBoth@EmployeeRepositoryTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@346762 testClass = EmployeeRepositoryTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], class dirties context [false], class mode [null], method dirties context [false].
11:50:17.421 [main] DEBUG o.s.t.c.s.DirtiesContextTestExecutionListener - After test class: context [DefaultTestContext@2606b8 testClass = EmployeeRepositoryTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@346762 testClass = EmployeeRepositoryTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], dirtiesContext [false].
11:50:17.422 [Thread-1] INFO o.s.c.s.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@1a1732d: startup date [Wed Mar 04 11:50:15 CET 2015]; root of context hierarchy
11:50:17.422 [Thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
11:50:17.422 [Thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1865381: defining beans [org.springframework.context.config.internalBeanConfigurerAspect,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,storeServiceImpl,org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension#0,org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor,foo,jpaMappingContext,employeeARepository,employeeBRepository,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager,dataSourceA,dataSourceB,entityManagerFactoryA,entityManagerFactoryB,atomikosTransactionManager,atomikosUserTransaction,setMyAtomikosSystemProps,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor,org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1]; root of factory hierarchy
我们使用的是Springboot 1.2.2,Atomikos 3.9.3,Hibernate 4.3.7和SQL Server 2008.
我们的大多数配置都在persistence.xml中:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="PersistenceUnitA"
transaction-type="JTA">
<class>demo.entities.employeea.EmployeeA</class>
<properties>
<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="PersistenceUnitB"
transaction-type="JTA">
<class>demo.entities.employeeb.EmployeeB</class>
<properties>
<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>
在applicatioContext.xml中:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
// xmlns configuration
<context:spring-configured />
<context:component-scan base-package="demo.entities" />
<context:component-scan base-package="demo.service" />
<jpa:repositories base-package="demo.repositories.employeea"
entity-manager-factory-ref="entityManagerFactoryA" />
<jpa:repositories base-package="demo.repositories.employeeb"
entity-manager-factory-ref="entityManagerFactoryB" />
<tx:annotation-driven />
<tx:jta-transaction-manager />
<bean id="dataSourceA" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>DataSourceA</value>
</property>
<property name="xaDataSourceClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerXADataSource</value>
</property>
<property name="xaProperties">
<props>
<prop key="databaseName">bbdd1</prop>
<prop key="serverName">localhost</prop>
<prop key="user">****</prop>
<prop key="password">****</prop>
</props>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
</bean>
<bean id="dataSourceB" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>DataSourceB</value>
</property>
<property name="xaDataSourceClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerXADataSource</value>
</property>
<property name="xaProperties">
<props>
<prop key="databaseName">bbdd2</prop>
<prop key="serverName">localhost</prop>
<prop key="user">****</prop>
<prop key="password">****</prop>
</props>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
</bean>
<bean id="entityManagerFactoryA"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation">
<value>classpath*:persistence.xml</value>
</property>
<property name="persistenceUnitName" value="PersistenceUnitA" />
<property name="dataSource" ref="dataSourceA" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
</bean>
</property>
</bean>
<bean id="entityManagerFactoryB"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation">
<value>classpath*:persistence.xml</value>
</property>
<property name="persistenceUnitName" value="PersistenceUnitB" />
<property name="dataSource" ref="dataSourceB" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
</bean>
</property>
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" depends-on="setMyAtomikosSystemProps"
init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" depends-on="setMyAtomikosSystemProps">
<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>
<bean id="setMyAtomikosSystemProps"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<!-- System.getProperties() -->
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System" />
<property name="targetMethod" value="getProperties" />
</bean>
</property>
<property name="targetMethod" value="putAll" />
<property name="arguments">
<!-- The new Properties -->
<util:properties>
<prop key="com.atomikos.icatch.file">classpath:jta.properties</prop>
<prop key="com.atomikos.icatch.hide_init_file_path">true</prop>
</util:properties>
</property>
</bean>
我的@Repositories是空的,它们只是扩展了JpaRepository,实际上它们只是接口。
我的测试班:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
//@SpringApplicationConfiguration(classes = MainConfig.class)
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class EmployeeRepositoryTest {
@Autowired
private EmployeeARepository employeeARepository;
@Autowired
private EmployeeBRepository employeeBRepository;
@Autowired
private StoreService storeService;
@Test
public void saveBoth() {
EmployeeA c = new EmployeeA();
c.setId("101");
c.setName("test-name");
c.setAge(30);
EmployeeB b = new EmployeeB();
b.setId("102");
b.setName("test-name");
b.setAge(31);
try {
storeService.store(c, b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*Asserts*/
}
...
我在帖子的限制内,所需的任何其他信息都会被记录下来。
为什么交易似乎没有加入Hibernate的任何建议?
谢谢。