集成Spring事物管理

时间:2023-03-09 01:58:01
集成Spring事物管理

什么是事物

事物是访问数据库的一个操作序列,数据库应用系统通过事物集来完成对数据库的存取。事物的正确执行使得数据库从一种状态转换为另一种状态

事物必须服从ISO/IEC所制定的ACID原则。ACID是原子性(atomicity)、一致性(consistency)、隔离性(isolation)、持久性(durability)的缩写,这四种状态的意思是:

1、原子性

即不可分割,事物要么全部被执行,要么全部不执行。如果事物的所有子事物全部提交成功,则所有的数据库操作被提交,数据库状态发生变化;如果有子事物失败,则其他子事物的数据库操作被回滚,即数据库回到事物执行前的状态,不会发生状态转换

2、一致性

事物的执行使得数据库从一种正确状态转换成另外一种正确状态

3、隔离性

在事物正确提交之前,不允许把事物对该数据的改变提供给任何其他事物,即在事物正确提交之前,它可能的结果不应该显示给其他事物

4、持久性

事物正确提交之后,其结果将永远保存在数据库之中,即使在事物提交之后有了其他故障,事物的处理结果也会得到保存

事物的作用

事物管理对于企业级应用而言至关重要,它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性。就像银行的自动提款机ATM,通常ATM都可以正常为客户服务,但是也难免遇到操作过程中及其突然出故障的情况,此时,事物就必须确保出故障前对账户的操作不生效,就像用户刚才完全没有使用过ATM机一样,以保证用户和银行的利益都不受损失。

并发下事物会产生的问题

举个例子,事物A和事物B操纵的是同一个资源,事物A有若干个子事物,事物B也有若干个子事物,事物A和事物B在高并发的情况下,会出现各种各样的问题。"各种各样的问题",总结一下主要就是五种:第一类丢失更新、第二类丢失更新、脏读、不可重复读、幻读。五种之中,第一类丢失更新、第二类丢失更新不重要,不讲了,讲一下脏读、不可重复读和幻读。

1、脏读

所谓脏读,就是指事物A读到了事物B还没有提交的数据,比如银行取钱,事物A开启事物,此时切换到事物B,事物B开启事物-->取走100元,此时切换回事物A,事物A读取的肯定是数据库里面的原始数据,因为事物B取走了100块钱,并没有提交,数据库里面的账务余额肯定还是原始余额,这就是脏读。

2、不可重复读

所谓不可重复读,就是指在一个事物里面读取了两次某个数据,读出来的数据不一致。还是以银行取钱为例,事物A开启事物-->查出银行卡余额为1000元,此时切换到事物B事物B开启事物-->事物B取走100元-->提交,数据库里面余额变为900元,此时切换回事物A,事物A再查一次查出账户余额为900元,这样对事物A而言,在同一个事物内两次读取账户余额数据不一致,这就是不可重复读。

3、幻读

所谓幻读,就是指在一个事物里面的操作中发现了未被操作的数据。比如学生信息,事物A开启事物-->修改所有学生当天签到状况为false,此时切换到事物B,事物B开启事物-->事物B插入了一条学生数据,此时切换回事物A,事物A提交的时候发现了一条自己没有修改过的数据,这就是幻读,就好像发生了幻觉一样。幻读出现的前提是并发的事物中有事物发生了插入、删除操作。

事物隔离级别

事物隔离级别,就是为了解决上面几种问题而诞生的。为什么要有事物隔离级别,因为事物隔离级别越高,在并发下会产生的问题就越少,但同时付出的性能消耗也将越大,因此很多时候必须在并发性和性能之间做一个权衡。所以设立了几种事物隔离级别,以便让不同的项目可以根据自己项目的并发情况选择合适的事物隔离级别,对于在事物隔离级别之外会产生的并发问题,在代码中做补偿。

事物隔离级别有4种,但是像Spring会提供给用户5种,来看一下:

1、DEFAULT

默认隔离级别,每种数据库支持的事物隔离级别不一样,如果Spring配置事物时将isolation设置为这个值的话,那么将使用底层数据库的默认事物隔离级别。顺便说一句,如果使用的MySQL,可以使用"select @@tx_isolation"来查看默认的事物隔离级别

