Spring中如何配置Hibernate事务

转自:http://www.open-open.com/lib/view/open1372570081941.html

 为了保证数据的一致性,在编程的时候往往需要引入事务这个概念。事务有4个特性:原子性、一致性、隔离性、持久性。

         事务的种类有两种:编程式事务和声明式事务。编程式事务就是将事务处理放在程序中,而声明式事务则是通过配置文件或者注解进行操作。

         在Spring中有声明式事务的概念,通过和Hibernate类似框架的集成,可以很好的完成声明式事务。

         其实,不论在Spring中有几种配置Hibernate事务的方法,都逃不出一下几条:

         1.配置SessionFactory

         2.配置事务容器

         3.配置事务规则

         4.配置事务入口

         后面一共为大家提供4种配置Hibernate事务的方法。

         首先说下配置SessionFactory,配置SessionFactory有两种方式,一种是通过配置hibernate.cfg.xml文件的位置来配置SessionFactory,另一种就是在Spring配置文件中,手动配置数据源。

         下面是两种配置SessionFactory的方式(第二种配置需要额外引入两个包:commons-dbcp、commons-pool)

?
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
<!-- 1、第一种配置SessionFactory的方式 -->
< bean id = "sessionFactory"
     class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
     < property name = "configLocation" value = "classpath:hibernate.cfg.xml" />
</ bean >
 
<!-- 2、第二种配置SessionFactory的方式 -->
<!-- 2.1配置数据源 -->
< bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource"
     destroy-method = "close" >
     < property name = "driverClassName" value = "com.mysql.jdbc.Driver" ></ property >
     < property name = "url" value = "jdbc:mysql://localhost:3306/hibernate_cache" ></ property >
     < property name = "username" value = "root" ></ property >
     < property name = "password" value = "admin" ></ property >
</ bean >
<!-- 2.2、配置SessionFactory -->
< bean id = "sessionFactory"
     class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
     < property name = "dataSource" ref = "dataSource" ></ property >
     < property name = "hibernateProperties" >
         < props >
             < prop key = "hibernate.hbm2ddl.auto" >update</ prop >
         </ props >
     </ property >
     < property name = "mappingLocations" >
         < list >
             < value >classpath:实体对应xml的路径</ value >
         </ list >
     </ property >
</ bean >

         至此Hibernate就成功的将SessionFactory交给了Spring来管理。现在再来看Spring是怎样管理Hibernate事务的吧。

         第一种方式,利用tx标签配置事务。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- 配置事务容器 -->
< bean id = "transactionManager"
     class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
     < property name = "sessionFactory" ref = "sessionFactory" />
</ bean >
<!-- 定义事务规则 -->
< tx:advice id = "txAdvice" transaction-manager = "transactionManager" >
     < tx:attributes >
         < tx:method name = "add*" propagation = "REQUIRED" />
         < tx:method name = "modify*" propagation = "REQUIRED" />
         < tx:method name = "del*" propagation = "REQUIRED" />
         < tx:method name = "*" propagation = "REQUIRED" read-only = "true" />
     </ tx:attributes >
</ tx:advice >
<!-- 定义事务入口 -->
< aop:config >
     < aop:pointcut id = "allDaoMethod" expression = "execution(* com.jianxin.dao.*.*(..))" />
     < aop:advisor advice-ref = "txAdvice" pointcut-ref = "allDaoMethod" />
</ aop:config >

         第二种,用代理进行配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- 配置事务容器 -->
< bean id = "transactionManager"
     class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
     < property name = "sessionFactory" ref = "sessionFactory" />
</ bean >
<!-- 定义事务规则 -->
< bean id = "transactionProxy"
     class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
     abstract = "true" >
     < property name = "transactionManager" ref = "transactionManager" />
     < property name = "transactionAttributes" >
         < props >
             <!-- ,回滚为-,不回滚为+ -->
             < prop key = "add*" >PROPAGATION_REQUIRED,-Exception</ prop >
             < prop key = "modify*" >PROPAGATION_REQUIRED,+MyException</ prop >
             < prop key = "del*" >PROPAGATION_REQUIRED</ prop >
             < prop key = "*" >READONLY</ prop >
         </ props >
     </ property >
</ bean >
<!-- 定义事务入口 -->
< bean id = "userDaoProxy" parent = "transactionProxy" >
     < property name = "target" ref = "userDao" ></ property >
</ bean >

         第三种,利用拦截器

?
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
<!-- 配置事务容器 -->
< bean id = "transactionManager"
     class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
     < property name = "sessionFactory" ref = "sessionFactory" />
</ bean >
<!-- 定义事务规则 -->
< bean id = "transactionInterceptor"
     class = "org.springframework.transaction.interceptor.TransactionInterceptor" >
     < property name = "transactionManager" ref = "transactionManager" />
     < property name = "transactionAttributes" >
         < props >
             <!-- 回滚为-,不回滚为+ -->
             < prop key = "add*" >PROPAGATION_REQUIRED,-Exception</ prop >
             < prop key = "modify*" >PROPAGATION_REQUIRED,+MyException</ prop >
             < prop key = "del*" >PROPAGATION_REQUIRED</ prop >
             < prop key = "*" >READONLY</ prop >
         </ props >
     </ property >
</ bean >
<!-- 定义事务入口 -->
< bean id = "proxyFactory"
     class = "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
     < property name = "interceptorNames" >
         < list >
             < value >transactionInterceptor</ value >
         </ list >
     </ property >
     < property name = "beanNames" >
         < list >
             < value >*Dao</ value >
         </ list >
     </ property >
</ bean >

         第四种,利用注解。

         首先,在配置文件中写入下面语句,打开注解功能

?
1
2
<!-- 开户事务注解功能 -->
< tx:annotation-driven transaction-manager = "transactionManager" />

         然后用@Transactional对类或者方法进行标记,如果标记到类上,那么次类中所有方法都进行事务回滚处理,在类中定义Transactional的时候,它有propagation、rollbackFor、noRollbackFor等属性,此属性是用来定义事务规则,而定义到哪这个就是事务入口。

         纵观以上四种在Spring中配置Hibernate事务的方法,其核心都是一样的,不同的只是实现的方式而已。所以看到这,这篇博文中你只需要记住4句话,就可以轻松理解在Spring中配置Hibernate事务的核心:

         1.配置SessionFactory

         2.配置事务容器

         3.配置事务规则

         4.配置事务入口


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值