1. 配置Spring3声明式事务
在Spring3中配置声明式事务比早期版本显得更加简便。只需要几行配置文件+注解就可以实现面向切面的AOP事务
2. 配置文件
在Spring的配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName">
<!-- 配置Spring上下文的注解 -->
<context:annotation-config />
<!-- 配置DAO类 -->
<bean id="persondao" class="impl.PersonDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务管理配置 -->
<!-- 配置事务管理开始 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
在配置文件中并没有出现像以前版本的事务传播和隔离级别,也就是类似如下配置
<!-- 配置事务的传播特性 -->
<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>
因为可以通过注解@Transactional完成事务传播、事务隔离的功能。
3. DAO代码
DAO实现类代码如下
package impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import pojo.Test;
@Transactional
public class PersonDAOImpl extends HibernateDaoSupport implements PersonDAO {
public void save(Test test) {
this.getHibernateTemplate().saveOrUpdate(test);
Test test2 = new Test();
test2.setId(3);
test2.setName("3");
this.getHibernateTemplate().saveOrUpdate(test2);
throw new RuntimeException("主键重复");
}
}
使用注解将此DAO实现类的所有方法进行了事务拦截,当遇到运行是异常时,事务回滚。不提交到数据库。
当然可以指定单一原子方法进行隔离,方法可以指定
@Transactional(propagation=Propagation.NOT_SUPPORTED)
@Transactional(propagation=Propagation.MANDATORY)
@Transactional(propagation=Propagation.NESTED)
@Transactional(propagation=Propagation.REQUIRED)
@Transactional(propagation=Propagation.REQUIRES_NEW)
@Transactional(propagation=Propagation.SUPPORTS)
同JTA标准的事务隔离传播一样,具体参考
http://suhuanzheng7784877.iteye.com/blog/908382
之后在Service层可以进行原子操作的业务整合。
分享到:
相关推荐
Spring3配置声明式事务[借鉴].pdf
spring3,hibernate4 配置声明式事务管理(annotation方式)
Spring声明式事务配置管理方法
Spring使用TransactionProxyFactoryBean声明式事务配置实例
<?xml version="1.0" encoding="UTF-8"?><beansxmlns=...
一个实例小工程,说的是spring3+hibernate4怎么配置声明式事务管理(xml方式)
使用@Transactional注解时,可以通过参数配置事务详情: 5.2.2 基于Annotation方式的声明式事务 * * * * * * * * 声明式事务管理 如何实现Spring的声明式事务管理? 5.2 声明式事务管理 Spring的声明式事务管理可以...
Spring使用XML配置声明式事务 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45478007
1. 基于Aspectj实现动态数据源...6. 实现事务内切换数据源(支持原生Spring声明式事务哟,仅此一家),并支持多数据源事务回滚(有了它除了跨服务的事务你需要考虑分布式事务,其他都不需要,极大的减少了系统的复杂程度)
Spring ax/aop声明式事务配置实例
Spring_Hibernate使用TransactionInterceptor声明式事务配置.doc
Xml文件配置实现声明式事务管理 Xml文件配置实现声明式事务管理
1.掌握Myeclipse的使用。 2.掌握spring框架和hibernate框架的使用。 3. 掌握整合spring和hibernate的持久化操作编程 4.掌握基于AOP的声明式事务编程...3.配置WEB-INF/applicationContext.xml提供基于AOP的声明式事务
Java高级编程 实验报告 spring 声明事务 实验目的 掌握spring 声明式事务管理配置 实验环境 本实验采用本实验采用的eclipse或者 Myeclpse开发工具。Spring 4.0以上 Jdk1.7以上、oracle/mysql。
spring3、 hibernate4 配置声明式事务管理(annotation方式)
本文系统的介绍了Spring中的四种声明式事务的配置。可应用于实践项目中。
示例代码 博文链接:https://awaken2012.iteye.com/blog/1728283
spring声明式事务的配置 3. spring2.0配置事务 a) 将spring 1.2升级到spring2.0 i. 去掉spring1.2相关的包 ii. 添加spring2.0的jar包:spring.jar,aspecjrt.jar,aspectjweaver.jar 和cglib-nodep-2.1.3,jar iii. ...