1

有人可以帮我解决这个错误吗? 或者如何使用JTA Transactionmanager的具体示例代码?如何配置Spring Jta事务?

我正在使用多数据库,主数据库是用Transactionmanager在XML中定义的, 和其余的数据库是用Java文件中的GenericApplicationContext创建的。在XML

的JtaTransactionManager设置是这样的

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" autowire="constructor"> 
    </bean> 
<tx:annotation-driven/> 

和错误代码..

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/spring/db.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No JTA UserTransaction available - specify either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName' 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) 
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) 
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) 
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791) 
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633) 
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977) 
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1655) 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
at java.lang.Thread.run(Thread.java:722) 
+0

您使用的是什么应用程序服务器?一个JEE或Tomcat/Jetty? – abalogh

+0

我使用Tomcat作为服务器。 – Andrew

+0

什么时候你准确地得到这个错误? – abalogh

回答

0

如果你使用JTA事务管理器,您需要:
1. Web服务器支持JTA,像JBoss,Weblogic。
2.或JTA lib的实现,如atomikos。

所以在你的情况下(使用tomcat),你应该使用JTA实现库。如果您使用的春天开机,你只需要添加它POM,如:

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-jta-atomikos</artifactId> 
    </dependency> 

而且,你并不需要,如果你使用的弹簧引导做任何事情在你的配置。

但是,您需要用XADataSourceWrapper包装您的dataSource。您可以自动装载此属性,并使用xaDataSourceWrapper.wrapDataSource()为您创建一个XA数据源的数据源。