解惑 spring 嵌套事务

解惑 spring 嵌套事务

/**
* @author 王政
* @date 2006-11-24
* @note 转载请注明出处
*/

在所有使用 spring 的应用中, 声明式事务管理可能是使用率最高的功能了, 但是, 从我观察到的情况看,
绝大多数人并不能深刻理解事务声明中不同事务传播属性配置的的含义, 让我们来看一下 TransactionDefinition 接口中的定义

 

代码
  1. /**   
  2.      * Support a current transaction, create a new one if none exists.   
  3.      * Analogous to EJB transaction attribute of the same name.   
  4.      * <p>This is typically the default setting of a transaction definition.   
  5.      */   
  6.     int PROPAGATION_REQUIRED = 0;   
  7.   
  8.     /**   
  9.      * Support a current transaction, execute non-transactionally if none exists.   
  10.      * Analogous to EJB transaction attribute of the same name.   
  11.      * <p>Note: For transaction managers with transaction synchronization,   
  12.      * PROPAGATION_SUPPORTS is slightly different from no transaction at all,   
  13.      * as it defines a transaction scopp that synchronization will apply for.   
  14.      * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc)   
  15.      * will be shared for the entire specified scope. Note that this depends on   
  16.      * the actual synchronization configuration of the transaction manager.   
  17.      * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization   
  18.      */   
  19.     int PROPAGATION_SUPPORTS = 1;   
  20.   
  21.     /**   
  22.      * Support a current transaction, throw an exception if none exists.   
  23.      * Analogous to EJB transaction attribute of the same name.   
  24.      */   
  25.     int PROPAGATION_MANDATORY = 2;   
  26.   
  27.     /**   
  28.      * Create a new transaction, suspend the current transaction if one exists.   
  29.      * Analogous to EJB transaction attribute of the same name.   
  30.      * <p>Note: Actual transaction suspension will not work on out-of-the-box   
  31.      * on all transaction managers. This in particular applies to JtaTransactionManager,   
  32.      * which requires the <code>javax.transaction.TransactionManager</code> to be   
  33.      * made available it to it (which is server-specific in standard J2EE).   
  34.      * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager   
  35.      */   
  36.     int PROPAGATION_REQUIRES_NEW = 3;   
  37.   
  38.     /**   
  39.      * Execute non-transactionally, suspend the current transaction if one exists.   
  40.      * Analogous to EJB transaction attribute of the same name.   
  41.      * <p>Note: Actual transaction suspension will not work on out-of-the-box   
  42.      * on all transaction managers. This in particular applies to JtaTransactionManager,   
  43.      * which requires the <code>javax.transaction.TransactionManager</code> to be   
  44.      * made available it to it (which is server-specific in standard J2EE).   
  45.      * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager   
  46.      */   
  47.     int PROPAGATION_NOT_SUPPORTED = 4;   
  48.   
  49.     /**   
  50.      * Execute non-transactionally, throw an exception if a transaction exists.   
  51.      * Analogous to EJB transaction attribute of the same name.   
  52.      */   
  53.     int PROPAGATION_NEVER = 5;   
  54.   
  55.     /**   
  56.      * Execute within a nested transaction if a current transaction exists,   
  57.      * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.   
  58.      * <p>Note: Actual creation of a nested transaction will only work on specific   
  59.      * transaction managers. Out of the box, this only applies to the JDBC   
  60.      * DataSourceTransactionManager when working on a JDBC 3.0 driver.   
  61.      * Some JTA providers might support nested transactions as well.   
  62.      * @see org.springframework.jdbc.datasource.DataSourceTransactionManager   
  63.      */   
  64.     int PROPAGATION_NESTED = 6;   

 

可以看到 Savepoint 是 SavepointManager.createSavepoint 实现的, 再看 SavepointManager 的层次结构, 发现
其 Template 实现是 JdbcTransactionObjectSupport, 常用的 DatasourceTransactionManager, HibernateTransactionManager
中的 TransactonObject 都是它的子类 :



JdbcTransactionObjectSupport 告诉我们必须要满足两个条件才能 createSavepoint :

2. java.sql.Savepoint 必须存在, 即 jdk 版本要 1.4+
3. Connection.getMetaData().supportsSavepoints() 必须为 true, 即 jdbc drive 必须支持 JDBC 3.0


确保以上条件都满足后, 你就可以尝试使用 PROPAGATION_NESTED 了. (全文完)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值