spring事务传播

本文详细介绍了编程式和声明式事务管理的区别及配置方式,包括getCurrentSession()与openSession()的使用场景,以及Spring框架下事务的传播特性和隔离级别的配置。

采用编程式事务
1、 getCurrentSession()与openSession()的区别?
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession创建的session必须手动关闭
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事务(jdbc事务)
<property name=”hibernate.current_session_contex t_class”>thread</property>
* 如果使用的是全局事务(jta事务)
<property name=”hibernate.current_session_context_class”>jta</property>
采用声明式事务
声明式事务配置

* 配置SessionFactory
* 配置事务管理器
* 事务的传播特性
* 那些类哪些方法使用事务
2、编写业务逻辑方法
* 继承HibernateDaoSupport类,使用HibernateTemplate类持久化,HibernateTemplate
是Hibernate session的封装
* 默认的回滚是Runtime Exception(包括继承RuntimeException的子类),普通异常不回滚
* 在编写业务逻辑方法时,最好将异常一直往上抛出,在呈现层处理(str uts)
* spring的事务需要设置到业务方法上(事务边界定义到Facade类上),不要添加到Dao上
3、了解事务的集中传播忒性
1、PROPAGATION_REQUIRED:如果存在一个事务,则支持当前事务。如果没有事务则开启。
2、PROPAGATION_SUPPORTS:如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行。
3、PROPAGATION_MANDATORY:如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
4、PROPAGATION_REQUIRES_NEW:总是开启一个新的事务。如果一个事务存在,则将这个存在的事务挂起。
5、PROPAGATION_NOT_SUPPORTED:总是非事务地执行,并挂起任何存在的事务。
6、PROPAGATION_NEVER:总是非事务地执行,如果存在一个活动事务,则抛出异常。
7、 PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中,如果没有活动事务,则按TransactionDefinition.PROPAGATION_REQUIRED属性执行
4、Spring事务的隔离级别
1、 ISOLATION_DEFAULT: 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别。
2、ISOLATION_READ_UNCOMMITTED:这是事务最低的隔离级别,它允许另外一个事务可以看到这个事务未提交的数据。
3、ISOLATION_READ_COMMITTED:保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。
4、ISOLATION_REPEATALBE_READ: 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻想读。它除了保证一个事务不能读取另外一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。
5、ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不课重复读外,还避免了幻想读。
事务属性
T1
T2
Required

T1
T2
T1
RequiredNew

T1
T2
T2
Support

T1

T1
Mandatory

T1
抛异常
T1
NoSupport

T1


Never

T1

抛异常
à
<!-- 配置sessionFactory -- >
<bean id =”sessionFacory”class=”org.springframework.orm .hibernate3.LocalSessionFactoryBean”>
<property name=”configLocat ion”>
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器-- >
<beanid=”transactionManager”class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory”>
<ref bean=”sessionFactory”/>
</property>
</bean>
<!-- 配置事务的传播特性-- >
<tx:advice id=”txAdvice” transaction-manager=”transactionManager”>
<tx:attributes>
<tx:method name=”add *” propagation=”REQUIRED”/>
<tx:method name=”del*” propagation=”REQUIRED”/>
<tx:method name=”modify*” propagation=”REQUIRED”/>
<tx:method name=” *” read-only=”true”/> //只读提高了性能
</tx:attributes>
</tx:advice>
<!-- 哪些类的哪些方法参与事务-- >
<aop:config>
<aop:pointcut id=”allManagerMethod” expr ession=”execution(* com.bjsxt.usermgr.manager.*.*(..))”/>
<aop:advisor pointcut-ref=”allManagerMethod” advice-ref=”txAdvice”/>
</aop:config>

 

 

 

使用为

@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class)

 