2、READ_UNCOMMITTED

读未提交,即能够读取到没有被提交的数据,所以很明显这个级别的隔离机制无法解决脏读、不可重复读、幻读中的任何一种,因此很少使用

3、READ_COMMITED

读已提交,即能够读到那些已经提交的数据,自然能够防止脏读,但是无法限制不可重复读和幻读

4、REPEATABLE_READ

重复读取,即在数据读出来之后加锁,类似"select * from XXX for update",明确数据读取出来就是为了更新用的,所以要加一把锁,防止别人修改它。REPEATABLE_READ的意思也类似,读取了一条数据,这个事物不结束,别的事物就不可以改这条记录,这样就解决了脏读、不可重复读的问题,但是幻读的问题还是无法解决

5、SERLALIZABLE

串行化,最高的事物隔离级别,不管多少事物,挨个运行完一个事物的所有子事物之后才可以执行另外一个事物里面的所有子事物,这样就解决了脏读、不可重复读和幻读的问题了

网上专门有图用表格的形式列出了事物隔离级别解决的并发问题:

集成Spring事物管理

再必须强调一遍,不是事物隔离级别设置得越高越好,事物隔离级别设置得越高,意味着势必要花手段去加锁用以保证事物的正确性,那么效率就要降低,因此实际开发中往往要在效率和并发正确性之间做一个取舍,一般情况下会设置为READ_COMMITED,此时避免了脏读,并发性也还不错,之后再通过一些别的手段去解决不可重复读和幻读的问题就好了。

单独使用MyBatis对事物进行管理

前面MyBatis的文章有写过相关内容,这里继续写一个最简单的Demo,算是复习一下之前MyBatis的内容吧,先是建表,建立一个简单的Student表:

create table student
(
student_id int auto_increment,
student_name varchar(20) not null,
primary key(student_id)
)

建立实体类Student.java:

public class Student
{
private int studentId;
private String studentName; public int getStudentId()
{
return studentId;
} public void setStudentId(int studentId)
{
this.studentId = studentId;
} public String getStudentName()
{
return studentName;
} public void setStudentName(String studentName)
{
this.studentName = studentName;
} public String toString()
{
return "Student{[studentId:" + studentId + "], [studentName:" + studentName + "]}";
}
}

多说一句,对实体类重写toString()方法,打印其中每一个(或者说是关键属性)是一个推荐的做法。接着是config.xml,里面是jdbc基本配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<typeAliases>
<typeAlias alias="Student" type="org.xrq.domain.Student" />
</typeAliases> <environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="student_mapper.xml"/>
</mappers>
</configuration>

然后是student_mapper.xml,主要是具体的sql语句:

