Spring事务管理学习

2017-04-16  本文已影响0人  漠简尘

一、spring事务管理架构

image

1. PlatformTranscationManager

2. TranscationStatus

3. TranscationManager

4. TranscationDefinition

image

5. 事务传播行为

behavior throw exception new transaction suspend curent transaction
not_suport no no yes
supports no no no
required no null -> yes no
require_new no yes yes
nested no yes yes
never yes no no
mandatory yes no no
** Definition
脏读 A事务读取了B事务修改了但还没有提交的数据,但是B事务因为失败而回滚了,这个时候A事务读取的数据是无效的
不可重复读 A事务进行了同样的两次查询,但是获得的数据不一样,一般是因为两次查询中间有B事务对数据进行了更新
幻读 类似于不可重复读,但是重点是B事务对原数据进行了增删而不是修改,不可重复读侧重于修改
隔离级别 含义 作用(阻止了以上那个问题的发生)
default 使用数据库默认的隔离级别 --
read_uncommitted 允许读取未提交的数据 --
read_committed 只允许读取已提交的数据 脏读
repetable_read 多次读取同一字段得到同样的数据,除非是本身修改 脏读、不可重复读
serializable 串行化执行事务操作 脏读、不可重复读、幻读

6. 回滚规则

7. 是否只读

8. 事务超时

二 、 Spring编程式事务

1. 使用TransactionTemplate

image
TranscationTemplate 类图
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName">  
        <value>com.mysql.jdbc.Driver</value>  
    </property>  
    <property name="url">  
        <value>${jdbc.mgr.url}</value>  
    </property>  
    <property name="username">  
        <value>${jdbc.mgr.user}</value>  
    </property>  
    <property name="password">  
        <value>${jdbc.mgr.password}</value>  
    </property>  
</bean>
<bean id="sessionFactory"  
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
    <property name="dataSource" ref="dataSource"/>  
    <property name="packagesToScan">  
        <list>  
            <!-- 可以加多个包 -->  
            <value>com.cuilei01.mgr.utils</value>  
        </list>  
    </property>  
    <property name="hibernateProperties">  
        <props>  
  
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>  
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
        </props>  
    </property>  
</bean>  
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
    <property name="sessionFactory" ref="sessionFactory"></property>  
</bean>  
<!-- 配置transactionTemplate -->  
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">  
    <property name="transactionManager" ref="transactionManager"></property>  
    <!--定义事务隔离级别,-1表示使用数据库默认级别-->  
    <property name="readOnly" value="false"></property>  
    <property name="isolationLevelName" value="ISOLATION_DEFAULT"></property>  
    <property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"></property>  
</bean>  
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})  
@RunWith(SpringJUnit4ClassRunner.class)  
public class TransactionTest {  
  
    private final Logger logger = LoggerFactory.getLogger(TransactionTest.class);  
  
    @Resource  
    private TransactionTemplate transactionTemplate;  
  
    @Test  
    public void testProgrammaticTransaction() {  
        logger.info("Begin test programmatic transaction!########################");  
        // 第一个事务  
        Integer result = transactionTemplate.execute(new TransactionCallback<Integer>() {  
            @Override  
            public Integer doInTransaction(TransactionStatus status) {  
                logger.info("Do in transaction with a return value!#####################################");  
                // 在事务中执行, 有返回值  
                return 1;  
            }  
        });  
  
        logger.info("result:{}", result);  
  
        // 第二个事务  
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {  
            @Override  
            protected void doInTransactionWithoutResult(TransactionStatus status) {  
                logger.info("Do in transaction without a return value!#####################################");  
                // 在事务中执行,没有返回值  
            }  
        });  
    }  

三、Spring声明式事务管理

属性 类型 描述
value String 可选的限定描述符,指定使用的事务管理器
propagation enum: Propagation 可选的事务传播行为设置
isolation enum: Isolation 可选的事务隔离级别设置
readOnly boolean 读写或只读事务,默认读写
timeout int (in seconds granularity) 事务超时时间设置
rollbackFor Class对象数组,必须继承自Throwable 导致事务回滚的异常类数组
rollbackForClassName 类名数组,必须继承自Throwable 导致事务回滚的异常类名字数组
noRollbackFor Class对象数组,必须继承自Throwable 不会导致事务回滚的异常类数组

noRollbackForClassName 类名数组,必须继承自Throwable 不会导致事务回滚的异常类名字数组

上一篇 下一篇

猜你喜欢

热点阅读