您还未登录! 登录 | 注册 | 帮助  

您的位置: 首页 > 软件测试管理 > 配置管理 > 正文

Spring管理事务配置的五种方式

发表于:2017-08-07 作者:网络转载 来源:

  spring配置文件中关于事务配置总是由三个组成部分,DataSource、TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块!
  首先我创建了两个类,一个接口类一个实现类:
package com.dao;
public interface UserDao {
public void getUser();
}
package com.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.dao.UserDao;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public void getUser(){
}
}
  第一种:每个Bean都有一个代理:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xmlns:aop="http://www.springframework.org/schema/aop"
5     xmlns:tx="http://www.springframework.org/schema/tx"
6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
7            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
8            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
9     <!-- 数据源 -->
10     <bean id="dataSource"
11         class="org.apache.commons.dbcp.BasicDataSource"
12         destroy-method="close">
13         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
14         <property name="url"
15             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
16         <property name="username" value="root" />
17         <property name="password" value="root" />
18         <!-- 连接池启动时的初始值 -->
19         <property name="initialSize" value="10" />
20         <!-- 连接池的最大值 -->
21         <property name="maxActive" value="10" />
22         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
23         <property name="maxIdle" value="20" />
24         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
25         <property name="minIdle" value="10" />
26         <property name="defaultAutoCommit" value="true" />
27     </bean>
28     <!-- 会话工厂 -->
29     <bean id="sessionFactory"
30         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
31         <property name="dataSource" ref="dataSource" />
32         <property name="mappingLocations">
33             <list>
34                 <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
35             </list>
36         </property>
37         <property name="hibernateProperties">
38             <props>
39                 <prop key="hibernate.dialect">
40                     org.hibernate.dialect.MySQL5Dialect
41                 </prop>
42                 <prop key="hibernate.show_sql">true</prop>
43                 <prop key="hibernate.format_sql">true</prop>
44             </props>
45         </property>
46     </bean>
47     <!-- 定义事务管理器 -->
48     <bean id="transactionManager"
49         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
50         <property name="sessionFactory" ref="sessionFactory" />
51     </bean>
52     <!-- 配置服务层 -->
53     <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
54         <property name="sessionFactory" ref="sessionFactory" />
55     </bean>
56     <bean id="userDao"
57         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
58         <!-- 配置事务管理器 -->
59         <property name="transactionManager" ref="transactionManager" />
60         <property name="target" ref="userDaoAgency" />
61         <property name="proxyInterfaces" value="com.dao.UserDao" />
62         <!-- 配置事务属性 -->
63         <property name="transactionAttributes">
64             <props>
65                 <prop key="*">PROPAGATION_REQUIRED</prop>
66             </props>
67         </property>
68     </bean>
69 </beans>
  第二种:所有Bean共享一个代理:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.0.xsd
3            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
4            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
5 <!-- 数据源 -->
6 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
7 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
8 <property name="url" value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"/>
9 <property name="username" value="root"/>
10 <property name="password" value="root"/>
11 <!-- 连接池启动时的初始值 -->
12 <property name="initialSize" value="10"/>
13 <!-- 连接池的最大值 -->
14 <property name="maxActive" value="10"/>
15 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
16 <property name="maxIdle" value="20"/>
17 <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
18 <property name="minIdle" value="10"/>
19 <property name="defaultAutoCommit" value="true"/>
20 </bean>
21 <!-- 会话工厂 -->
22 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
23 <property name="dataSource" ref="dataSource"/>
24 <property name="mappingLocations">
25 <list>
26 <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
27 </list>
28 </property>
29 <property name="hibernateProperties">
30 <props>
31 <prop key="hibernate.dialect">
32                     org.hibernate.dialect.MySQL5Dialect
33                 </prop>
34 <prop key="hibernate.show_sql">true</prop>
35 <prop key="hibernate.format_sql">true</prop>
36 </props>
37 </property>
38 </bean>
39 <!-- 定义事务管理器 -->
40 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
41 <property name="sessionFactory" ref="sessionFactory"/>
42 </bean>
43 <!-- 定义事务 -->
44 <bean id="base" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
45 <!-- 配置事务管理器 -->
46 <property name="transactionManager" ref="transactionManager"/>
47 <!-- 配置事务属性 -->
48 <property name="transactionAttributes">
49 <props>
50 <prop key="*">PROPAGATION_REQUIRED</prop>
51 </props>
52 </property>
53 </bean>
54 <!-- 配置服务层 -->
55 <bean id="userDao" class="com.dao.impl.UserDaoImpl">
56 <property name="sessionFactory" ref="sessionFactory"/>
57 </bean>
58 <!-- 代理对象 -->
59 <bean id="userDaoAgency" parent="base">
60 <property name="target" ref="userDao"/>
61 </bean>
62 </beans>
  第三种:拦截器:
