首页 > 其他分享 >Spring 配置 事务的几种方式

Spring 配置 事务的几种方式

时间:2023-06-13 14:03:08浏览次数:29  
标签:事务 hibernate Spring value 几种 sessionFactory org true class


评:

Spring配置文件中关于事务配置总是由三个组成部分,DataSource、TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块!

 

首先我创建了两个类,一个接口一个实现:

1. package
2. public interface
3. public void
4. }

 

实现:

1. package
2. import
3. import
4. public class UserDaoImpl extends HibernateDaoSupport implements
5. public void
6.     }     
7. }


 

 

第一种:每个Bean都有一个代理:

1. <?xml versinotallow="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:schemaLocatinotallow="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. >
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&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 versinotallow="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http://www.springframework.org/schema/aop"
5. "http://www.springframework.org/schema/tx"
6. //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
7. //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
8. //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
9.     <!-- 数据源 -->  
10. "dataSource"
11. class="org.apache.commons.dbcp.BasicDataSource"
12. "close">  
13. "driverClassName" value="com.mysql.jdbc.Driver"
14. "url"
15. "jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8"
16. "username" value="root"
17. "password" value="root"
18.         <!-- 连接池启动时的初始值 -->  
19. "initialSize" value="10"
20.         <!-- 连接池的最大值 -->  
21. "maxActive" value="10"
22.         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->  
23. "maxIdle" value="20"
24.         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->  
25. "minIdle" value="10"
26. "defaultAutoCommit" value="true"
27.     </bean>  
28.     <!-- 会话工厂 -->  
29. "sessionFactory"
30. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
31. "dataSource" ref="dataSource"
32. "mappingLocations">  
33.             <list>  
34. /**/*.hbm.xml</value>  
35.             </list>  
36.         </property>  
37. "hibernateProperties">  
38.             <props>  
39. "hibernate.dialect">  
40.                     org.hibernate.dialect.MySQL5Dialect  
41.                 </prop>  
42. "hibernate.show_sql">true</prop>  
43. "hibernate.format_sql">true</prop>  
44.             </props>  
45.         </property>  
46.     </bean>  
47.     <!-- 定义事务管理器 -->  
48. "transactionManager"
49. class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
50. "sessionFactory" ref="sessionFactory"
51.     </bean>  
52.      <!-- 定义事务 -->  
53. "base"
54. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
55. "true" abstract="true">  
56.         <!-- 配置事务管理器 -->  
57. "transactionManager" ref="transactionManager"
58.         <!-- 配置事务属性 -->  
59. "transactionAttributes">  
60.             <props>  
61. "*">PROPAGATION_REQUIRED</prop>  
62.             </props>  
63.         </property>  
64.     </bean>  
65.     <!-- 配置服务层 -->  
66. "userDao"
67. class="com.dao.impl.UserDaoImpl">  
68. "sessionFactory" ref="sessionFactory"
69.     </bean>  
70.     <!-- 代理对象 -->  
71. "userDaoAgency" parent="base">  
72. "target" ref="userDao"
73.     </bean>  
74. </beans>

 

 

第三种:拦截器:

1. <?xml versinotallow="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http://www.springframework.org/schema/aop"
5. "http://www.springframework.org/schema/tx"
6. //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
7. //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
8. //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
9.     <!-- 数据源 -->  
10. "dataSource"
11. class="org.apache.commons.dbcp.BasicDataSource"
12. "close">  
13. "driverClassName" value="com.mysql.jdbc.Driver"
14. "url"
15. "jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8"
16. "username" value="root"
17. "password" value="root"
18.         <!-- 连接池启动时的初始值 -->  
19. "initialSize" value="10"
20.         <!-- 连接池的最大值 -->  
21. "maxActive" value="10"
22.         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->  
23. "maxIdle" value="20"
24.         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->  
25. "minIdle" value="10"
26. "defaultAutoCommit" value="true"
27.     </bean>  
28.     <!-- 会话工厂 -->  
29. "sessionFactory"
30. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
31. "dataSource" ref="dataSource"
32. "mappingLocations">  
33.             <list>  
34. /**/*.hbm.xml</value>  
35.             </list>  
36.         </property>  
37. "hibernateProperties">  
38.             <props>  
39. "hibernate.dialect">  
40.                     org.hibernate.dialect.MySQL5Dialect  
41.                 </prop>  
42. "hibernate.show_sql">true</prop>  
43. "hibernate.format_sql">true</prop>  
44.             </props>  
45.         </property>  
46.     </bean>      
47.      <!-- 定义事务管理器(声明式的事务) -->    
48. "transactionManager"
49. class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
50. "sessionFactory" ref="sessionFactory"
51.     </bean>     
52.      <!-- 定义事务 -->   
53. "transactionInterceptor"
54. class="org.springframework.transaction.interceptor.TransactionInterceptor">    
55. "transactionManager" ref="transactionManager"
56.         <!-- 配置事务属性 -->    
57. "transactionAttributes">    
58.             <props>    
59. "*">PROPAGATION_REQUIRED</prop>    
60.             </props>    
61.         </property>    
62.     </bean>        
63. class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    
64. "beanNames">    
65.             <list>    
66.                 <value>*DaoImpl</value>  
67.             </list>    
68.         </property>    
69. "interceptorNames">    
70.             <list>    
71.                 <value>transactionInterceptor</value>    
72.             </list>    
73.         </property>    
74.     </bean>    
75.     <!-- 配置服务层 -->  
76. "userDaoAgency" class="com.dao.impl.UserDaoImpl">  
77. "sessionFactory" ref="sessionFactory"
78.     </bean>  
79. </beans>


 

 

