在声明性事务管理中,无论事务是提交还是回滚,如何在 Spring 中获取事务信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13395794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:31:54  来源:igfitidea点击:

How do I get transaction info in Spring whether transaction is committed or rollback in a declarative transaction management?

springhibernatetransactions

提问by hari

I use following declarative Spring transaction:

我使用以下声明性 Spring 事务:

<!-- Declare a transaction manager-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />  
<!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="transactionManager" mode="proxy" proxy-target-class="true"/>

Here is the DAO:

这是 DAO:

@Repository
@Transactional(readOnly = true, propagation=Propagation.REQUIRES_NEW )
@Scope("prototype")
public class Xdao{

    public Object getValues(){
        .....
    }
}


@Service
@Scope("prototype")
public class Xservice{
private Xdao xdao;

    public Object getx(){
        xdao.getValues();//here I want to know whether the transaction started  is             
        //committed or rollback by aop. Is it possible somehow? I don't want to include that code 
        //in any service or dao layer. 
        .........
    }

    @Autowired
    public void setXdao(Xdao xdao){
        this.xdao=xdao;
    }
}

I want to know about transaction status i.e transaction is committed or rolled back. I need it for logging.

我想了解事务状态,即事务是提交还是回滚。我需要它来记录。

回答by Jose Luis Martin

If transaction is in scope you can get TransactionStatusfrom TransactionAspectSupport.currentTransactionStatus(). For example:

如果交易在范围内,您可以TransactionStatusTransactionAspectSupport.currentTransactionStatus(). 例如:

if (TransactionSynchronizationManager.isActualTransactionActive()) {
   TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();
   ...
}

But this will not work after transaction is completed.

但这在交易完成后不起作用。

You could add a TransactionSynchronizationand implement afterCompletion(int status)to log the status or store it in a ThreadLocalvariable for later usage.

您可以添加一个TransactionSynchronization并实现afterCompletion(int status)来记录状态或将其存储在ThreadLocal变量中以备后用。

public class LogTransactionSynchronization extends TransactionSynchronizationAdapter {
   @Override
   public afterCompletion(int status) {
      // log the status or store it for later usage
   }
}

回答by Muthu Chithambara Jothi

Adding the following to your log4j.properties will enable transaction status logging,

将以下内容添加到 log4j.properties 将启用事务状态日志记录,

log4j.logger.org.hibernate.transaction=DEBUG,R
log4j.logger.org.springframework.transaction=DEBUG,R