Spring两种事物处理机制,一是声明式事物,二是编程式事物

声明式事物

1)Spring的声明式事务管理在底层是建立在AOP的基础之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。声明式事务最大的优点就是不需要通过编程的方式管理事务,这样就不需要在业务逻辑代码中掺杂事务管理的代码,只需在配置文件中做相关的事务规则声明(或通过等价的基于标注的方式),便可以将事务规则应用到业务逻辑中。因为事务管理本身就是一个典型的横切逻辑,正是AOP的用武之地。Spring开发团队也意识到了这一点,为声明式事务提供了简单而强大的支持。Spring强大的声明式事务管理功能,这主要得益于Spring依赖注入容器和Spring AOP的支持。依赖注入容器为声明式事务管理提供了基础设施,使得Bean对于Spring框架而言是可管理的;而Spring AOP则是声明式事务管理的直接实现者。和编程式事务相比,声明式事务唯一不足地方是,后者的最细粒度只能作用到方法级别,无法做到像编程式事务那样可以作用到代码块级别。但是即便有这样的需求,也存在很多变通的方法,比如,可以将需要进行事务管理的代码块独立为方法等等。

2)5种配置方式
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。
DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。
关系图如下:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>

<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
注意:sessionFactorty和transactionManager是下面5中配置方式的基本配置,

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

<!-- 配置DAO -->
<bean id="userDaoTarget" class="com.test.spring.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="userDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="userDaoTarget" />
<property name="proxyInterfaces" value="com.test.spring.dao.GeneratorDao" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
第二种方式:所有Bean共享一个代理基类
<bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<!-- 配置DAO -->
<bean id="userDaoTarget" class="com.test.spring.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="userDao" parent="transactionBase">
<property name="target" ref="userDaoTarget" />
</bean>
第三种方式:使用拦截器
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<!-- 配置DAO -->
<bean id="userDao" class="com.test.spring.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
第四种方式:使用tx标签配置的拦截器    
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="interceptorPointCuts"
expression="execution(* com.test.spring.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config>
第五种方式:全注解
public class test {
@Transactional
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

public List<User> listUsers() {
return null
}
}
}

编程式事务
Spring的编程式事务即在代码中使用编程的方式进行事务处理,可以做到比声明式事务更细粒度。有两种方式一是使用TransactionManager,另外就是TransactionTemplate。

1)TransactionManager使用方式

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
private HibernateTransactionManager transactionManager;
private DefaultTransactionDefinition def;

public HibernateTransactionManager getTransactionManager() {
return transactionManager;
}

public void setTransactionManager(HibernateTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}

public void createTransactionDefinition() {
def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
}

public void saveOrUpdate(User user) {
TransactionStatus status = transactionManager.getTransaction(def);
try {
this.getHibernateTemplate().saveOrUpdate(user);
} catch (DataAccessException ex) {
transactionManager.rollback(status);
throw ex;
}
transactionManager.commit(status);
}
}
2)TransactionTemplate方式

ResultDto ret = null;
ret = (ResultDto) this.transactionTemplate.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
ResultDto ret = null;
try {
drillTaskDao.deleteByKey(taskid);
} catch (Exception e) {
logger.error("delDrillTask:" + e.getMessage(), e);
ret = ResultBuilder.buildResult(ResultBuilder.FAIL_CODE, null, ErrorCode.COM_DBDELETEERROR);
return ret;
}
finally {
status.setRollbackOnly();
}

ret = cleartaskrelativedata(taskid, appid, true);
return ret;
}
});
return ret;

---------------------
原文:https://blog.csdn.net/pingnanlee/article/details/11488695

