各位大神好:
我用的是spring mvc框架+hibernate。然后我用的是@Transactional注解处理事务,
问题是这样的:
我有一个service处理业务,然后在dao中进行数据库update,我的@Transactional在
dao中注解的(配置在dao中),service中没有任何事务的配置。现在的是问题是非要service
方法中的代码执行完才会提交事务。各位大神帮我分析一下原因,不胜感谢!
spring配置文件中是不是配有aop
你需要在spring的配置文件配置事务管理器,
对所要拦截的方法进行设定.你的service的方法需要按照配置的起名要求起名并被spring管理.
给你一个例子,按照你的例子你的service方法需要按照saveXXX之类的进行命名:
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config>
<aop:pointcut expression="execution(public * com.lanyuan.service.*.*Impl.*(..))"
id="pointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="logicDelById" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
我用的是注解方式配置的事务
很简单的道理,假如你的add和delete方法在一个方法里面执行的,照你的意思我执行完了一个add就提交一次的话,
那么如果中间一旦出现异常delete还执行吗?就跟银行的转账一样,转账是一个业务,执行了delete你就想提交一旦
方法中间出了异常另外一个人加不上钱 不得亏死