Transactional annotation on whole class + excluding a single method

后端 未结 2 2052
忘掉有多难
忘掉有多难 2021-01-01 11:47

I have a class with @Transactional annotation (instead of marking it for all of its method).

Although i have a single method inside that class that sho

2条回答
  •  Happy的楠姐
    2021-01-01 12:22

    There are different transaction propagation strategies to use. These exist in the enum Propagation. The ones you might want to use are

    /**
     * Execute non-transactionally, suspend the current transaction if one exists.
     * Analogous to EJB transaction attribute of the same name.
     * 

    Note: Actual transaction suspension will not work on out-of-the-box * on all transaction managers. This in particular applies to JtaTransactionManager, * which requires the {@code javax.transaction.TransactionManager} to be * made available it to it (which is server-specific in standard J2EE). * @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), // maybe not this one

    So annotate the method inside your class with either of these.

    @Transactional
    public class MyTransactionalClass { 
        @Transactional(propagation = Propagation.NOT_SUPPORTED)
        public void nonTransactionalMethod() {...}
    }
    

    You can find all the propagation strategies here.

提交回复
热议问题