Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修复Atomikos JTA和Hibernate的Spring配置?_Java_Spring_Hibernate_Jpa_Atomikos - Fatal编程技术网

Java 如何修复Atomikos JTA和Hibernate的Spring配置?

Java 如何修复Atomikos JTA和Hibernate的Spring配置?,java,spring,hibernate,jpa,atomikos,Java,Spring,Hibernate,Jpa,Atomikos,如何修复JTA和的测试配置 异常堆栈跟踪: 2017-02-06 16:03:22.944 [TRACE] [main] [org.hibernate.service.internal.AbstractServiceRegistryImpl:224] Initializing service [role=org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform] 2017-02-06 16:03:22.953 [WARN] [ma

如何修复JTA和的测试配置

异常堆栈跟踪:

2017-02-06 16:03:22.944 [TRACE] [main] [org.hibernate.service.internal.AbstractServiceRegistryImpl:224] Initializing service [role=org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform]
2017-02-06 16:03:22.953 [WARN] [main] [org.springframework.context.support.GenericApplicationContext:549] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hudsonTradeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.opessoftware.platinum.oms.webservice.TradeService com.opessoftware.platinum.oms.controller.HudsonTradeController.tradeService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tradeServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.opessoftware.platinum.oms.db.OrderManagementSystemDao com.opessoftware.platinum.oms.webservice.TradeServiceImpl.orderDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderManagementSystemDaoJpa': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.opessoftware.platinum.oms.configuration.PersistenceConfiguration: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
2017-02-06 16:03:22.964 [ERROR] [main] [org.springframework.test.context.TestContextManager:231] Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@a96d56c] to prepare test instance [com.opessoftware.platinum.oms.controller.CancelServiceImplTest@6ab4a5b]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    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:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hudsonTradeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.opessoftware.platinum.oms.webservice.TradeService com.opessoftware.platinum.oms.controller.HudsonTradeController.tradeService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tradeServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.opessoftware.platinum.oms.db.OrderManagementSystemDao com.opessoftware.platinum.oms.webservice.TradeServiceImpl.orderDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderManagementSystemDaoJpa': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.opessoftware.platinum.oms.configuration.PersistenceConfiguration: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:778)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:843)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 31 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.opessoftware.platinum.oms.webservice.TradeService com.opessoftware.platinum.oms.controller.HudsonTradeController.tradeService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tradeServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.opessoftware.platinum.oms.db.OrderManagementSystemDao com.opessoftware.platinum.oms.webservice.TradeServiceImpl.orderDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderManagementSystemDaoJpa': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.opessoftware.platinum.oms.configuration.PersistenceConfiguration: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 47 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tradeServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.opessoftware.platinum.oms.db.OrderManagementSystemDao com.opessoftware.platinum.oms.webservice.TradeServiceImpl.orderDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderManagementSystemDaoJpa': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.opessoftware.platinum.oms.configuration.PersistenceConfiguration: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1021)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 49 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.opessoftware.platinum.oms.db.OrderManagementSystemDao com.opessoftware.platinum.oms.webservice.TradeServiceImpl.orderDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderManagementSystemDaoJpa': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.opessoftware.platinum.oms.configuration.PersistenceConfiguration: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 60 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderManagementSystemDaoJpa': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.opessoftware.platinum.oms.configuration.PersistenceConfiguration: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:357)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1021)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 62 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.opessoftware.platinum.oms.configuration.PersistenceConfiguration: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:575)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:541)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:707)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:680)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:354)
    ... 73 common frames omitted
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:953)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:883)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:340)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:319)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
    ... 86 common frames omitted
Caused by: java.lang.NullPointerException: null
    at org.hibernate.resource.transaction.backend.jta.internal.DdlTransactionIsolatorJtaImpl.<init>(DdlTransactionIsolatorJtaImpl.java:34)
    at org.hibernate.tool.schema.internal.Helper.buildDefaultDdlTransactionIsolator(Helper.java:145)
    at org.hibernate.tool.schema.internal.HibernateSchemaManagementTool.getDdlTransactionIsolator(HibernateSchemaManagementTool.java:162)
    at org.hibernate.tool.schema.internal.HibernateSchemaManagementTool.buildGenerationTargets(HibernateSchemaManagementTool.java:122)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:109)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:137)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:65)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:459)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:880)
    ... 91 common frames omitted
JPA配置:

@Configuration
@EnableTransactionManagement
public class AtomikosJtaTransactionManagerConfiguration {

    @Autowired
    private ApplicationEnvironmentProperties applicationProperties;

    @Bean
    public UserTransaction userTransaction() throws Throwable {
        final UserTransactionImp userTransaction = new UserTransactionImp();
        userTransaction.setTransactionTimeout(1000);

        AtomikosJtaPlatform.transaction = userTransaction;

        return userTransaction;
    }

