美文网首页
使用JPA拦截器查看事务的开始与结束

使用JPA拦截器查看事务的开始与结束

作者: 陈柴盐 | 来源:发表于2020-01-06 13:05 被阅读0次

首先我们看一下事务的传播行为有哪些,下面这个类记录了spring事务的所有传播行为

public enum Propagation {

    /**
     * Support a current transaction, create a new one if none exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>This is the default setting of a transaction annotation.
     */
    REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),

    /**
     * Support a current transaction, execute non-transactionally if none exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>Note: For transaction managers with transaction synchronization,
     * {@code SUPPORTS} is slightly different from no transaction at all,
     * as it defines a transaction scope that synchronization will apply for.
     * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc)
     * will be shared for the entire specified scope. Note that this depends on
     * the actual synchronization configuration of the transaction manager.
     * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
     */
    SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),

    /**
     * Support a current transaction, throw an exception if none exists.
     * Analogous to EJB transaction attribute of the same name.
     */
    MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),

    /**
     * Create a new transaction, and suspend the current transaction if one exists.
     * Analogous to the EJB transaction attribute of the same name.
     * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
     * on all transaction managers. This in particular applies to
     * {@link org.springframework.transaction.jta.JtaTransactionManager},
     * which requires the {@code javax.transaction.TransactionManager} to be
     * made available to it (which is server-specific in standard Java EE).
     * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
     */
    REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),

    /**
     * Execute non-transactionally, suspend the current transaction if one exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
     * on all transaction managers. This in particular applies to
     * {@link org.springframework.transaction.jta.JtaTransactionManager},
     * which requires the {@code javax.transaction.TransactionManager} to be
     * made available to it (which is server-specific in standard Java EE).
     * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
     */
    NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),

    /**
     * Execute non-transactionally, throw an exception if a transaction exists.
     * Analogous to EJB transaction attribute of the same name.
     */
    NEVER(TransactionDefinition.PROPAGATION_NEVER),

    /**
     * Execute within a nested transaction if a current transaction exists,
     * behave like {@code REQUIRED} otherwise. There is no analogous feature in EJB.
     * <p>Note: Actual creation of a nested transaction will only work on specific
     * transaction managers. Out of the box, this only applies to the JDBC
     * DataSourceTransactionManager. Some JTA providers might support nested
     * transactions as well.
     * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
     */
    NESTED(TransactionDefinition.PROPAGATION_NESTED);


    private final int value;


    Propagation(int value) {
        this.value = value;
    }

    public int value() {
        return this.value;
    }

}

常用的有REQUIRED和SUPPORTS这两种,但是如果你不敢肯定代码这种的事务是如何执行的,这时就可以使用JPA(hibernate)的拦截器,断定事务是重那里开始那里结束的

Part1:定义拦截器

@Component
@Log
public class JpaInterceptor extends EmptyInterceptor {

    @Override
    public void afterTransactionBegin(Transaction tx) {
        log.info("\n===============事务开启=============\n");
    }

    @Override
    public void afterTransactionCompletion(Transaction tx) {
        log.info("\n===============事务结束=============\n");
    }
    
}

其中@Log是lombok的注解

Part2:控制springboot使用拦截器

spring.jpa.properties.hibernate.session_factory.interceptor=xx.JpaInterceptor 

Part3:在需要使用事务的Service上添加@Transactional注解,@Transactional注解中的propagation可以控制事务的传播行为,举例如下

@Transactional(propagation = Propagation.SUPPORTS)

原创文章,转载请注明出处

相关文章

网友评论

      本文标题:使用JPA拦截器查看事务的开始与结束

      本文链接:https://www.haomeiwen.com/subject/axtkactx.html