关于 spring batch 和 hibernate 事务冲突的问题

2019-06-26 10:49:44 +08:00
 sodadev

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: txManager,batchtxManager at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:365) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331) at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:370) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:271) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 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.$Proxy77.sendAmqpPacket1(Unknown Source) 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.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

2970 次点击
所在节点    Java
8 条回复
sodadev
2019-06-26 10:50:43 +08:00
大概意思是扫描到两个 org.springframework.transaction.PlatformTransactionManager 类型的 bean 是这个意思吗
manr
2019-06-26 11:03:47 +08:00
expected single matching bean but found 2: txManager,batchtxManager
重复扫描了吧,看看这两个 bean 配置
airfling
2019-06-26 11:05:49 +08:00
你要去除 hibernate 的事务配置,或者去除 batch 的事务配置就可以了
sodadev
2019-06-26 11:06:25 +08:00
<!-- spring batch 配置开始 -->
<bean id="batchtxManager" lazy-init="true"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
p:dataSource-ref="dataSource"><!-- 用于查询所有 job 实例 -->
</bean>
<batch:job-repository id="jobRepository"
data-source="dataSource"
transaction-manager="batchtxManager"
isolation-level-for-create="SERIALIZABLE"
table-prefix="BATCH_"
/>
<task:executor id="executor"></task:executor><!-- 异步 -->
<bean id="jobLauncherAsyn" class="com.javaoa.batch.launcher.CustmJobLauncher"><!-- class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> -->
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor" ref="executor"></property>
</bean>
<batch:job id="restartSynchronizedJob" restartable="true">
<batch:step id="restartSynchronizedStep" >
<batch:tasklet transaction-manager="txManager">
<batch:chunk reader="itemReader" writer="itemWriter" commit-interval="1" retry-policy="alwaysRetryPolicy">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="alwaysRetryPolicy" class="org.springframework.retry.policy.AlwaysRetryPolicy" />
<bean id="itemReader"
class="com.javaoa.batch.read.CustmGwjhItemRead" scope="step">
<property name="sessionFactory" ref="sessionFactory" />
<property name="sendUuid" value="#{jobParameters['sUuid']}"/>
<property name="inceptId" value="#{jobParameters['inceptId']}"/>
<property name="replyId" value="#{jobParameters['rpId']}"/>
<property name="bmId" value="#{jobParameters['bmUuid']}"/>
<property name="inceptDeptIds" value="#{jobParameters['inceptDeptIds']}"/>
<property name="sendType" value="#{jobParameters['type']}"/>
<property name="hqYeWuUuid" value="#{jobParameters['hqYWUuid']}"/>
<property name="hqDwUuid" value="#{jobParameters['hqDanweiUuid']}"/>
<property name="proccessInst" value="#{jobParameters['proccessInstUuid']}"/>
<property name="activityInstId" value="#{jobParameters['activityInstId']}"/>
<property name="jblUuid" value="#{jobParameters['jblUuid']}"/>
<!-- <property name="delegate" ref="hibernateCursorReader"/> -->
</bean>

<bean id="itemWriter" class="com.javaoa.batch.writer.CustmWriter" scope="step">
<property name="sessionFactory" ref="sessionFactory" />
<property name="sendUuid" value="#{jobParameters['sUuid']}"/>
<property name="inceptId" value="#{jobParameters['inceptId']}"/>
<property name="sendType" value="#{jobParameters['type']}"/>
<property name="hqYeWuUuid" value="#{jobParameters['hqYWUuid']}"/>
<property name="hqDwUuid" value="#{jobParameters['hqDanweiUuid']}"/>
<property name="proccessInst" value="#{jobParameters['proccessInstUuid']}"/>
<property name="activityInstId" value="#{jobParameters['activityInstId']}"/>
<property name="jblUuid" value="#{jobParameters['jblUuid']}"/>
</bean>

<!-- spring batch 配置结束 -->
sodadev
2019-06-26 11:07:13 +08:00
<!-- 开启注解事务 只对当前配置文件有效 -->
<tx:annotation-driven transaction-manager="txManager"/>

<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="globalRollbackOnParticipationFailure" value="false" />
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="use*" propagation="REQUIRED"/>
<!--hibernate4 必须配置为开启事务 否则 getCurrentSession()获取不到-->
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="count*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
sodadev
2019-06-26 11:14:59 +08:00
@airfling 如果注释掉的话 tomcat 启动不起来 两个都试过了
sodadev
2019-06-26 11:15:47 +08:00
@manr 我想知道如何在配置文件里更改指定的事务管理。。。我只有 class 文件 没有源码
limuyan44
2019-06-26 11:40:46 +08:00
去掉一个不就好了,怎么可能不配置事务 Tomcat 就报错的,不行你试试 springbatch 的配置用 txmanager

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/577566

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX