Transaction Management in the form of annotations in the SSM framework

Source: Internet
Author: User

The previous blog "SSM three major framework integration detailed tutorial" details how to integrate the three frameworks spring, springmvc, and mybatis. But I didn't talk about how to configure the transaction management of mybatis. In development, transactions are essential. As a supplement to the previous article, this article describes how to use annotations to manage transactions in the SSM framework.


What is a transaction?

During business writing, transactions are required. If you need to execute multiple insert statements, if the first few statements are successful and the last one fails, therefore, we need to roll back the database operation to maintain data consistency and integrity. At this time, we need to use dB transaction processing. Transactions are the basic unit of recovery and concurrency control.


Simply put, a transaction is a sequence of operations. These operations are either executed or not executed. It is an inseparable unit of work.


Transactions should have four attributes: atomicity, consistency, isolation, and persistence. These four attributes are generally called ACID properties.


Atomicity ). A transaction is an inseparable unit of work. All operations involved in a transaction are either done or not done.


Consistency ). Transactions must change the database from one consistent state to another. Consistency is closely related to atomicity.


Isolation ). The execution of a transaction cannot be disturbed by other transactions. That is to say, the operations and data used within a transaction are isolated from other concurrent transactions, and the transactions executed concurrently cannot interfere with each other.


Durability ). Permanence refers to a transaction that changes the data in the database once committed. Other subsequent operations or faults should not have any impact on them.


Mybatis integrates Spring transaction management

In the SSM framework, spring's transaction management mechanism is used. Spring can implement transactions programmatically, declarative transactions, and annotated transactions. This article describes how to use annotation @ transanctional to implement transaction management.


The code example in this article is based on the previous blog post. The specific code "SSM three frameworks integration detailed tutorial" has been given. A simple look at the directory structure and object class:



1. Configure the spring-mybatis.xml File

To implement transaction management in the form of annotations, you only need to add the following code to the configuration file:


<! -- Enable transaction annotation driver --> <TX: annotation-driven/> <! -- (Transaction Management) Transaction Manager, use jtatransactionmanager for global TX --> <bean id = "transactionmanager" class = "org. springframework. JDBC. datasource. datasourcetransactionmanager "> <property name =" datasource "ref =" datasource "/> </bean>

Of course, if the XML file reports an error at this time, it is caused by the absence of xmlns and schema, and the document structure cannot be identified. You just need to introduce the header file. Here is my file, which can be introduced as needed:


<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xsi:schemaLocation="http://www.springframework.org/schema/beans                          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd                          http://www.springframework.org/schema/context                          http://www.springframework.org/schema/context/spring-context-3.1.xsd                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                          http://www.springframework.org/schema/mvc                          http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">



2. How to Use


Here is a small example to test whether transaction management is successfully configured. The basis of the Code is the test code in the SSM framework. The test method is as follows: I want to insert a set of user objects. If the number of objects is less than 2, the object can be inserted successfully, but if the number is greater than 2, then an exception is thrown (the transaction processing must throw an exception, so that spring can help the transaction rollback). In this way, the database will be rolled back without inserting any data. If no data is inserted into the database, the transaction processing configuration is successful.

Note that @ transactional can only be applied to the public method. For other non-public methods, if @ transactional is marked, no error is reported, but the method has no transaction function.


Entity class, Dao interface, business interface, and business implementation are all available. This test only requires you to add a method to the business layer and then use JUnit for testing. Add the following method to the business implementation class, note @ transactional:

/*** The transaction must throw an exception before spring can roll back the transaction * @ Param users */@ transactional @ overridepublic void insertuser (list <user> Users) {// todo auto-generated method stubfor (INT I = 0; I <users. size (); I ++) {if (I <2) {This. userdao. insert (users. get (I) ;}else {Throw new runtimeexception ();}}}

Next, add the following method to the test class for testing:


@ Testpublic void testtransaction () {list <user> Users = new arraylist <user> (); For (INT I = 1; I <5; I ++) {user = new user (); User. setage (I); User. setpassword (I + "111111"); User. setusername ("test" + I); users. add (User);} This. userservice. insertuser (users );}


Note: In this case, an error occurs during the JUnit test, because this exception is thrown in the method. In essence, transaction management is performed, and data is not inserted, which indicates that the configuration is successful. Otherwise, if the annotation is removed, the first two pieces of data are inserted successfully, and an exception is thrown later.


(Original address: http://blog.csdn.net/zhshulin)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.