Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I use following declarative Spring transaction:

<!-- 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:

@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.

like image 753
hari Avatar asked Nov 15 '12 10:11

hari


People also ask

What is use of @transactional annotation in Spring?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

Does Spring provide declarative transaction management?

The Spring Framework enables declarative transaction management to be applied to any class, not merely special classes such as EJBs. The Spring Framework offers declarative rollback rules: this is a feature with no EJB equivalent. Both programmatic and declarative support for rollback rules is provided.

Does @transactional commit?

The transaction will commit unless a runtime exception crosses the method boundary. You can override whether an exception forces the rollback or not by using @Transactional(dontRollbackOn=SomeException. class) (or rollbackOn ). You can also programmatically ask for a transaction to be marked for rollback.

What does @transactional annotation do?

Transactional annotation provides the application the ability to declaratively control transaction boundaries on CDI managed beans, as well as classes defined as managed beans by the Java EE specification, at both the class and method level where method level annotations override those at the class level.


2 Answers

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

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

But this will not work after transaction is completed.

You could add a TransactionSynchronization and implement afterCompletion(int status) to log the status or store it in a ThreadLocal variable for later usage.

public class LogTransactionSynchronization extends TransactionSynchronizationAdapter {
   @Override
   public afterCompletion(int status) {
      // log the status or store it for later usage
   }
}
like image 123
Jose Luis Martin Avatar answered Sep 28 '22 21:09

Jose Luis Martin


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

log4j.logger.org.hibernate.transaction=DEBUG,R
log4j.logger.org.springframework.transaction=DEBUG,R
like image 34
Muthu Chithambara Jothi Avatar answered Sep 28 '22 20:09

Muthu Chithambara Jothi