1<?xmlversion="1.0"encoding="UTF-8"?>
2<beansxmlns="http://www.springframework.org/schema/beans"
3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4xmlns:aop="http://www.springframework.org/schema/aop"
5xmlns:tx="http://www.springframework.org/schema/tx"
6xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
7http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
8http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
9<!--数据源-->
10<beanid="dataSource"
11class="org.apache.commons.dbcp.BasicDataSource"
12destroy-method="close">
13<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
14<propertyname="url"
15value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"/>
16<propertyname="username"value="root"/>
17<propertyname="password"value="root"/>
18<!--连接池启动时的初始值-->
19<propertyname="initialSize"value="10"/>
20<!--连接池的最大值-->
21<propertyname="maxActive"value="10"/>
22<!--最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止-->
23<propertyname="maxIdle"value="20"/>
24<!--最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请-->
25<propertyname="minIdle"value="10"/>
26<propertyname="defaultAutoCommit"value="true"/>
27</bean>
28<!--会话工厂-->
29<beanid="sessionFactory"
30class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
31<propertyname="dataSource"ref="dataSource"/>
32<propertyname="mappingLocations">
33<list>
34<value>classpath:/com/nms/entity/**/*.hbm.xml</value>
35</list>
36</property>
37<propertyname="hibernateProperties">
38<props>
39<propkey="hibernate.dialect">
40org.hibernate.dialect.MySQL5Dialect
41</prop>
42<propkey="hibernate.show_sql">true</prop>
43<propkey="hibernate.format_sql">true</prop>
44</props>
45</property>
46</bean>
47<!--定义事务管理器(声明式的事务)-->
48<beanid="transactionManager"
49class="org.springframework.orm.hibernate3.HibernateTransactionManager">
50<propertyname="sessionFactory"ref="sessionFactory"/>
51</bean>
52<!--定义事务-->
53<beanid="transactionInterceptor"
54class="org.springframework.transaction.interceptor.TransactionInterceptor">
55<propertyname="transactionManager"ref="transactionManager"/>
56<!--配置事务属性-->
57<propertyname="transactionAttributes">
58<props>
59<propkey="*">PROPAGATION_REQUIRED</prop>
60</props>
61</property>
62</bean>
63<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
64<propertyname="beanNames">
65<list>
66<value>*DaoImpl</value>
67</list>
68</property>
69<propertyname="interceptorNames">
70<list>
71<value>transactionInterceptor</value>
72</list>
73</property>
74</bean>
75<!--配置服务层-->
76<beanid="userDaoAgency"class="com.dao.impl.UserDaoImpl">
77<propertyname="sessionFactory"ref="sessionFactory"/>
78</bean>
79</beans>

  第四种:使用tx标签配置的拦截器:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xmlns:context="http://www.springframework.org/schema/context"
