V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sodadev
V2EX  ›  Java

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

  •  
  •   sodadev · 2019-06-26 10:49:44 +08:00 · 2981 次点击
    这是一个创建于 1983 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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)

    8 条回复    2019-06-26 11:40:46 +08:00
    sodadev
        1
    sodadev  
    OP
       2019-06-26 10:50:43 +08:00
    大概意思是扫描到两个 org.springframework.transaction.PlatformTransactionManager 类型的 bean 是这个意思吗
    manr
        2
    manr  
       2019-06-26 11:03:47 +08:00
    expected single matching bean but found 2: txManager,batchtxManager
    重复扫描了吧,看看这两个 bean 配置
    airfling
        3
    airfling  
       2019-06-26 11:05:49 +08:00
    你要去除 hibernate 的事务配置,或者去除 batch 的事务配置就可以了
    sodadev
        4
    sodadev  
    OP
       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
        5
    sodadev  
    OP
       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
        6
    sodadev  
    OP
       2019-06-26 11:14:59 +08:00
    @airfling 如果注释掉的话 tomcat 启动不起来 两个都试过了
    sodadev
        7
    sodadev  
    OP
       2019-06-26 11:15:47 +08:00
    @manr 我想知道如何在配置文件里更改指定的事务管理。。。我只有 class 文件 没有源码
    limuyan44
        8
    limuyan44  
       2019-06-26 11:40:46 +08:00 via Android
    去掉一个不就好了,怎么可能不配置事务 Tomcat 就报错的,不行你试试 springbatch 的配置用 txmanager
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2923 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 11:29 · PVG 19:29 · LAX 03:29 · JFK 06:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.