sur*_*one 1 spring spring-transactions mybatis
在"applicationContext-base.xml"中我添加如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
>
.....
<!-- transaction support-->
<!-- PlatformTransactionMnager -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- enable transaction annotation support -->
<tx:annotation-driven transaction-manager="txManager" />
Run Code Online (Sandbox Code Playgroud)
我想在"控制器"中为一个函数设置一个事务
@Controller
@RequestMapping("/ywdata")
public class YwController extends BaseController{
....
@Transactional(timeout=1)
private void submitNewSXSQ(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception {
...//STEP1 :do some db insert and update STEP1
if(true)
throw new Exception("test transaction ");
...//STEP2: do another db insert and update
Run Code Online (Sandbox Code Playgroud)
我期望db操作永远不应该提交,因为我在返回之前抛出异常.但实际上不是.
您的代码存在多个问题:
@Transactional 私有方法不起作用@Transactional在带@Controller注释的类上通常不起作用最后一个问题很容易理解.让我解释前两个问题.Spring中的AOP是这样的:
为什么私有方法有问题:
@Transactional如果你有@Transactional非接口方法(代理上只有接口方法)将无法工作@@Transactional如果你有@Transactional私有或最终方法将无法工作(只有非私有和非最终方法可以在动态子类中重写)为什么控制器有问题:
RequestMappingHandlerMapping(负责将请求映射到您的bean @Controllers)要求应用程序上下文获取带@Controller注释的
所有bean该怎么办:
OpenSessionInViewFilter.TransactionTemplate帮助程序类.| 归档时间: |
|
| 查看次数: |
2615 次 |
| 最近记录: |