第四种:使用tx标签配置的拦截器:

1. <?xml versinotallow="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http://www.springframework.org/schema/context"
5. "http://www.springframework.org/schema/aop"
6. "http://www.springframework.org/schema/tx"
7. //www.springframework.org/schema/beans 
8. //www.springframework.org/schema/beans/spring-beans-2.5.xsd
9. //www.springframework.org/schema/context
10. //www.springframework.org/schema/context/spring-context-2.5.xsd
11. //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12. //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13.     <!-- 数据源 -->  
14. "dataSource"
15. class="org.apache.commons.dbcp.BasicDataSource"
16. "close">  
17. "driverClassName" value="com.mysql.jdbc.Driver"
18. "url"
19. "jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8"
20. "username" value="root"
21. "password" value="root"
22.         <!-- 连接池启动时的初始值 -->  
23. "initialSize" value="10"
24.         <!-- 连接池的最大值 -->  
25. "maxActive" value="10"
26.         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->  
27. "maxIdle" value="20"
28.         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->  
29. "minIdle" value="10"
30. "defaultAutoCommit" value="true"
31.     </bean>  
32.     <!-- 会话工厂 -->  
33. "sessionFactory"
34. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
35. "dataSource" ref="dataSource"
36. "mappingLocations">  
37.             <list>  
38. /**/*.hbm.xml</value>  
39.             </list>  
40.         </property>  
41. "hibernateProperties">  
42.             <props>  
43. "hibernate.dialect">  
44.                     org.hibernate.dialect.MySQL5Dialect  
45.                 </prop>  
46. "hibernate.show_sql">true</prop>  
47. "hibernate.format_sql">true</prop>  
48.             </props>  
49.         </property>  
50.     </bean>  
51.     <context:annotation-config />  
52. package="com.dao"
53.     <!-- 定义事务管理器 -->    
54. "transactionManager"
55. class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
56. "sessionFactory" ref="sessionFactory"
57.     </bean>  
58.     <!-- 定义事务 -->  
59. "txAdvice" transaction-manager="transactionManager">  
60.         <tx:attributes>  
61. "*" propagatinotallow="REQUIRED"
62.         </tx:attributes>  
63.     </tx:advice>  
64.     <!-- 定义切面 -->  
65.     <aop:config>  
66. "interceptorPointCuts" expressinotallow="execution(* com.dao.*.*(..))"
67. "txAdvice" pointcut-ref="interceptorPointCuts"
68.     </aop:config>  
69. </beans>

 

第五种:注解:

1. <?xml versinotallow="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http://www.springframework.org/schema/context"
5. "http://www.springframework.org/schema/aop"
6. "http://www.springframework.org/schema/tx"
7. //www.springframework.org/schema/beans 
8. //www.springframework.org/schema/beans/spring-beans-2.5.xsd
9. //www.springframework.org/schema/context
10. //www.springframework.org/schema/context/spring-context-2.5.xsd
11. //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12. //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13.     <!-- 数据源 -->  
14. "dataSource"
15. class="org.apache.commons.dbcp.BasicDataSource"
16. "close">  
17. "driverClassName" value="com.mysql.jdbc.Driver"
18. "url"
19. "jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8"
20. "username" value="root"
21. "password" value="root"
22.         <!-- 连接池启动时的初始值 -->  
23. "initialSize" value="10"
24.         <!-- 连接池的最大值 -->  
25. "maxActive" value="10"
26.         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->  
27. "maxIdle" value="20"
28.         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->  
29. "minIdle" value="10"
30. "defaultAutoCommit" value="true"
31.     </bean>  
32.     <!-- 会话工厂 -->  
33. "sessionFactory"
34. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
35. "dataSource" ref="dataSource"
36. "mappingLocations">  
37.             <list>  
38. /**/*.hbm.xml</value>  
39.             </list>  
40.         </property>  
41. "hibernateProperties">  
42.             <props>  
43. "hibernate.dialect">  
44.                     org.hibernate.dialect.MySQL5Dialect  
45.                 </prop>  
46. "hibernate.show_sql">true</prop>  
47. "hibernate.format_sql">true</prop>  
48.             </props>  
49.         </property>  
50.     </bean>  
51.     <context:annotation-config />  
52.     <!-- 使用注解的包路径 -->  
53. package="com.dao"
54. @Transactional
55. "transactionManager"/>  
56.     <!-- 定义事务管理器 -->    
57. "transactionManager"
58. class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
59. "sessionFactory" ref="sessionFactory"
60.     </bean>  
61. </beans>


 

