温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringMVC中事务可不可以加在Controller层

发布时间:2022-02-28 09:29:13 来源:亿速云 阅读:170 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关SpringMVC中事务可不可以加在Controller层,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

SpringMVC中事务是否可以加在Controller层

一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以。

我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找到有力的论据来支持我这个想法,搞得我一度认为事务只能加在Service层,直到我读过spring官方文档并实践之后,我知道我的想法是对的。

在spring-framework-reference.pdf文档中有这样一段话:

<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services. 

这句话的意思是,<tx:annoation-driven/>只会查找和它在相同的应用上下文件中定义的bean上面的@Transactional注解,如果你把它放在Dispatcher的应用上下文中,它只检查控制器上的@Transactional注解,而不是你services上的@Transactional注解。

于是,我将事务配置定义在Spring MVC的应用上下文(*-servlet.xml)中,将@Transactional注解打在Controller上,终于事务起作用了。

综上,在Spring MVC中,事务不仅可以加在Service层,同样也可以加在Controller层(虽然不推荐这么做,但至少满足了我的好奇心,(*^__^*) 嘻嘻&hellip;&hellip;)。

记录一下自己的情况,当时是相当于二次开发,什么都是配置好的。但是很坑的是只有一个controller层,当时也没觉得什么,就跟着在controller里面写。结果报错之后发现事务没有回滚,这就很尴尬了。一检查,配置文件里面配置了事务,注解也是写了的,一脸懵。

读了这篇文章后发现,自己的事务是配置在spring的配置文件(一般都是配置在这里),但是我只有controller层,那就得配置到spring-mvc的配置文件里面,换了配置后就好了。

另外,项目是配置了双数据源。这里在记录一下。  

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManagerMS"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceMS" />
</bean>
 
<!-- 可通过注解控制事务 -->
<tx:annotation-driven transaction-manager="transactionManagerMS" />
 
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
 
<!-- 可通过注解控制事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

用的时候通过注解Transactional 就行了。但是一个controller里面涉及到两个数据库的事务的话就只能手动开启事务了 

Spring在Controller层的事务操作

以下是代码

package cn.hr.controller;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.hr.base.action.BaseAction;
import cn.hr.service.IAnnualbonuService;
@Controller
@RequestMapping(value="/demoTest")
public class DemoTestController extends BaseAction<Object>{
    @Autowired
    private IAnnualbonuService annualbonuService;
    @Autowired
    private PlatformTransactionManager transactionManager;
    /**
      * 
      * @param payNamelist
      * @param request
      * @param response
      * @param session
      */
    @RequestMapping(value = "/deletePayNamelist",method = RequestMethod.POST)
    public void deletePayNamelist(HttpServletRequest request, HttpServletResponse response,HttpSession session) {
        PrintWriter out = null;
        TransactionStatus status = this.transaction();
        try {
            //=====================业务逻辑处理地方================================
            out=response.getWriter();
            out.write("0");
            out.flush();
            transactionManager.commit(status);
        } catch (Exception e) {
            transactionManager.rollback(status);
            out.write("");
            out.flush();
            logger.error(e);
        }finally{     
            out.flush();
            out.close();
       }
    }
    private TransactionStatus transaction(){
        DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
        defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(defaultTransactionDefinition);
        return status;
    }
}

主要是transaction这个方法,意思是:new 一个新的事务,再设置自己所需要的事务隔离级别,最后通过注入的transactionManager得到该事务即可。

关于“SpringMVC中事务可不可以加在Controller层”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI