当前位置:编程学习 > JAVA >>

Spring3开发实战 之 第五章:Spring中的事务

Spring框架引人注目的重要因素之一是它全面的事务支持。Spring框架提供了一致的事务管理抽象,这带来了以下好处:
1:为复杂的事务API提供了一致的编程模型,如JTA、JDBC、Hibernate、JPA和JDO
2:支持声明式事务管理
3:提供比复杂的事务API(诸如JTA)更简单的、更易于使用的编程式事务管理API
4:非常好地整合Spring的各种数据访问抽象
Spring事务抽象的关键是事务策略的概念。这个概念由org.springframework.transaction.PlatformTransactionManager接口定义:

java代码:
查看复制到剪贴板打印
public interface PlatformTransactionManager { 
    TransactionStatus getTransaction(TransactionDefinition definition) 
        throws TransactionException; 
    void commit(TransactionStatus status) throws TransactionException; 
    void rollback(TransactionStatus status) throws TransactionException; 

ge 

tTransaction(..)方法根据一个类型为 TransactionDefinition 的参数返回一个 TransactionStatus 对象。返回的 TransactionStatus 对象可能代表一个新的或已经存在的事务(如果在当前调用堆栈有一个符合条件的事务。如同J2EE事务环境,一个 TransactionStatus 也是和执行 线程 绑定的)
TransactionDefinition接口指定
1:事务隔离:当前事务和其它事务的隔离的程度。例如,这个事务能否看到其他事务未提交的写数据?
2:事务传播:通常在一个事务中执行的所有代码都会在这个事务中运行。但是,如果一个事务上下文已经存在,有几个选项可以指定一个事务性方法的执行行为:例如,简单地在现有的事务中继续运行(大多数情况);或者挂起现有事务,创建一个新的事务。Spring提供EJB CMT中常见的事务传播选项。
3:事务超时: 事务在超时前能运行多久(自动被底层的事务基础设施回滚)。
4:只读状态: 只读事务不修改任何数据。只读事务在某些情况下(例如当使用Hibernate时),是一种非常有用的优化。
 
高层次方案:
首选的方法是使用Spring的高层持久化集成API。这种方式不会替换原始的API,而是在内部封装资源创建、复用、清理、事务同步以及异常映射等功能,这样用户的数据访问代码就不必关心这些,而集中精力于自己的持久化逻辑。
低层次方案 :
在较低层次上,有以下这些类:DataSourceUtils(针对JDBC),SessionFactoryUtils(针对Hibernate),PersistenceManagerFactoryUtils(针对JDO)等等。也就是说,在纯JDBC中,如果你需要使用Spring的事务,那么就需要使用上面的类来管理连接,如下:
Connection conn=DataSourceUtils.getConnection(dataSource);
当然,一旦你用过Spring的JDBC支持或Hibernate支持,你一般就不再会选择 DataSourceUtils 或是别的辅助类了,因为你会更乐意与Spring抽象一起工作,而不是直接使用相关的API。

 
 
Spring框架提供了一致的事务管理抽象,通常实施事务的步骤是:
1:定义资源(DataSource/SessionFactory……)
2:定义事务管理器(管理资源的事务)
3:定义事务通知:定义了如何实施事务(实施事务的方法名和对应的事务属性),需要使用事务管理器管理事务,定义了如何选择目标对象的方法及实施的事务属性
4:定义advisor(切入点和事务通知):切入点选择需要实施事务的目标对象(通常是业务逻辑层)
5:Spring织入事务通知到目标对象(AOP代理)
要使用Spring的事务,首先要先定义一个与数据源连接的DataSource,然后使用Spring的DataSourceTransactionManager,并传入指向DataSource的引用,比如,JDBC的事务配置如下:

java代码:
查看复制到剪贴板打印
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
<property name="driverClassName"> 
<value>oracle.jdbc.driver.OracleDriver</value></property> 
<property name="url"> 
<value>jdbc:oracle:thin:@localhost:1521:orcl</value></property> 
<property name="username"> <value>test</value> </property> 
<property name="password" value="test"/> 
</bean> 
<bean id="txManager" 
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> 
</bean> 
如果使用JTA,示例如下:

java代码:
查看复制到剪贴板打印
<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:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:jee="http://www.springframework.org/schema/jee" 
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
"> 
  
  <jee:jndi-lookup id="dataSource" jndi-name="jdbc/jpetstore"/> 
  <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" /> 
</beans> 
Hibernate的事务配置,示例如下:
1:Spring对Hibernate的集成
2:然后配置事务如下:

java代码:
查看复制到剪贴板打印
<bean id="txManager“ 
class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 
可以简单地使用 JtaTransactionManager 来处理Hibernate事务和JTA事务,就像我们处理JDBC,或者任何其它的资源策略一样,如下:

java代码:
查看复制到剪贴板打印
<bean id="txManager" 
class="org.springframework.transaction.jta.JtaTransactionManager"/> 
注意任何资源的JTA配置都是这样的,因为它们都是全局事务,可以支持任何事务性资源。在所有这些情况下,应用程序代码根本不需要做任何改动。仅仅通过改变配置就可以改变事务管理方式,即使这些更改是在局部事务和全局事务间切换。
Spring的声明式事务管理是通过AOP实现的,Xml风格的声明式事务配置如下:

java代码:
查看复制到剪贴板打印
<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:aop="http://www.springframework.org/schema/aop" 
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
<property name="dataSource" ref="dataSource"/>&n

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,