好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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层(虽然不推荐这么做,但至少满足了我的好奇心,(*^__^*) 嘻嘻……)。

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

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<!-- (事务管理)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层的事务操作

以下是代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

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测试数据mit(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得到该事务即可。

百分百生效! 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.51cto测试数据/u_5880861/1544168

查看更多关于SpringMVC中事务是否可以加在Controller层的问题的详细内容...

  阅读:25次