Transaction management is implemented in the form of annotations in the SSM framework

 

What is a transaction?

In the process of writing a business, transaction processing will be required. When multiple insert statements need to be executed, if the first few succeed and the last one fails, then we need to roll back the database operation to maintain the consistency and integrity of the data. In this case, it is necessary to use the transaction processing of the DB. A transaction is the basic unit of recovery and concurrency control.

        Simply put, the so-called transaction is a sequence of operations, which are either executed or not executed, and it is an indivisible unit of work.

Transactions should have 4 properties: atomicity, consistency, isolation, and durability. These four properties are often referred to as ACID properties.


        Atomicity. A transaction is an inseparable unit of work, and the operations included in the transaction are either done or not done.


        Consistency. A transaction must change the database from one consistent state to another. Consistency and atomicity are closely related.


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


        Durability. Persistence, also known as permanence, means that once a transaction is committed, its changes to the data in the database should be permanent. Other operations or failures that follow should not have any effect on it.

MyBatis integrates Spring transaction management

In the SSM framework, Spring's transaction management mechanism is used. Spring can implement transactions programmatically, declaratively, and annotatively. This article mainly talks about how to use the annotation @Transanctional to achieve transaction management.

The code example in this article is based on the previous blog post, and the specific code   has been given in the "Detailed Tutorial on the Integration of the Three SSM Frameworks" . Just look at the directory structure and entity classes:

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:

<!-- 开启事务注解驱动 -->
<tx:annotation-driven />
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

           

当然,如果此时xml文件报错,那是由于没有引入xmlns和schema导致的,无法识别文档结构。引入头文件即可,以下是我的,根据自己需要引入:

<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、如何使用

在此用一个小例子来测试事务管理是否成功配置。代码基础是 SSM 框架搭建里面的测试代码。我们现在 测试的方法 是:我要插入一个 User 对象的集合,如果此对象数量小于 2 ,那么可以成功插入,但是如果大于 2 ,那么就抛出异常( 事务处理必须抛出异常,只有这样Spring才帮助事务回滚 ),这样数据库就会回滚,不插入任何数据。测试结果如果数据库没插入任何数据,那么表示事务处理配置成功,反正,失败。

      注意@Transactional只能被应用到public方法上 ,对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能。  

        实体类、DAO接口,业务接口,以及业务实现都有,这个测试 仅需要在业务层中添加一个方法,然后使用JUnit测试 即可,业务实现类中添加如下方法, 注意 注解@Transactional :

/**
 * 事务处理必须抛出异常,Spring才会帮助事务回滚
 * @param users
 */

@Transactional
@Override
public void insertUser(List<User> users) {
// TODO Auto-generated method stub
for (int i = 0; i < users.size(); i++) {
if(i<2){
this.userDao.insert(users.get(i));
}
else {
throw new RuntimeException();
}
}
}

接下来在测试类中添加如下方法进行测试:

@Test
public void testTransaction(){
List<User> users = new ArrayList<User>();
for(int i=1;i<5;i++){
User user = new User();
user.setAge(i);
user.setPassword(i+"111111");
user.setUserName("测试"+i);
users.add(user);
}
this.userService.insertUser(users);
}

          注意 :此时进行JUnit测试会发现出现错误,这是因为方法中抛出了这个异常。实质上确实进行了事务管理,数据没有插入,此时表示 配置成功了 ;反之,如果去掉注解,那么前两条数据会插入成功,然后后面会抛出异常。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326977457&siteId=291194637