05-23 16:00:49.311 25132-25512 A0c0d0/JSAPP I result {"code":200,"msg":"操作成功","data":[{"id":1,"name":"华为Mate 60 Pro","model":"ALN-AL80","imageUrl":"https://img.alicdn.com/imgextra/i3/2213047591560/O1CN01QNx6eB1NOWu3oeUfn_!!2213047591560.jpg_q50.jpg_.webp","shortDescription":"麒麟9000S 卫星通信","price":6999.00,"subsidizedPrice":6499.00,"sales":15000,"rating":4.90,"stock":500,"isAvailable":0,"brand":"华为"},{"id":2,"name":"华为P60 Art","model":"NOH-AN80","imageUrl":"https://img.alicdn.com/imgextra/i4/2609173245/O1CN011h2L1I1ZqGCdlsYlJ_!!2609173245.jpg_q50.jpg_.webp","shortDescription":"超聚光XMAGE影像","price":7988.00,"subsidizedPrice":7588.00,"sales":12000,"rating":4.80,"stock":300,"isAvailable":0,"brand":"华为"},{"id":3,"name":"华为nova 12","model":"BAC-AL00","imageUrl":"https://picasso.alicdn.com/imgextra/O1CNA1bnbda21Vuba4TjLYB_!!2838892713-0-psf.jpg_.webp","shortDescription":"前置双摄人像","price":2999.00,"subsidizedPrice":2699.00,"sales":50000,"rating":4.70,"stock":1000,"isAvailable":0,"brand":"华为"},{"id":4,"name":"荣耀Magic6 Pro","model":"PGT-AN30","imageUrl":"https://img.alicdn.com/imgextra/i2/263726286/O1CN01ko93gt1wJ2hQAORAB_!!4611686018427380942-0-item_pic.jpg_.webp","shortDescription":"骁龙8 Gen3 青海湖电池","price":5699.00,"subsidizedPrice":5399.00,"sales":25000,"rating":4.80,"stock":600,"isAvailable":0,"brand":"荣耀"},{"id":5,"name":"荣耀X50 GT","model":"ANY-AN00","imageUrl":"https://img.alicdn.com/imgextra/i3/3851598352/O1CN01UHPfyv2BZGwPnHUlx_!!0-item_pic.jpg_q50.jpg_.webp","shortDescription":"1.5K护眼曲屏","price":1999.00,"subsidizedPrice":1799.00,"sales":80000,"rating":4.60,"stock":1500,"isAvailable":0,"brand":"荣耀"},{"id":6,"name":"荣耀90 Pro","model":"REA-AN20","imageUrl":"https://img.alicdn.com/imgextra/i3/1114511827/O1CN01r3ngr41PMof7MhFhY_!!4611686018427386323-0-item_pic.jpg_q50.jpg_.webp","shortDescription":"2亿像素写真相机","price":3299.00,"subsidizedPrice":2999.00,"sales":40000,"rating":4.70,"stock":800,"isAvailable":0,"brand":"荣耀"},{"id":7,"name":"荣耀Play7T Pro","model":"CMA-AN40","imageUrl":"https://img.alicdn.com/imgextra/i1/1907069314/O1CN019jd3kt2IfrnZ6J0AH_!!1907069314.jpg_q50.jpg_.webp","shortDescription":"40W快充 OLED屏","price":1799.00,"subsidizedPrice":1599.00,"sales":120000,"rating":4.50,"stock":2000,"isAvailable":0,"brand":"荣耀"},{"id":8,"name":"华为畅享70 Pro","model":"CMA-AN70","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"7000mAh长续航","price":1599.00,"subsidizedPrice":1399.00,"sales":150000,"rating":4.40,"stock":3000,"isAvailable":0,"brand":"华为"},{"id":9,"name":"华为Mate X5","model":"TET-AN80","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"折叠屏 玄武钢化","price":12999.00,"subsidizedPrice":12499.00,"sales":5000,"rating":4.90,"stock":100,"isAvailable":0,"brand":"华为"},{"id":10,"name":"荣耀Magic Vs2","model":"CMA-AN80","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"轻薄折叠屏","price":8999.00,"subsidizedPrice":8499.00,"sales":10000,"rating":4.70,"stock":300,"isAvailable":0,"brand":"荣耀"},{"id":11,"name":"iPhone 15 Pro Max","model":"A3108","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"A17 Pro 钛金属","price":9999.00,"subsidizedPrice":null,"sales":80000,"rating":4.80,"stock":200,"isAvailable":0,"brand":"苹果"},{"id":12,"name":"iPhone 15","model":"A3092","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"A16芯片 灵动岛","price":5999.00,"subsidizedPrice":null,"sales":150000,"rating":4.80,"stock":5000,"isAvailable":0,"brand":"苹果"},{"id":13,"name":"iPhone 15 Plus","model":"A3096","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"6.7英寸大屏","price":6999.00,"subsidizedPrice":null,"sales":80000,"rating":4.70,"stock":3000,"isAvailable":0,"brand":"苹果"},{"id":14,"name":"iPhone SE 4","model":"A2785","imageUrl":"https://android-api.oss-cn-beijing.aliyun 05-23 16:00:49.311 25132-25512 A0c0d0/JSAPP I result [object Object] 05-23 16:00:49.359 25132-25512 C03f00/ArkCompiler E [ArkRuntime Log] TypeError: is not callable 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Lifetime: 0.000000s 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Js-Engine: ark 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]page: pages/Index.js 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Error message: is not callable 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Stacktrace: 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at forEachUpdateFunction (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:4352:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at anonymous (entry|entry|1.0.0|src/main/ets/pages/Index.ts:144:13) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at updateFunc (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6812:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at UpdateElement (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6514:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at anonymous (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6741:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at updateDirtyElements (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6736:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at rerender (entry|entry|1.0.0|src/main/ets/pages/Index.ts:152:9)
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值