5     xmlns:aop="http://www.springframework.org/schema/aop"
6     xmlns:tx="http://www.springframework.org/schema/tx"
7     xsi:schemaLocation="http://www.springframework.org/schema/beans
8            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
9            http://www.springframework.org/schema/context
10            http://www.springframework.org/schema/context/spring-context-2.5.xsd
11            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13     <!-- 数据源 -->
14     <bean id="dataSource"
15         class="org.apache.commons.dbcp.BasicDataSource"
16         destroy-method="close">
17         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
18         <property name="url"
19             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
20         <property name="username" value="root" />
21         <property name="password" value="root" />
22         <!-- 连接池启动时的初始值 -->
23         <property name="initialSize" value="10" />
24         <!-- 连接池的最大值 -->
25         <property name="maxActive" value="10" />
26         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
27         <property name="maxIdle" value="20" />
28         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
29         <property name="minIdle" value="10" />
30         <property name="defaultAutoCommit" value="true" />
31     </bean>
32     <!-- 会话工厂 -->
33     <bean id="sessionFactory"
34         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
35         <property name="dataSource" ref="dataSource" />
36         <property name="mappingLocations">
37             <list>
38                 <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
39             </list>
40         </property>
41         <property name="hibernateProperties">
42             <props>
43                 <prop key="hibernate.dialect">
44                     org.hibernate.dialect.MySQL5Dialect
45                 </prop>
46                 <prop key="hibernate.show_sql">true</prop>
47                 <prop key="hibernate.format_sql">true</prop>
48             </props>
49         </property>
50     </bean>
51     <context:annotation-config />
52     <context:component-scan base-package="com.dao" />
53     <!-- 定义事务管理器 -->
54     <bean id="transactionManager"
55         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
56         <property name="sessionFactory" ref="sessionFactory" />
57     </bean>
58     <!-- 定义事务 -->
59     <tx:advice id="txAdvice" transaction-manager="transactionManager">
60         <tx:attributes>
61             <tx:method name="*" propagation="REQUIRED" />
62         </tx:attributes>
63     </tx:advice>
64     <!-- 定义切面 -->
65     <aop:config>
66         <aop:pointcut id="interceptorPointCuts" expression="execution(* com.dao.*.*(..))" />
67         <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
68     </aop:config>
69 </beans>
  第五种:注解:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xmlns:context="http://www.springframework.org/schema/context"
5     xmlns:aop="http://www.springframework.org/schema/aop"
6     xmlns:tx="http://www.springframework.org/schema/tx"
7     xsi:schemaLocation="http://www.springframework.org/schema/beans
8            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
9            http://www.springframework.org/schema/context
10            http://www.springframework.org/schema/context/spring-context-2.5.xsd
11            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13     <!-- 数据源 -->
14     <bean id="dataSource"
15         class="org.apache.commons.dbcp.BasicDataSource"
16         destroy-method="close">
17         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
18         <property name="url"
19             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
20         <property name="username" value="root" />
21         <property name="password" value="root" />
22         <!-- 连接池启动时的初始值 -->
23         <property name="initialSize" value="10" />
24         <!-- 连接池的最大值 -->
25         <property name="maxActive" value="10" />
26         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
27         <property name="maxIdle" value="20" />
28         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
29         <property name="minIdle" value="10" />
30         <property name="defaultAutoCommit" value="true" />
31     </bean>
32     <!-- 会话工厂 -->
33     <bean id="sessionFactory"
34         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
35         <property name="dataSource" ref="dataSource" />
36         <property name="mappingLocations">
37             <list>
38                 <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
39             </list>
40         </property>
41         <property name="hibernateProperties">
42             <props>
43                 <prop key="hibernate.dialect">
44                     org.hibernate.dialect.MySQL5Dialect
45                 </prop>
46                 <prop key="hibernate.show_sql">true</prop>
47                 <prop key="hibernate.format_sql">true</prop>
48             </props>
49         </property>
50     </bean>
51     <context:annotation-config />
52     <!-- 使用注解的包路径 -->
53     <context:component-scan base-package="com.dao" />
54     <!-- 支持  @Transactional 标记 -->
55     <tx:annotation-driven transaction-manager="transactionManager"/>
56     <!-- 定义事务管理器 -->
57     <bean id="transactionManager"
58         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
59         <property name="sessionFactory" ref="sessionFactory" />
60     </bean>
61 </beans>
  如果使用了注解,那么实现类应该这样写:
1 package com.dao.impl;
2 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
3 import org.springframework.stereotype.Component;
4 import org.springframework.transaction.annotation.Transactional;
5 import com.dao.UserDao;
6 @Transactional
7 @Component("userDaoAgency")
8 public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
9     /**
10      * 为方法增加事务处理特性
11      */
12     @Transactional(readOnly=true)
13     public void getUser(){
14     }
15 }
  这样每个方法都能自己定义自己的事务处理!