Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTransaction,commit,这些都是重复的工作,我们可以把事务管理部分交给spring框架完成。

配置事务(xml方式)

使用spring管理事务后在dao中不再需要调用beginTransaction和commit,也不需要调用session.close(),使用API  sessionFactory.getCurrentSession()来替代sessionFactory.openSession()

 1 @Repository2 public class UserDaoImpl implements UserDao {3     @Autowired4     private SessionFactory sessionFactory;5     6     public User findUserById(int id) {7         Session session = sessionFactory.getCurrentSession();8         User user = (User)session.get(User.class, id);9         session.delete(user);
10
11         return user;
12     }
13 }

采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会。

采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()创建的session必须手动关闭。

使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:

* 如果使用的是本地事务(jdbc事务)

<property name="hibernate.current_session_context_class">thread</property>

* 如果使用的是全局事务(jta事务)

<property name="hibernate.current_session_context_class">jta</property>

如果采用的时Hibernate4,使用getCurrentSession()必须配置事务,否则无法取到session

applicationContext.xml配置

 1 <?xml version="1.0" encoding="UTF-8"?>2 <beans3     xmlns="http://www.springframework.org/schema/beans"4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5     xmlns:p="http://www.springframework.org/schema/p"6     xmlns:context="http://www.springframework.org/schema/context"7     xmlns:aop="http://www.springframework.org/schema/aop"8     xmlns:tx="http://www.springframework.org/schema/tx"9     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
10     xmlns:cache="http://www.springframework.org/schema/cache"
11     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
12                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
13                         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
14                         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
15                         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
16                         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
17
18     <context:component-scan base-package="dao"/>
19     <context:component-scan base-package="service"/>
20     <context:component-scan base-package="test"/>
21
22     <context:property-placeholder location="classpath:dbcp.properties"/>
23     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
24         <property name="driverClassName" value="${driverClassName}" />
25         <property name="url" value="${url}" />
26         <property name="username" value="${mysqlusername}" />
27         <property name="password" value="${mysqlpassword}" />
28         <property name="maxActive" value="${maxActive}" />
29         <property name="maxIdle" value="${maxIdle}" />
30         <property name="minIdle" value="${minIdle}" />
31         <property name="maxWait" value="${maxWait}" />
32         <property name="initialSize" value="${initialSize}" />
33         <property name="logAbandoned" value="${logAbandoned}" />
34         <property name="removeAbandoned" value="${removeAbandoned}" />
35         <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
36         <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
37         <property name="numTestsPerEvictionRun" value="${numTestsPerEvictionRun}" />
38     </bean>
39
40     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
41         <property name="dataSource" ref="dataSource" />
42
43         <property name="hibernateProperties">
44             <props>
45                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
46                 <prop key="hibernate.show_sql">true</prop>
47                 <prop key="current_session_context_class">thread</prop>
48             </props>
49         </property>
50
51         <property name="packagesToScan">
52             <list>
53                 <value>po</value>
54             </list>
55         </property>
56     </bean>
57
58
59     <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
60     <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
61         <property name="sessionFactory" ref="sessionFactory"></property>
62     </bean>
63
64     <tx:advice id="txAdvice" transaction-manager="txManager">
65         <tx:attributes>
66             <tx:method name="find*" propagation="REQUIRED" />
67             <tx:method name="*" read-only="true"/>
68         </tx:attributes>
69     </tx:advice>
70
71     <aop:config proxy-target-class="true">
72         <!-- <aop:advisor advice-ref="txAdvice" pointcut="execution(* dao.*.*(..))"/> -->
73         <aop:pointcut expression="execution(* dao.*.*(..))" id="pointcut"/>
74         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
75     </aop:config>
76
77 </beans>

Spring中Propagation类的事务属性详解:

PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

PROPAGATION_SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。

PROPAGATION_MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。

PROPAGATION_REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。

PROPAGATION_NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

PROPAGATION_NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。

PROPAGATION_NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。

配置事务(声明方式)

需要在xml配制中设置<tx:annotation-driven transaction-manager="transactionManager">

 1 <?xml version="1.0" encoding="UTF-8"?>2 <beans3     xmlns="http://www.springframework.org/schema/beans"4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5     xmlns:p="http://www.springframework.org/schema/p"6     xmlns:context="http://www.springframework.org/schema/context"7     xmlns:aop="http://www.springframework.org/schema/aop"8     xmlns:tx="http://www.springframework.org/schema/tx"9     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
