Spring中的事物管理

1.编程式事务管理

编写程序式的事务管理可以清楚的定义事务的边界,可以实现细粒度的事务控制,比如你可以通过程序代码来控制你的事务何时开始,何时结束等,与后面介绍的声明式事务管理相比,它可以实现细粒度的事务控制,例如jdbc,hibernate。但spring中不提倡使用
优缺点:
1. 事务控制精确
2. 事务代码,与业务逻辑处理代码,耦合在一起!事务代码,不能共用! 重新写事务控制操作!开发效率低,不便于维护! (不想用事务,要改代码!)

2.声明式事务管理 (在Spring中使用)

如果你并不需要细粒度的事务控制,你可以使用声明式事务,在Spring中,你只需要在Spring配置文件中做一些配置,即可将操作纳入到事务管理中,解除了和代码的耦合, 这是对应用代码影响最小的选择,从这一点再次验证了Spring关于AOP的概念。当你不需要事务管理的时候,可以直接从Spring配置文件中移除该设置

2.1 xml配置声明式事物管理

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 1. 数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
</bean>

<!-- 2. JdbcTemplate配置 , 注入数据源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 3. dao实例,注入jdbcTemplate -->
<bean id="deptDao" class="cn.itcast.a_tx_jdbc.DeptDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

<!-- 4. Service实例,注入dao实例 -->
<bean id="deptService" class="cn.itcast.a_tx_jdbc.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>

<!-- 5. Spring声明式事务管理配置 -->

<!-- 5.1 配置事务管理器类 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 5.2 事务通知配置, 拦截到指定的方法后如何管理事务 -->
<!-- find* find开头的方法,是只读的事务 -->
<!-- * 上面所有的方法都不满足时候,采用的事务控制规则 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:met****hod name="get*" read-only="true"/>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>

<!-- 5.3 事务Aop配置 = 切入点表达式 + 应用上面的事务通知 -->
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.a_tx_jdbc.*Service.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>

</beans>

2.2 注解实现声明式事物

1
2
开启
<tx:annotation-driven transaction-manager="txManager"/>

在service中添加注解@Transactional