    @Bean(initMethod = "init", destroyMethod = "close")
    public TransactionManager transactionManager() throws Throwable {
        final UserTransactionManager userTransactionManager = new UserTransactionManager();
        userTransactionManager.setForceShutdown(false);

        AtomikosJtaPlatform.transactionManager = userTransactionManager;

        return userTransactionManager;
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager(TransactionManager transactionManager,
            UserTransaction userTransaction) throws Throwable {
        return new JtaTransactionManager(userTransaction, transactionManager);
    }

    @Bean
    public XAConnectionFactory xaConnectionFactory(RedeliveryPolicy redeliveryPolicy) {
        final ActiveMQXAConnectionFactory xaConnectionFactory = new ActiveMQXAConnectionFactory();
        xaConnectionFactory.setBrokerURL(this.applicationProperties.getJmsBrokerUrl());
        xaConnectionFactory.setPassword(this.applicationProperties.getJmsBrokerPassword());
        xaConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
        xaConnectionFactory.setTrustAllPackages(true);
        xaConnectionFactory.setUserName(this.applicationProperties.getJmsBrokerUserName());
        return xaConnectionFactory;
    }

    @Bean(initMethod = "init", destroyMethod = "close")
    public ConnectionFactory connectionFactory(XAConnectionFactory xaConnectionFactory) {
        final AtomikosConnectionFactoryBean atomikosConnectionFactory = new AtomikosConnectionFactoryBean();
        atomikosConnectionFactory.setUniqueResourceName("xamq");
        atomikosConnectionFactory.setLocalTransactionMode(false);
        atomikosConnectionFactory.setXaConnectionFactory(xaConnectionFactory);

        final PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
        pooledConnectionFactory.setMaxConnections(3);
        pooledConnectionFactory.setConnectionFactory(new CachingConnectionFactory(atomikosConnectionFactory));
        return atomikosConnectionFactory;
    }
}

public class AtomikosJtaPlatform extends AbstractJtaPlatform {

    private static final long serialVersionUID = 7364400174741027431L;

    static UserTransaction transaction;

    static TransactionManager transactionManager;

    @Override
    protected TransactionManager locateTransactionManager() {
        return transactionManager;
    }

    @Override
    protected UserTransaction locateUserTransaction() {
        return transaction;
    }
}
@Configuration
@Import({CommonConfiguration.class})
public class PersistenceConfiguration {

    @Autowired
    private ApplicationEnvironmentProperties applicationProperties;

    @Autowired
    private HibernateProperties hibernateProperties;

    @Bean
    public DataSource derbyDataSource() {
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
        dataSource.setPassword(this.applicationProperties.getJdbcConnectionPassword());
        dataSource.setUsername(this.applicationProperties.getJdbcConnectionUserName());
        dataSource.setUrl(this.applicationProperties.getJdbcConnectionUrl());
        return dataSource;
    }

    @Bean
    public AbstractEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
            JpaVendorAdapter jpaVendorAdapter) {
        final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactory.setPackagesToScan("com.opessoftware.platinum.model");
        entityManagerFactory.setJpaProperties(this.hibernateProperties.getProperties());
        return entityManagerFactory;
    }

    @Bean
    public JpaVendorAdapter hibernateJpaVendorAdapter() {
        final HibernateJpaVendorAdapter jpaAdapter = new HibernateJpaVendorAdapter();
        jpaAdapter.setShowSql(true);
        jpaAdapter.setGenerateDdl(true);
        return jpaAdapter;
    }

    @Bean
    public OrderManagementSystemDao orderManagementSystemDaoJpa() {
        return new OrderManagementSystemDaoJpa();
    }
}
Atomikos的Hibernate配置(属性):

public class AtomikosJtaHibernateConfiguration {
    @Bean
    public HibernateProperties hibernateProperties() {
        return new AtomikosJtaHibernateProperties(new DefaultHibernateProperties());
    }
}
休眠属性:

public class AtomikosJtaHibernateProperties implements HibernateProperties {

        private final HibernateProperties hibernateProperties;

        public AtomikosJtaHibernateProperties(HibernateProperties defaultHibernateProperties) {
            this.hibernateProperties = defaultHibernateProperties;
            this.hibernateProperties.getProperties().setProperty("hibernate.transaction.jta.platform",
                    AtomikosJtaPlatform.class.getName());
            this.hibernateProperties.getProperties().setProperty("javax.persistence.transactionType", "JTA");
            this.hibernateProperties.getProperties().setProperty("hibernate.transaction.factory_class",
                    "org.hibernate.transaction.JTATransactionFactory");
            this.hibernateProperties.getProperties().setProperty("hibernate.transaction.manager_lookup_class",
                    "com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup");
        }