如果使用了注解,那么实现类应该这样写:

1. package
2. import
3. import
4. import
5. import
6. @Transactional
7. @Component("userDaoAgency")  
8. public class UserDaoImpl extends HibernateDaoSupport implements
9. /**
10.      * 为方法增加事务处理特性
11.      */
12. @Transactional(readOnly=true)  
13. public void
14.     }     
15. }

 

这样每个方法都能自己定义自己的事务处理!

标签:事务,hibernate,Spring,value,几种,sessionFactory,org,true,class
From: https://blog.51cto.com/u_16080829/6469263

相关文章

  • springboot 测试用例 gradle
    在springboot2.4.5之后的变成了jinut5直接引用即可不需要排除org.junit.jupiter.api.Testorg.springframework.boot:spring-boot-starter-test测试数据H2packagecom.example.test_pro_gradle;importorg.junit.jupiter.api.Test;importorg.slf4j.Logger;impor......
  • springcloud 启动失败 YAMLException java.nio.charset.MalformedInputException Inp
     上面这个是错误信息,但是该微服务在本地启动的时候是可以的,但是本地打成jar包本地执行的时候就失败。需要再Java-jar的中间加一下字符编码java-Dfile.encoding=utf-8-jar  myself.jar   myself.jar是自己的jar包问题解决......
  • 一文解读.NET 数据库事务
    事务是数据库系统中的重要概念,本文讲解作者从业CRUD十余载的事务多种使用方式总结。以下所有内容都是针对单机事务而言,不涉及分布式事务相关的东西!关于事务原理的讲解不针对具体的某个数据库实现,所以某些地方可能和你的实践经验不符。1|0认识事务为什么需要数据库事务?转账是生......
  • s1sh整合实例 Strut1.2+Spring2.6+Hibernate3.2
    [code]开发环境:MyEclipse8.5+Mysql说明:本实例是简单注册程序(只有两个属性)数据库脚本:user.sqlCREATETABLE`user`(`Id`int(11)NOTNULLAUTO_INCREMENT,`username`varchar(255)DEFAULTNULL,`password`varchar(255)DEFAULTNULL,P......
  • Spring下的权限框架 spring security总结
    Spring下的权限框架springsecurity总结[code]springsecurity总结首先导入springsecurity所需要的jar包spring-security-core-2.0.5.RELEASE.jarspring-security-core-tiger-2.0.5.RELEASE.jar一.配置过滤器在web.xml中定义如下过滤器<filter><fil......
  • SpringCloudAlibaba
    一、SpringCloudAlibaba功能组件?Sentinel:流量控制,熔断降级,系统负载保护等方面。Nacos:注册中心,配置管理中心。RocketMq:分布式消息系统。Dubbo:java的RPC框架。Seata:分布式事务解决方案。AlibabaCloudOSS:阿里云对象存储服务。AlibabaCloudSchedulerX:分布式任务调度产品......
  • 跨越式初学SpringBoot的各种问题《一》
    前提:本人没有学过一点SSM,在学SpringBoot之前,上网搜索了各种回答,关于能否跨越直接学SpringBoot,得到的都是肯定回答可以;在本人觉得promising,开始在哔哩哔哩大学,翻找各种SpringBoot叫教学视频,每一个教程打开第一集就是,要求懂SSM(谁懂啊bleak)。然而,我依然坚定开始了零SSM基础的学习!应......
  • Spring的环境搭建的IOC
    1、Spring的简单组成bean的生命周期的管理:java对象的创建,使用,销毁等轻量级:使用简单容器:spring可以管理对象,创建好对象,放入spring容器,容器就是存放对象的组成部分:SpringCore、SpringAOP、SpringORM、SpringDAO、SpringContext、SpringWeb和SpringWeb......
  • 什么是AOP【Spring AOP】
    OOP(Object-OrientedProgramming)面向对象编程,允许开发者定义纵向的关系,但并适用于定义横向的关系,导致了大量代码的重复,而不利于各个模块的重用。   AOP(Aspect-OrientedProgramming)面向切面编程,作为面向对象的一种补充,用于将那些与业务无关,但却对多个对象产生影响的公共行......
  • SpringBoot环境扩展机制
    前言SpringBoot在启动时,会先创建Environment实例,然后再创建ApplicationContext上下文。在创建Environment时,提供了扩展机制给用户对Environment实例进行修改,如SpringBoot默认使用的application.yml属性配置文件。如何使用该机制编写类实现EnvironmentPostProcessor接口。在......