<mapper namespace="StudentMapper">
<resultMap type="Student" id="StudentMap">
<id column="student_id" property="studentId" jdbcType="INTEGER" />
<result column="student_name" property="studentName" jdbcType="VARCHAR" />
</resultMap> <select id="selectAllStudents" resultMap="StudentMap">
select student_id, student_name from student;
</select> <insert id="insertStudent" useGeneratedKeys="true" keyProperty="studentId" parameterType="Student">
insert into student(student_id, student_name) values(#{studentId, jdbcType=INTEGER}, #{studentName, jdbcType=VARCHAR});
</insert>
</mapper>

建立一个MyBatisUtil.java,用于建立一些MyBatis基本元素的,后面的类都继承这个类:

public class MyBatisUtil
{
protected static SqlSessionFactory ssf;
protected static Reader reader; static
{
try
{
reader = Resources.getResourceAsReader("config.xml");
ssf = new SqlSessionFactoryBuilder().build(reader);
}
catch (IOException e)
{
e.printStackTrace();
}
} protected SqlSession getSqlSession()
{
return ssf.openSession();
}
}

企业级开发讲求:

1、定义和实现分开

2、分层开发,通常情况下为Dao-->Service-->Controller,不排除根据具体情况多一层/几层或少一层

所以,先写一个StudentDao.java接口:

public interface StudentDao
{
public List<Student> selectAllStudents();
public int insertStudent(Student student);
}

最后写一个StudentDaoImpl.java实现这个接口,注意要继承MyBatisUtil.java类

 1 public class StudentDaoImpl extends MyBatisUtil implements StudentDao
2 {
3 private static final String NAMESPACE = "StudentMapper.";
4
5 public List<Student> selectAllStudents()
6 {
7 SqlSession ss = getSqlSession();
8 List<Student> list = ss.selectList(NAMESPACE + "selectAllStudents");
9 ss.close();
10 return list;
11 }
12
13 public int insertStudent(Student student)
14 {
15 SqlSession ss = getSqlSession();
16 int i = ss.insert(NAMESPACE + "insertStudent", student);
17 // ss.commit();
18 ss.close();
19 return i;
20 }
21 }

写一个测试类:

public class StudentTest
{
public static void main(String[] args)
{
StudentDao studentDao = new StudentDaoImpl(); Student student = new Student();
student.setStudentName("Jack"); studentDao.insertStudent(student);
System.out.println("插入的主键为:" + student.getStudentId()); System.out.println("-----Display students------");
List<Student> studentList = studentDao.selectAllStudents();
for (int i = 0, length = studentList.size(); i < length; i++)
System.out.println(studentList.get(i));
}
}

结果一定是空。

我说过这个例子既是作为复习,也是作为一个引子引入我们今天的内容,空的原因是,insert操作已经做了,但是MyBatis并不会帮我们自动提交事物,所以展示出来的自然是空的。这种时候就必须手动通过SqlSession的commit()方法提交事务,即打开StudentDaoImpl.java类第17行的注释就可以了。

多说一句,这个例子除了基本的MyBatis插入操作之外,在插入的基础上还有返回插入的主键id的功能。

接下来,就利用Spring管理MyBatis事物,这也是企业级开发中最常用的事物管理做法。

使用Spring管理MyBatis事物

关于这块,网上有很多文章讲解,我搜索了很多,但是要么就是相互复制黏贴,要么就是没有把整个例子讲清楚的,通过这一部分,我尽量讲清楚如何使用Spring管理MyBatis事物。

使用Spring管理MyBatis事物,除了Spring必要的模块beans、context、core、expression、commons-logging之外,还需要以下内容:

(1)MyBatis-Spring-1.x.0.jar,这个是Spring集成MyBatis必要的jar包

(2)数据库连接池,dbcp、c3p0都可以使用,我这里使用的是阿里的druid

(3)jdbc、tx、aop,jdbc是基本的不多说,用到tx和aop是因为Spring对MyBatis事物管理的支持是通过aop来实现的

(4)aopalliance.jar,这个是使用Spring AOP必要的一个jar包

上面的jar包会使用Maven的可以使用Maven下载,没用过Maven的可以去CSDN上下载,一搜索就有的。

MyBatis的配置文件config.xml里面,关于jdbc连接的部分可以都去掉,只保留typeAliases的部分:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<typeAliases>
<typeAlias alias="Student" type="org.xrq.domain.Student" />
</typeAliases>
</configuration>

多提一句,MyBatis另外一个配置文件student_mapper.xml不需要改动。接着,写Spring的配置文件,我起名字叫做spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- 注解配置 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config />
<context:component-scan base-package="org.xrq" /> <!-- 数据库连接池,这里使用alibaba的Druid -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config.xml" />
<property name="mapperLocations" value="classpath:*_mapper.xml" />
<property name="dataSource" ref="dataSource" />
</bean> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> </beans>

这里面主要就是事务管理器数据库连接池两部分的内容。

另外我们看到有一个SqlSessionFactory它是用于配置MyBatis环境的,SqlSessionFactory里面有两个属性configLocation、mapperLocations,顾名思义分别代表配置文件的位置映射文件的位置,这里只要路径配置正确,Spring便会自动去加载这两个配置文件了。

然后要修改的是Dao的实现类,此时不再继承之前的MyBatisUtil这个类,而是继承MyBatis-Spring-1.x.0.jar自带的SqlSessionDaoSupport.java,具体代码如下:

@Repository
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao
{
private static final String NAMESPACE = "StudentMapper."; @Resource
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)
{
super.setSqlSessionFactory(sqlSessionFactory);
} public List<Student> selectAllStudents()
{
return getSqlSession().selectList(NAMESPACE + "selectAllStudents");
} public int insertStudent(Student student)
{
return getSqlSession().insert(NAMESPACE + "insertStudent", student);
}
}