Spring 事物机制(总结)的更多相关文章

  1. Spring 事物机制

    Spring两种事物处理机制,一是声明式事物,二是编程式事物  声明式事物 1)Spring的声明式事务管理在底层是建立在AOP的基础之上的. 其本质是对方法前后进行拦截,然后在目标方法开始之前创建或 ...

  2. Spring 事物机制总结

    Spring两种事物处理机制,一是声明式事务,二是编程式事务 声明式事物 1)Spring的声明式事务管理在底层是建立在AOP的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加 ...

  3. 170608、Spring 事物机制总结

    spring两种事物处理机制,一是声明式事物,二是编程式事物 声明式事物 1)Spring的声明式事务管理在底层是建立在AOP的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加 ...

  4. 170323、Spring 事物机制总结

    spring两种事物处理机制,一是声明式事物,二是编程式事物 声明式事物 1)Spring的声明式事务管理在底层是建立在AOP的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加 ...

  5. 170110、Spring 事物机制总结

    spring两种事物处理机制,一是声明式事物,二是编程式事物 声明式事物 1)Spring的声明式事务管理在底层是建立在AOP的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加 ...

  6. spring事物的传播行为

    1.spring事物的传播行为,主要是用来解决业务层拥有事物的方法,相互调用的问题. 2.声明事物, 在代码执行前,开启事务.代码执行完,提交事务 3.spring并没有提供事务具体的处理,而只是调用 ...

  7. Spring 事物Transaction

    日常开发中Spring 为我们提供了两种事物的定义方式 XML 配置 方式 :这种方式配置起来比较麻烦,但后期比较好进行维护 注解方式:配置起来比较方便,也是日常开发常用的: 我们这里进行第二种注解的 ...

  8. spring 事物的一些理解

    推荐一个我认为Spring事物写得很好的文章. 文章链接:http://www.codeceo.com/article/spring-transactions.html  文章作者:码农网 – 吴极心 ...

  9. Hibernate工作原理及为什么要用?. Struts工作机制?为什么要使用Struts? spring工作机制及为什么要用?

    三大框架是用来开发web应用程序中使用的.Struts:基于MVC的充当了其中的试图层和控制器Hibernate:做持久化的,对JDBC轻量级的封装,使得我们能过面向对象的操作数据库Spring: 采 ...

随机推荐

  1. RedHat Linux6.4下安装apache服务

    一.换yum 原因:安装apache2.4是需要安装apr . apr-util .pcre.httpd四个包, 在安装pcre包时会报错: configure: error: You need a ...

  2. 04 全局配置,java 编意默认版本,1.6.修改配置

    https://www.cnblogs.com/liu-s/p/5371289.html <!-- 修改Intellij Idea 创建maven项目默认Java编译版本 --> < ...

  3. dos编辑文件上传到unix系统多余^M删除方法

    linux上的文件sz到window编辑后多出^M, 方法一: 1.grep -anR '^M' filename |wc -l2.crontab -e 或vim filename3.:set ff  ...

  4. C# Label换行解决方法

    一.label太短,无法完成显示所要显示信息长度,要换行,解决方法如下: (1) string aa =(长串) ; string cc= aa.Substring(0,10);//取前10个字符 s ...

  5. Zsh vs. Bash不完全对比解析,zsh是一种更强大的被成为“终极”的Shell

    https://www.zhihu.com/question/21418449 Mort | Zsh vs. Bash:不完全对比解析(1) 2014-10-07  bdpqlxz     Zsh和B ...

  6. 清北学堂Day 6之STL

    电脑突然一炸,什么都没有保存,凉了.(又出现了笔记凉凉事件嘤嘤嘤) 行吧慢慢回忆 就算我们会手写,我们也要学STL.吸了O2的STL可是要上天的. 数据结构 pair 使用方式: pair<类型 ...

  7. mysql错误:1093-You can’t specify target table for update in FROM clause的解决方法

    update语句中包含的子查询的表和update的表为同一张表时,报错:1093-You can’t specify target table for update in FROM clause my ...

  8. 测开之路七十六:linux变量和环境变量

    变量 赋值 variable=0,访问 $var或${var} 参数 $n 用``引住的会先执行(~键) 位置参数 环境变量/etc/profile:全局的环境变量 . bash_profile:用户 ...

  9. CodeIgniter 技巧 - 通过 Composer 安装 CodeIgniter 框架并安装依赖包

    PHP 项目中,通过 Composer 来管理各种依赖包,类似 Java 中的 Maven,或 Node 中的 npm.CodeIgniter 框架要想通过 Composer 自动加载包也很简单,步骤 ...

  10. Ural Amount of Degrees(数位dp)

    传送门 Amount of Degrees Time limit: 1.0 secondMemory limit: 64 MB Description Create a code to determi ...