使用spring事务必须需要注意的问题

介绍事务的文章很多,这里就不做讲解了,本文主要讲解一个使用中应该注意的问题,否则可能会出现数据库连接池连接数再多也会被耗尽的问题,下面看一个典型的配置:

<bean id="xxTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="xxDataSource"/>

</bean>

    <tx:advice id="xxAdvice" transaction-manager="xxTransactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>

    </tx:advice>

    <aop:config>
        <aop:pointcut id="xxPointCut"
                      expression="execution(* xx..*.impl.*.*(..))" />
        <aop:advisor advice-ref="xxAdvice"
                     pointcut-ref="xxPointCut" />
    </aop:config>

笔者这里是传统的springmvc项目,所以事务配置采用的是xml配置,这样就会导致出现一个问题,所有切点都会被事务环绕,即使你的服务跟数据库半毛钱关系都没有,这样一个并发量很大的redis服务被调用时也会因数据库连接池被耗尽而无法执行!


建议:只在有需要的service上配置事务,不需要的地方最好不要!

猜你喜欢

转载自blog.csdn.net/john1337/article/details/80540113