您的位置:首页 > 编程语言 > Java开发

Spring中事务的定义

2011-07-04 23:08 162 查看
转载自:http://www.blogjava.net/280211429/articles/75529.html,推荐看原文页面,原页面的CSS比较好看,看文字也非常舒服

Spring中事务的定义:

一、Propagation :

对于特定的方法或方法命名模式,代理的具体事务行为由事务属性驱动,如下面的例子所示:
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>   key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:
PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
      前六个策略类似于EJB CMT:常量名相同,因此,对EJB开发人员来说,应该立刻就感到熟悉。第七个(PROPAGATION_NESTED)是Spring所提供的一个特殊变量。它要求事务管理器或者使用JDBC 3.0 Savepoint API提供嵌套事务行为(如Spring的DataSourceTransactionManager),或者通过JTA支持嵌套事务。
    
二、Isolation Level(事务隔离等级):
1、Serializable:最严格的级别,事务串行执行,资源消耗最大;
2、REPEATABLE READ:保证了一个事务不会修改已经由另一个事务读取但未提交(回滚)的数据。避免了“脏读取”和“不可重复读取”的情况,但是带来了更多的性能损失。
3、READ COMMITTED:大多数主流数据库的默认事务等级,保证了一个事务不会读到另一个并行事务已修改但未提交的数据,避免了“脏读取”。该级别适用于大多数系统。
4、Read Uncommitted:保证了读取过程中不会读取到非法数据。

spring中的Isolation属性:
1、ISOLATION_DEFAULT :使用当前数据源的默认级别
2、ISOLATION_READ_UNCOMMITTED :Dirty reads, non-repeatable reads, and phantom reads can occur.
3、ISOLATION_READ_COMMITTED :Dirty reads are prevented; non-repeatable reads and phantom reads can occur.
4、ISOLATION_REPEATABLE_READ:Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5、ISOLATION_SERIALIZABLE:Dirty reads, non-repeatable reads, and phantom reads are prevented.

三、readOnly
事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务。这是一个最优化提示。在一些情况下,一些事务策略能够起到显著的最优化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。

四、Timeout
      在事务属性中还有定义“timeout”值的选项,指定事务超时为几秒。在JTA中,这将被简单地传递到J2EE服务器的事务协调程序,并据此得到相应的解释。

事务划分策略

1、推荐在业务层使用事务,这样可以允许业务层捕获导致rollback的异常,并抛出恰当的业务层异常;不在dao层使用事务是因为这会限制了dao重用其他事务需求,并且dao层没有实现业务逻辑,并且原子性也是业务层的概念。

spring声明性事务的划分:
1、有四个地方需要配置:The four participants are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.

2、使用ProxyFactoryBean/Transaction Interceptor(transactionInterceptor)配置spring事务

以下为配置实例:
<!-- The DBCP DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

<!-- The DAO class -->
<bean id="dao"
   class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- The transactionmanager to use for regular non JTA datasource -->
  <bean id="transactionManager"   
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- TransactionInterceptor -->
  <bean id="transactionInterceptor"     
   class="org.springframework.transaction.interceptor.TransactionInterceptor">    <property name="transactionManager">      <ref bean="transactionManager"/>    </property>    <property name="transactionAttributeSource">      <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED
</value>
</property>
</bean>

<!-- Transactional proxy for the primary business object -->
  <bean id="boxOffice"         
   class="org.springframework.aop.framework.ProxyFactoryBean">    <property name="target">      <ref local="boxOfficeTarget"/>    </property>    <property name="proxyInterfaces">      <value>org.springframework.prospring.ticket.service.BoxOffice</value>    </property>    <property name="interceptorNames">      <value>transactionInterceptor</value>    </property>
</bean>

<!-- Business Object -->
<bean id="boxOfficeTarget"
class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
<property name="boxOfficeDao">
<ref local="dao"/>
</property>
</bean>3、使用TransactionProxyFactoryBean配置spring事务
以下为配置实例: <!-- The DBCP DataSource -->
<bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

<!-- The DAO class -->
<bean id="dao"
class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- The transactionmanager to use for regular non JTA datasource -->
  <bean id="transactionManager"    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- Transactional proxy and the primary business object -->
  <bean id="boxOffice"   
   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">    <property name="transactionManager"><ref bean="transactionManager"/></property>    <property name="target">      <bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
<property name="boxOfficeDao">
<ref local="dao"/>
</property>
</bean>
</property>
    <property name="transactionAttributes">      
      <props>        
         <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>        
         <prop key="allocate*">PROPAGATION_REQUIRED</prop>      
      </props>    
   </property>
</bean> 4、使用BeanNameAutoProxyCreator配置spring事务
如果有大量的bean需要使用事物,那么只要在配置文件中提供bean name给BeanNameAutoProxyCreator,spring就会个给该bean提供事务代理,配置实例如下:
<!-- The DBCP DataSource -->
<bean id="dataSource"
   class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

<!-- The DAO class -->
<bean id="dao"
   class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- The transactionmanager to use for regular non JTA datasource -->
  <bean id="transactionManager"    
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- TransactionInterceptor -->
  <bean id="transactionInterceptor"         class="org.springframework.transaction.interceptor.TransactionInterceptor">    <property name="transactionManager">      <ref bean="transactionManager"/>    </property>    <property name="transactionAttributeSource">      <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED      </value>    </property>
</bean>

<!-- BeanNameAutoProxyCreator -->
<bean id="autoProxyCreator"    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  <property name="interceptorNames">    <value>transactionInterceptor</value>  </property>  <property name="beanNames">    <list>      <idref local="boxOffice"/>    </list>  </property>
</bean>

<!-- Business Object -->
<bean id="boxOffice"
class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
<property name="boxOfficeDao">
<ref local="dao"/>
</property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息