        @Override
        public Properties getProperties() {
            return this.hibernateProperties.getProperties();
        }
    }

public class DefaultHibernateProperties implements HibernateProperties {

    private final Properties hibernateProperties;

    public DefaultHibernateProperties() {
        this.hibernateProperties = new Properties();
        this.hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
        this.hibernateProperties.setProperty("hibernate.format_sql", "true");
        this.hibernateProperties.setProperty("hibernate.jdbc.fetch_size", "50");
        this.hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create");
    }

    @Override
    public Properties getProperties() {
        return this.hibernateProperties;
    }
}
jdbc.ConnectionDatabaseName=testdb
jdbc.ConnectionPassword=
jdbc.ConnectionPortNumber=0
jdbc.ConnectionServerName=memory
jdbc.ConnectionUrl=jdbc:derby:memory:testdb;create=true
jdbc.ConnectionUserName=sa
JDBC连接属性:

public class AtomikosJtaHibernateProperties implements HibernateProperties {

        private final HibernateProperties hibernateProperties;

        public AtomikosJtaHibernateProperties(HibernateProperties defaultHibernateProperties) {
            this.hibernateProperties = defaultHibernateProperties;
            this.hibernateProperties.getProperties().setProperty("hibernate.transaction.jta.platform",
                    AtomikosJtaPlatform.class.getName());
            this.hibernateProperties.getProperties().setProperty("javax.persistence.transactionType", "JTA");
            this.hibernateProperties.getProperties().setProperty("hibernate.transaction.factory_class",
                    "org.hibernate.transaction.JTATransactionFactory");
            this.hibernateProperties.getProperties().setProperty("hibernate.transaction.manager_lookup_class",
                    "com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup");
        }

        @Override
        public Properties getProperties() {
            return this.hibernateProperties.getProperties();
        }
    }

public class DefaultHibernateProperties implements HibernateProperties {

    private final Properties hibernateProperties;

    public DefaultHibernateProperties() {
        this.hibernateProperties = new Properties();
        this.hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
        this.hibernateProperties.setProperty("hibernate.format_sql", "true");
        this.hibernateProperties.setProperty("hibernate.jdbc.fetch_size", "50");
        this.hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create");
    }

    @Override
    public Properties getProperties() {
        return this.hibernateProperties;
    }
}
jdbc.ConnectionDatabaseName=testdb
jdbc.ConnectionPassword=
jdbc.ConnectionPortNumber=0
jdbc.ConnectionServerName=memory
jdbc.ConnectionUrl=jdbc:derby:memory:testdb;create=true
jdbc.ConnectionUserName=sa
软件配置:

@Configuration
@EnableTransactionManagement
public class AtomikosJtaTransactionManagerConfiguration {

    @Autowired
    private ApplicationEnvironmentProperties applicationProperties;

    @Bean
    public UserTransaction userTransaction() throws Throwable {
        final UserTransactionImp userTransaction = new UserTransactionImp();
        userTransaction.setTransactionTimeout(1000);

        AtomikosJtaPlatform.transaction = userTransaction;

        return userTransaction;
    }

    @Bean(initMethod = "init", destroyMethod = "close")
    public TransactionManager transactionManager() throws Throwable {
        final UserTransactionManager userTransactionManager = new UserTransactionManager();
        userTransactionManager.setForceShutdown(false);

        AtomikosJtaPlatform.transactionManager = userTransactionManager;

        return userTransactionManager;
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager(TransactionManager transactionManager,
            UserTransaction userTransaction) throws Throwable {
        return new JtaTransactionManager(userTransaction, transactionManager);
    }

    @Bean
    public XAConnectionFactory xaConnectionFactory(RedeliveryPolicy redeliveryPolicy) {
        final ActiveMQXAConnectionFactory xaConnectionFactory = new ActiveMQXAConnectionFactory();
        xaConnectionFactory.setBrokerURL(this.applicationProperties.getJmsBrokerUrl());
        xaConnectionFactory.setPassword(this.applicationProperties.getJmsBrokerPassword());
        xaConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
        xaConnectionFactory.setTrustAllPackages(true);
        xaConnectionFactory.setUserName(this.applicationProperties.getJmsBrokerUserName());
        return xaConnectionFactory;
    }

    @Bean(initMethod = "init", destroyMethod = "close")
    public ConnectionFactory connectionFactory(XAConnectionFactory xaConnectionFactory) {
        final AtomikosConnectionFactoryBean atomikosConnectionFactory = new AtomikosConnectionFactoryBean();
        atomikosConnectionFactory.setUniqueResourceName("xamq");
        atomikosConnectionFactory.setLocalTransactionMode(false);
        atomikosConnectionFactory.setXaConnectionFactory(xaConnectionFactory);

        final PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
        pooledConnectionFactory.setMaxConnections(3);
        pooledConnectionFactory.setConnectionFactory(new CachingConnectionFactory(atomikosConnectionFactory));
        return atomikosConnectionFactory;
    }
}

public class AtomikosJtaPlatform extends AbstractJtaPlatform {