10     xmlns:cache="http://www.springframework.org/schema/cache"
11     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
12                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
13                         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
14                         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
15                         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
16                         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
17
18     <context:component-scan base-package="dao"/>
19     <context:component-scan base-package="service"/>
20     <context:component-scan base-package="test"/>
21
22     <context:property-placeholder location="classpath:dbcp.properties"/>
23     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
24         <property name="driverClassName" value="${driverClassName}" />
25         <property name="url" value="${url}" />
26         <property name="username" value="${mysqlusername}" />
27         <property name="password" value="${mysqlpassword}" />
28         <property name="maxActive" value="${maxActive}" />
29         <property name="maxIdle" value="${maxIdle}" />
30         <property name="minIdle" value="${minIdle}" />
31         <property name="maxWait" value="${maxWait}" />
32         <property name="initialSize" value="${initialSize}" />
33         <property name="logAbandoned" value="${logAbandoned}" />
34         <property name="removeAbandoned" value="${removeAbandoned}" />
35         <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
36         <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
37         <property name="numTestsPerEvictionRun" value="${numTestsPerEvictionRun}" />
38     </bean>
39
40     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
41         <property name="dataSource" ref="dataSource" />
42
43         <property name="hibernateProperties">
44             <props>
45                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
46                 <prop key="hibernate.show_sql">true</prop>
47                 <prop key="current_session_context_class">thread</prop>
48             </props>
49         </property>
50
51         <property name="packagesToScan">
52             <list>
53                 <value>po</value>
54             </list>
55         </property>
56     </bean>
57
58
59     <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
60         <property name="sessionFactory" ref="sessionFactory"></property>
61     </bean>
62     <tx:annotation-driven transaction-manager="txManager"/>
63
64 </beans>

事物注解方式: @Transactional

当标于类前时,标示类中所有方法都进行事物处理,以下代码在service层进行事务处理(给Service层配置事务是比较好的方式,因为一个Service层方法操作可以关联到多个DAO的操作。在Service层执行这些Dao操作,多DAO操作有失败全部回滚,成功则全部提交。)

 1 @Service2 @Transactional3 public class UserServiceImpl implements UserService {4     @Autowired5     private UserDao userDao;6     7     public User getUserById(int id) {8         return userDao.findUserById(id);9     }
10 }

当类中某些方法不需要事物时:

 1 @Service2 @Transactional3 public class UserServiceImpl implements UserService {4     @Autowired5     private UserDao userDao;6     7     @Transactional(propagation = Propagation.NOT_SUPPORTED)8     public User getUserById(int id) {9         return userDao.findUserById(id);
10     }
11 }

@Transactional(propagation=Propagation.REQUIRED)

如果有事务, 那么加入事务, 没有的话新建一个(默认情况下)
@Transactional(propagation=Propagation.NOT_SUPPORTED) 
容器不为这个方法开启事务
@Transactional(propagation=Propagation.REQUIRES_NEW) 
不管是否存在事务,都创建一个新的事务,原来的挂起,新的执行完毕,继续执行老的事务
@Transactional(propagation=Propagation.MANDATORY) 
必须在一个已有的事务中执行,否则抛出异常
@Transactional(propagation=Propagation.NEVER) 
必须在一个没有的事务中执行,否则抛出异常(与Propagation.MANDATORY相反)
@Transactional(propagation=Propagation.SUPPORTS) 
如果其他bean调用这个方法,在其他bean中声明事务,那就用事务.如果其他bean没有声明事务,那就不用事务.

事物超时设置:
@Transactional(timeout=30) //默认是30秒

事务隔离级别:
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
读取未提交数据(会出现脏读, 不可重复读) 基本不使用
@Transactional(isolation = Isolation.READ_COMMITTED)
读取已提交数据(会出现不可重复读和幻读)
@Transactional(isolation = Isolation.REPEATABLE_READ)
可重复读(会出现幻读)
@Transactional(isolation = Isolation.SERIALIZABLE)
串行化

MYSQL: 默认为REPEATABLE_READ级别
SQLSERVER: 默认为READ_COMMITTED

脏读 : 一个事务读取到另一事务未提交的更新数据
不可重复读 : 在同一事务中, 多次读取同一数据返回的结果有所不同, 换句话说, 
后续读取可以读到另一事务已提交的更新数据. 相反, "可重复读"在同一事务中多次
读取数据时, 能够保证所读数据一样, 也就是后续读取不能读到另一事务已提交的更新数据
幻读 : 一个事务读到另一个事务已提交的insert数据

Spring整合hibernate4:事务管理,布布扣,bubuko.com

Spring整合hibernate4:事务管理