这里用到了两个注解,分别说一下。

(1)@Repository,这个注解和@Component、@Controller和我们最常见的@Service注解是一个作用,都可以将一个类声明为一个Spring的Bean。它们的区别到不在于具体的语义上,更多的是在于注解的定位上。之前说过,企业级应用注重分层开发的概念,因此,对这四个相似的注解应当有以下的理解:

  • @Repository注解,对应的是持久层即Dao层,其作用是直接和数据库交互,通常来说一个方法对应一条具体的Sql语句
  • @Service注解,对应的是服务层即Service层,其作用是对单条/多条Sql语句进行组合处理,当然如果简单的话就直接调用Dao层的某个方法了
  • @Controller注解,对应的是控制层即MVC设计模式中的控制层,其作用是接收用户请求,根据请求调用不同的Service取数据,并根据需求对数据进行组合、包装返回给前端
  • @Component注解,这个更多对应的是一个组件的概念,如果一个Bean不知道属于拿个层,可以使用@Component注解标注

这也体现了注解的其中一个优点:见名知意,即看到这个注解就大致知道这个类的作用即它在整个项目中的定位。

(2)@Resource,这个注解和@Autowired注解是一个意思,都可以自动注入属性属性。由于SqlSessionFactory是MyBatis的核心,它在spring.xml中又进行过了声明,因此这里通过@Resource注解将id为"sqlSessionFactory"的Bean给注入进来,之后就可以通过getSqlSession()方法获取到SqlSession并进行数据的增、删、改、查了。

最后无非就是写一个测试类测试一下:

public class StudentTest
{
public static void main(String[] args)
{
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
StudentDao studentDao = (StudentDao)ac.getBean("studentDaoImpl"); Student student = new Student();
student.setStudentName("Lucy"); int j = studentDao.insertStudent(student);
System.out.println("j = " + j + "\n"); System.out.println("-----Display students------");
List<Student> studentList = studentDao.selectAllStudents();
for (int i = 0, length = studentList.size(); i < length; i++)
System.out.println(studentList.get(i));
}
}

由于StudentDaoImpl.java类使用了@Repository注解且没有指定别名,因此StudentDaoImpl.java在Spring容器中的名字为"首字母小写+剩余字母"即"studentDaoImpl"。

运行一下程序,可以看见控制台上遍历出了new出来的Student,即该Student直接被插入了数据库中,整个过程中没有任何的commit、rollback,全部都是由Spring帮助我们实现的,这就是利用Spring对MyBatis进行事物管理。

多数据的事物处理。文章内容主要包含两方面:

1、单表多数据的事物处理

2、多库/多表多数据的事物处理

这两种都是企业级开发中常见的需求,有一定的类似,在处理的方法与技巧上又各有不同,在进入文章前,先做一些准备工作,因为后面会用到多表的插入事物管理,前面的文章建立了一个Student相关表及类,这里再建立一个Teacher相关的表及类。第一步是建立一张Teacher表:

create table teacher
(
teacher_id int auto_increment,
teacher_name varchar(20) not null,
primary key(teacher_id)
)

