Test @Transactional annotation using log4j

 

The easiest way to test @Transactional annotations is using a logger. For example I have used log4j in this example.

In log4j.properties I turned up the logging of springframework to DEBUG level which is very verbose.

log4j.logger.org.springframework=DEBUG

The method is annotated to be @Transactional with no write back to the database and hence readOnly=true.

@Transactional(propagation= Propagation.REQUIRED, noRollbackForClassName="Exception.class", readOnly=true)
void method(){
}

And here are the magic logs which help me determine if the @Transactional annotation is working. I have used JUnit, EclipseLink JPA, Log4J and Netbeans for testing:-

2011-09-02 10:17:46 [main] DEBUG org.springframework.transaction.annotation.AnnotationTransactionAttributeSource     - Adding transactional method 'method' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
2011-09-02 10:17:46 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory     - Returning cached instance of singleton bean 'transactionManager'
2011-09-02 10:17:46 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager     - Creating new transaction with name [com.company.method]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
2011-09-02 10:17:46 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager     - Opened new EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@43ad4a] for JPA transaction
2011-09-02 10:17:46 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager     - Initiating transaction commit
2011-09-02 10:17:46 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager     - Committing JPA transaction on EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@43ad4a]
2011-09-02 10:17:46 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager     - Closing JPA EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@43ad4a] after transaction
2011-09-02 10:17:46 [main] DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils     - Closing JPA EntityManager

Try it and enjoy the beauty of Spring 🙂

Advertisements

One thought on “Test @Transactional annotation using log4j

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s