前一篇文章復習了MyBatis的基本使用以及使用Spring管理MyBatis的事務的做法,本文的目的是在這個的基礎上稍微做一點點的進階:多數據的事務處理。文章內容主要包含兩方面:
1、單表多數據的事務處理
2、多庫/多表多數據的事務處理
這兩種都是企業級開發中常見的需求,有一定的類似,在處理的方法與技巧上又各有不同,在進入文章前,先做一些准備工作,因為后面會用到多表的插入事務管理,前面的文章建立了一個Student相關表及類,這里再建立一個Teacher相關的表及類。第一步是建立一張Teacher表:
|
1
2
3
4
5
6
|
create
table
teacher
(
teacher_id
int
auto_increment,
teacher_name
varchar
(20)
not
null
,
primary
key
(teacher_id)
)
|
建立teacher_mapper.xml:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?
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:
|
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
|
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聲明一個別名:
|
1
2
3
4
5
6
7
8
9
10
|
<?
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接口:
|
1
2
3
4
5
|
public
interface
TeacherDao
{
public
List<Teacher> selectAllTeachers();
public
int
insertTeacher(Teacher teacher);
}
|
其實現類TeacherDaoImpl.java:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@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>:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?
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:
|
1
2
3
4
5
6
|
public
interface
StudentDao
{
public
List<Student> selectAllStudents();
public
int
insertStudent(Student student);
public
int
batchInsertStudents(List<Student> studentList);
}
|
StudentDaoImpl.java實現它:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@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這張表並重新建一下,然后寫一段測試程序:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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));
}
}
|
運行結果為:
|
1
2
3
|
-----Display students------
Student{[studentId:1], [studentName:Smith]}
Student{[studentId:2], [studentName:ArmStrong]}
|
看到批量插入成功。
從另外一個角度來看,假如我們這么建立這個studentList:
|
1
2
3
4
5
6
7
8
|
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接口:
|
1
2
3
4
|
public
interface
SchoolService
{
public
void
insertTeacherAndStudent(Teacher teacher, Student student);
}
|
寫一下這個接口的實現類:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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注解,會在下面進一步解讀。
接着寫一個測試類測試一下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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:
|
1
2
3
4
|
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注解,我們使用配置文件的做法進行聲明式事務管理,我網上隨便找一段配置文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- 事物切面配置 -->
<
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方法:
- 由於沒有指定propagation屬性,因此事務傳播特性為默認的REQUIRED
- StudentDao的insertStudent方法先運行,此時沒有事務,因此新建一個事務
- TeacherDao的insertTeacher方法接着運行,此時由於StudentDao的insertStudent方法已經開啟了一個事務,insertTeacher方法加入到這個事務中
- 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)不是很理解的朋友,我給一個例子:
|
1
2
3
4
5
6
7
8
|
@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這個屬性主要就是給某個事務一個名字而已,這樣在別的地方就可以使用這個事務的配置。