标签:des   style   blog   http   color   使用

转载于:https://www.cnblogs.com/zhangkaikai/p/6856180.html

(转载)spring配置hibernate 事务。相关推荐

  1. Spring中配置Hibernate事务的四种方式

    2019独角兽企业重金招聘Python工程师标准>>> 为了保证数据的一致性,在编程的时候往往需要引入事务这个概念.事务有4个特性:原子性.一致性.隔离性.持久性. 事务的种类有两种 ...

  2. Spring4.x(9)--Spring的Hibernate事务-XML

    Spring的Hibernate事务-XML 一.拷贝必要的jar包到工程的lib目录 二.创建spring的配置文件并导入约束 <?xml version="1.0" en ...

  3. spring 配置只读事务_只读副本和Spring Data第1部分:配置数据库

    spring 配置只读事务 这是有关我们为利用只读副本来提高应用程序性能而寻求的一系列博客文章. 对于这个项目,我们的目标是建立我们的spring数据应用程序,并使用read仓库进行写操作,并基于re ...

  4. spring 配置只读事务_只读副本和Spring Data第3部分:配置两个实体管理器

    spring 配置只读事务 我们之前的设置可以正常工作. 我们现在要做的是进一步发展,并配置两个单独的实体管理器,而不会影响我们之前实现的功能. 第一步是将默认实体管理器配置设置为主要配置. 这是第一 ...

  5. Spring对Hibernate事务管理

    http://www.cnblogs.com/m-xy/archive/2013/05/14/3077627.html(挺好的) 还有一种用parent的配置方式,parent配置到哪个层面,事务就控 ...

  6. spring整合hibernate事务编程中错误分析

    2019独角兽企业重金招聘Python工程师标准>>> 在spring整合hibernate过程中,我们的配置文件: <?xml version="1.0" ...

  7. spring配置hibernate的sessionFactory

    1.Spring通过dbcp配置dataSource来配置sessionFactory   jdbc.properties #  oracle JDBC jdbc.driver=oracle.jdbc ...

  8. spring 中 Hibernate 事务和JDBC事务嵌套问题

    http://www.iteye.com/topic/11063?page=2 ---mixed ORM and JDBC usage is a feature of Spring DAO 这是Rod ...

  9. spring配置mysql事务管理_Spring 数据库事务管理机制

    要点1 Spring事务管理方法编程式事务(TransactionTemplate.PlatformTransactionManager) 声明式事务(配置式.注解式) 2 Spring 注解 @Tr ...

最新文章

  1. list @size 验证_第33期:上海自来水来自海上,回文字符串验证!
  2. R 语言绘制环状热图
  3. Jfinal集成Spring插件
  4. linux shell审计--snoopy的注意事项
  5. java第五章抽象类与接口5.1-5.3 2020.3.27+31
  6. 懂集合吗?对,是dart中的集合
  7. 无空头的链表代码:学生管理系统
  8. javafx中的tree_JavaFX中的塔防(2)
  9. C语言学习笔记(5)
  10. android滚动视图实例,android实现自定义滚动条
  11. 北京专业一般人小规模代理记账
  12. 张量基础学习(四 张量代数运算——下)
  13. linux环境下使用logrotate工具实现nginx日志切割
  14. Mac应用程序无法打开或文件损坏的处理方法
  15. JavaScript 验证码制作
  16. shoug oracle,oracle 浅谈索引
  17. xbox手柄_请不要通过Xbox Live判断白人
  18. CCF CSP 点亮数字人生(记忆化搜索+拓扑排序判环)
  19. 最终幻想:探讨小鹏G9 800V 高压动力系统和架构路线
  20. PHP fwrite写入文件,记事本打开乱码

热门文章

  1. 比较两个表格的不同_两表数据的核对,WPS表格似乎更加方便容易
  2. 【Pytorch神经网络实战案例】22 基于Cora数据集实现图注意力神经网络GAT的论文分类
  3. mac 批量清空文件夹文件_【XSS 聚宝瓶】文件及文件夹批量改名工具
  4. LeetCode 1878. 矩阵中最大的三个菱形和(模拟)
  5. LeetCode 1642. 可以到达的最远建筑(二分查找 / 优先队列贪心)
  6. LeetCode 第 187 场周赛(1336/3107,前43.0%)
  7. LeetCode 138. 复制带随机指针的链表(哈希 / 深拷贝)
  8. POJ 1007 DNA排序解题
  9. 如何在linux中使用u盘,如何在Linux系统下使用U盘
  10. 怎么样才能更高效的学习区块链