Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Whether transactions in SpringMVC can be added to the Controller layer

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail whether transactions in SpringMVC can be added to the Controller layer. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Whether transactions in SpringMVC can be added to the Controller layer

Generally speaking, transactions are added to the Service layer, but I often wonder whether transactions can be added to the Controller layer.

I have been trying to prove that transactions can be added not only to the Service layer, but also to the Controller layer, but I failed to find a strong argument to support my idea, which led me to think that transactions could only be added to the Service layer until I read the official spring documentation and put it into practice. I knew I was right.

There is a passage in the spring-framework-reference.pdf document:

Only looks for @ Transactional on beans in the same application context it is defined in. This means that, if you put in a WebApplicationContext for a DispatcherServlet, it only checks for @ Transactional beans in your controllers, and not your services.

This means that only the @ Transactional annotation on the bean defined in the same application context will be found. If you put it in the Dispatcher application context, it will only check the @ Transactional annotation on the controller, not the @ Transactional annotation on your services.

So I defined the transaction configuration in the Spring MVC application context (*-servlet.xml), typed the @ Transactional annotation on the Controller, and finally the transaction worked.

To sum up, in Spring MVC, transactions can be added not only to the Service layer, but also to the Controller layer (although this is not recommended, at least it satisfies my curiosity, (* ^ _ ^ *) hee. ).

Record your own situation, at that time is equivalent to secondary development, everything is configured. But the crappy thing is that there is only one controller layer, and I didn't feel anything at that time, so I wrote it in controller. It was embarrassing to find that the transaction had not been rolled back after reporting an error. As soon as I checked, there were transactions configured in the configuration file, and the comments were also written, looking confused.

After reading this article, I found that my transaction is configured in the spring configuration file (usually configured here), but I only have the controller layer, so I have to configure it to the spring-mvc configuration file.

In addition, the project is configured with dual data sources. Here, make a record.

When you use it, just annotate Transactional. But if two database transactions are involved in a controller, you can only start the transaction manually.

The transaction operation of Spring at the Controller layer is the following code 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 {@ 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 {/ / = Business logic processing place = 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;}}

It is mainly the transaction method, which means: new a new transaction, then set the transaction isolation level you need, and finally get the transaction through the injected transactionManager.

This is the end of the article on "whether transactions in SpringMVC can be added to the Controller layer". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report