建立teacher_mapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="TeacherMapper">
<resultMap type="Teacher" id="TeacherMap">
<id column="teacher_id" property="teacherId" jdbcType="INTEGER" />
<result column="teacher_name" property="teacherName" jdbcType="VARCHAR" />
</resultMap> <select id="selectAllTeachers" resultMap="TeacherMap">
select teacher_id, teacher_name from teacher;
</select> <insert id="insertTeacher" useGeneratedKeys="true" keyProperty="teacher_id" parameterType="Teacher">
insert into teacher(teacher_id, teacher_name) values(null, #{teacherName, jdbcType=VARCHAR});
</insert>
</mapper>

建立Teacher.java:

public class Teacher
{
private int teacherId;
private String teacherName; public int getTeacherId()
{
return teacherId;
} public void setTeacherId(int teacherId)
{
this.teacherId = teacherId;
} public String getTeacherName()
{
return teacherName;
} public void setTeacherName(String teacherName)
{
this.teacherName = teacherName;
} public String toString()
{
return "Teacher{teacherId:" + teacherId + "], [teacherName:" + teacherName + "}";
}
}

还是再次提醒一下,推荐重写toString()方法,打印关键属性。不要忘了在config.xml里面给Teacher.java声明一个别名:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<typeAliases>
<typeAlias alias="Student" type="org.xrq.domain.Student" />
<typeAlias alias="Teacher" type="org.xrq.domain.Teacher" />
</typeAliases>
</configuration>

接着是TeacherDao.java接口:

public interface TeacherDao
{
public List<Teacher> selectAllTeachers();
public int insertTeacher(Teacher teacher);
}

其实现类TeacherDaoImpl.java:

@Repository
public class TeacherDaoImpl extends SqlSessionDaoSupport implements TeacherDao
{
private static final String NAMESPACE = "TeacherMapper."; @Resource
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)
{
super.setSqlSessionFactory(sqlSessionFactory);
} public List<Teacher> selectAllTeachers()
{
return getSqlSession().selectList(NAMESPACE + "selectAllTeachers");
} public int insertTeacher(Teacher teacher)
{
return getSqlSession().insert(NAMESPACE + "insertTeacher", teacher);
}
}

OK,这样准备工作就全部做完了,有需要的朋友可以实际去把TeacherDao中的方法正确性先验证一下,下面进入文章的内容。

单表事物管理

有一个很常见的需求,在同一张表里面,我想批量插入100条数据,但是由于这100条数据之间存在一定的相关性,只要其中任何一条事物的插入失败,之前插入成功的数据就全部回滚,这应当如何实现?这里有两种解决方案:

1、使用MyBatis的批量插入功能

2、使用Spring管理事物,任何一条数据插入失败

由于我们限定的前提是单表,因此比较推荐的是第一种做法

第二种做法尽管也可以实现我们的目标,但是每插入一条数据就要发起一次数据库连接,即使使用了数据库连接池,但在性能上依然有一定程度的损失。而使用MyBatis的批量插入功能,只需要发起一次数据库的连接,这100次的插入操作在MyBatis看来是一个整体,其中任何一个插入的失败都将导致整体插入操作的失败,即:要么全部成功,要么全部失败

下面来看一下实现,首先在student_mapper.xml中新增一个批量新增的方法<insert>:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="StudentMapper">
<resultMap type="Student" id="StudentMap">
<id column="student_id" property="studentId" jdbcType="INTEGER" />
<result column="student_name" property="studentName" jdbcType="VARCHAR" />
</resultMap> ... <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
<selectKey resultType="int" keyProperty="studentId" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into student(student_id, student_name) values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.studentId, jdbcType=INTEGER}, #{item.studentName, jdbcType=VARCHAR})
</foreach>
</insert>
</mapper>

这里主要是利用MyBatis提供的foreach,对传入的List做了一次遍历,并取得其中的属性进行插入。

然后在StudentDao.java中新增一个批量新增的方法batchInsert:

public interface StudentDao
{
public List<Student> selectAllStudents();
public int insertStudent(Student student);
public int batchInsertStudents(List<Student> studentList);
}

StudentDaoImpl.java实现它:

@Repository
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao
{
private static final String NAMESPACE = "StudentMapper."; @Resource
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)
{
super.setSqlSessionFactory(sqlSessionFactory);
}

... public int batchInsertStudents(List<Student> studentList)
{
return getSqlSession().insert(NAMESPACE + "batchInsert", studentList);
}
}

接着验证一下,首先drop一下student这张表并重新建一下,然后写一段测试程序:

public class StudentTest
{
@SuppressWarnings("resource")
public static void main(String[] args)
{
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
StudentDao studentDao = (StudentDao)ac.getBean("studentDaoImpl");
List<Student> studentList = null; Student student0 = new Student();
student0.setStudentName("Smith");
Student student1 = new Student();
student1.setStudentName("ArmStrong");
studentList = new ArrayList<>();
studentList.add(student0);
studentList.add(student1);
studentDao.batchInsertStudents(studentList); System.out.println("-----Display students------");
studentList = studentDao.selectAllStudents();
for (int i = 0, length = studentList.size(); i < length; i++)
System.out.println(studentList.get(i));
}
}