    private static final long serialVersionUID = 7364400174741027431L;

    static UserTransaction transaction;

    static TransactionManager transactionManager;

    @Override
    protected TransactionManager locateTransactionManager() {
        return transactionManager;
    }

    @Override
    protected UserTransaction locateUserTransaction() {
        return transaction;
    }
}
@Configuration
@Import({CommonConfiguration.class})
public class PersistenceConfiguration {

    @Autowired
    private ApplicationEnvironmentProperties applicationProperties;

    @Autowired
    private HibernateProperties hibernateProperties;

    @Bean
    public DataSource derbyDataSource() {
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
        dataSource.setPassword(this.applicationProperties.getJdbcConnectionPassword());
        dataSource.setUsername(this.applicationProperties.getJdbcConnectionUserName());
        dataSource.setUrl(this.applicationProperties.getJdbcConnectionUrl());
        return dataSource;
    }

    @Bean
    public AbstractEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
            JpaVendorAdapter jpaVendorAdapter) {
        final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactory.setPackagesToScan("com.opessoftware.platinum.model");
        entityManagerFactory.setJpaProperties(this.hibernateProperties.getProperties());
        return entityManagerFactory;
    }

    @Bean
    public JpaVendorAdapter hibernateJpaVendorAdapter() {
        final HibernateJpaVendorAdapter jpaAdapter = new HibernateJpaVendorAdapter();
        jpaAdapter.setShowSql(true);
        jpaAdapter.setGenerateDdl(true);
        return jpaAdapter;
    }

    @Bean
    public OrderManagementSystemDao orderManagementSystemDaoJpa() {
        return new OrderManagementSystemDaoJpa();
    }
}
  • Atomikos 4.0.4
  • Hibernate 5.1.3.3最终版本
  • 弹簧4.2.9.1释放
  • OpenJDK 1.8.0_121
参考资料:


在对我创建的一个名为的示例项目进行了大量的研究和实验之后,我终于成功地配置了Spring JPA、Atomikos JTA、Hibernate和嵌入式HSQLDB服务器。注意,在中,单元测试引入了需要的,因此JPA配置不需要事务管理器bean的
@DependsOn
注释。但是,我确实必须添加
@DependsOn({“brokerService”})
,以强制Spring创建嵌入式ActiveMQ代理:

@Configuration
@Import({AtomikosJtaHsqlDatabaseConfiguration.class, AtomikosJtaTransactionManagerConfiguration.class,
    CommonConfiguration.class, JpaConfiguration.class, MessageBrokerConfiguration.class, ServiceConfiguration.class,
    TransactionTemplateConfiguration.class})
@DependsOn({"brokerService"})
public class GlobalTransactionManagerConfiguration {
}
请注意,中的
EntityManagerFactory
实例设置了非JTA数据源,而不是JTA数据源,该数据源确认了my(并与之相矛盾),至少在这种情况下,仍然可以使用
setDataSource
而不是
setJtaDataSource

@Bean
    public AbstractEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
            JpaVendorAdapter jpaVendorAdapter) {
        final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactory.setPackagesToScan("me.derekmahar.example.spring.model");
        entityManagerFactory.setJpaProperties(this.hibernateProperties.getProperties());
        return entityManagerFactory;
    }
虽然只是警告,但每个单元测试都会生成恼人的
TransportDisposedIOException
异常,我还不知道如何消除这些异常:

org.apache.activemq.transport.TransportDisposedIOException: peer (vm://test#3) stopped.

这是hibernate3所需要的:

this.hibernateProperties.getProperties().setProperty(“hibernate.transaction.manager_lookup_class”,“com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup”)

对于hibernate5(或4)(适合我):

发件人:


很难说是什么错,但我很确定
AtomikosJtaPlatform
应该是一个bean,我看不出您在哪里创建它。
AtomikosJtaPlatform
的成员是
public static
。不确定这有什么关系,您失败的代码行似乎是,并且可能发生在
getService(JtaPlatform.class)
返回null。您可以在该行上设置断点并准确知道哪部分为null。将代码与比较,我发现您使用的是
UserTransactionImp
而不是
J2eeUserTransaction
,并在
LocalContainerEntityManagerFactoryBean上调用
setDataSource
而不是
setJtaDataSource
ode>@manish,是的,因为您的应用程序必须在Java应用程序容器中运行,而我的应用程序是独立的Java应用程序。