运行结果为:

-----Display students------
Student{[studentId:1], [studentName:Smith]}
Student{[studentId:2], [studentName:ArmStrong]}

看到批量插入成功。

从另外一个角度来看,假如我们这么建立这个studentList:

Student student0 = new Student();
student0.setStudentName("Smith");
Student student1 = new Student();
student1.setStudentName(null);
studentList = new ArrayList<>();
studentList.add(student0);
studentList.add(student1);
studentDao.batchInsertStudents(studentList);

故意制造第一条插入OK,第二条插入报错的场景,此时再运行一下程序,程序会抛出异常,即使第一条数据是OK的,依然不会插入。

最后,这里是批量插入,批量修改、批量删除也是一样的做法,可以自己试验一下。

多库/多表事物管理

上面的场景是对于单表的事物管理做法的推荐:实际上这并没有用到事物管理,而是使用MyBatis批量操作数据的做法,目的是为了减少和数据库的交互次数。

现在有另外一种场景,我要对单库/多库的两张表(Student表、Teacher表)同时插入一条数据,要么全部成功,要么全部失败,该如何处理?此时明显就不可以使用MyBatis批量操作的方法了,要实现这个功能,可以使用Spring的事物管理。

前面文章有讲,Dao层中的方法更多的是一种对数据库的增删改查的原子性操作,而Service层中的方法相当于对这些原子性的操作做一个组合,这里要同时操作TeacherDao、StudentDao中的insert方法,因此建立一个SchoolService接口:

public interface SchoolService
{
public void insertTeacherAndStudent(Teacher teacher, Student student);
}

写一下这个接口的实现类:

@Service
public class SchoolServiceImpl implements SchoolService
{
@Resource
private StudentDao studentDao; @Resource
private TeacherDao teacherDao; @Transactional
public void insertTeacherAndStudent(Teacher teacher, Student student)
{
studentDao.insertStudent(student);
teacherDao.insertTeacher(teacher);
}
}

这里用到了两个注解,解释一下。

(1)@Service注解

严格地说这里使用@Service注解不是特别好,因为Service作为服务层,更多的是应该对同一个Dao中的多个方法进行组合,如果要用到多个Dao中的方法,建议应该是放到Controller层中,引入两个Service,这里为了简单,就简单在一个Service中注入了StudentDao和TeacherDao两个了。

(2)@Transactional注解

这个注解用于开启事物管理,注意@Transactional注解的使用前提是该方法所在的类是一个Spring Bean,因此(1)中的@Service注解是必须的。换句话说,假如你给方法加了@Transactional注解却没有给类加@Service、@Repository、@Controller、@Component四个注解其中之一将类声明为一个Spring的Bean,那么对方法的事物管理,是不会起作用的。关于@Transactional注解,会在下面进一步解读。

接着写一个测试类测试一下:

public class SchoolTest
{
@SuppressWarnings("resource")
public static void main(String[] args)
{
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
SchoolService schoolService =
(SchoolService)ac.getBean("schoolServiceImpl"); Student student = new Student();
student.setStudentName("Billy");
Teacher teacher = new Teacher();
teacher.setTeacherName("Luna"); schoolService.insertTeacherAndStudent(teacher, student);
}
}

可以看一下数据库,Student表和Teacher表会同时多一条记录。接着继续从另外一个角度讲,我这么建立Student和Teacher:

Student student = new Student();
student.setStudentName("White");
Teacher teacher = new Teacher();
teacher.setTeacherName(null);

故意制造Teacher报错的场景,此时尽管Student没有问题,但是由于Teacher插入报错,因此Student的插入进行回滚,查看Student表,是不会有student_name为"White"这条记录的。

@Transactional注解

@Transactional这个注解绝对是Java程序员的一个福音,如果没有@Transactional注解,我们使用配置文件的做法进行声明式事务管理,我网上随便找一段配置文件:

<!-- 事物切面配置 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="insert" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="testService" expression="execution (* com.baobao.service.MyBatisService.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="testService"/>
</aop:config>

这种声明式的做法不得不说非常不好控制以及进行调试,尤其在要进行事物管理的内容不断增多之后,尤其体现出它的不方便。

使用@Transactional注解就不一样了,它可以精细到具体的类甚至具体的方法上(区别是同一个类,对方法的事物管理配置会覆盖对类的事务管理配置),另外,声明式事物中的一些属性,在@Transaction注解中都可以进行配置,下面总结一下常用的一些属性。

(1) @Transactional(propagation = Propagation.REQUIRED)

最重要的先说,propagation属性表示的是事物的传播特性,一共有以下几种:

事物传播特性 作      用
Propagation.REQUIRED 方法运行时如果已经处在一个事物中,那么就加入到这个事物中,否则自己新建一个事物,REQUIRED是默认的事物传播特性
Propagation.NOT_SUPPORTED 如果方法没有关联到一个事物,容器不会为它开启一个事物,如果方法在一个事物中被调用,该事物会被挂起直到方法调用结束再继续执行
Propagation.REQUIRES_NEW 不管是否存在事物,该方法总会为自己发起一个新的事物,如果方法已经运行在一个事物中,则原有事物挂起,新的事物被创建
Propagation.MANDATORY 该方法只能在一个已经存在的事物中执行,业务方法不能发起自己的事物,如果在没有事物的环境下被调用,容器抛出异常
Propagation.SUPPORTS 该方法在某个事物范围内被调用,则方法成为该事物的一部分,如果方法在该事物范围内被调用,该方法就在没有事物的环境下执行
Propagation.NEVER 该方法绝对不能在事物范围内执行,如果在就抛出异常,只有该方法没有关联到任何事物,才正常执行
Propagation.NESTED 如果一个活动的事物存在,则运行在一个嵌套的事物中。如果没有活动事物,则按REQUIRED属性执行,它只对DataSourceTransactionManager事物管理器有效

因此我们可以来简单分析一下上面的insertTeacherAndStudent方法:

  1. 由于没有指定propagation属性,因此事物传播特性为默认的REQUIRED
  2. StudentDao的insertStudent方法先运行,此时没有事物,因此新建一个事物
  3. TeacherDao的insertTeacher方法接着运行,此时由于StudentDao的insertStudent方法已经开启了一个事物,insertTeacher方法加入到这个事物中
  4. StudentDao的insertStudent方法和TeacherDao的insertTeacher方法组成了一个事物,两个方法要么同时执行成功,要么同时执行失败

(2)@Transactional(isolation = Isolation.DEFAULT)

事物隔离级别

(3)@Transactional(readOnly = true)

该事物是否为一个只读事物,配置这个属性可以提高方法执行效率。

(4)@Transactional(rollbackFor = {ArrayIndexOutOfBoundsException.class, NullPointerException.class})

遇到方法抛出ArrayIndexOutOfBoundsException、NullPointerException两种异常会回滚数据,仅支持RuntimeException的子类

(5)@Transactional(noRollbackFor = {ArrayIndexOutOfBoundsException.class, NullPointerException.class})

这个和上面的相反,遇到ArrayIndexOutOfBoundsException、NullPointerException两种异常不会回滚数据,同样也是仅支持RuntimeException的子类。对(4)、(5)不是很理解的朋友,我给一个例子:

@Transactional(rollbackForClassName = {"NullPointerException"})
public void insertTeacherAndStudent(Teacher teacher, Student student)
{
studentDao.insertStudent(student);
teacherDao.insertTeacher(teacher);
String s = null;
s.length();
}

构造Student、Teacher的数据运行一下,然后查看下库里面有没有对应的记录就好了,然后再把rollbackForClassName改为noRollbackForClassName,对比观察一下。

(6)@Transactional(rollbackForClassName = {"NullPointerException"})、@Transactional(noRollbackForClassName = {"NullPointerException"})

这两个放在一起说了,和上面的(4)、(5)差不多,无非是(4)、(5)是通过.class来指定要回滚和不要回滚的异常,这里是通过字符串形式的名字来制定要回滚和不要回滚的异常。

(7)@Transactional(timeout = 30)

事物超时时间,单位为秒。

(8)@Transactional(value = "tran_1")

value这个属性主要就是给某个事物一个名字而已,这样在别的地方就可以使用这个事物的配置。