How can I get a "start of transaction" timestamp in Spring + JPA + Hibernate setup?
I am using Spring 3.0.5, JPA, Hibernate 3.5.5-Final and Spring's JpaTransactionManager. Anyon开发者_JAVA技巧e know an easy way to programmatically get a timestamp for the start of each transaction?
Idea
Since JpaTransactionManager extends AbstractPlatformTransactionManager and every transaction starts with a call of the #getTransaction method, you can write an aspect around that method.
Aspect
@Aspect
public class AspectAroundTransactions() {
@Pointcut("execution(* org.springframework.*.AbstractPlatformTransactionManager.getTransaction(..))" )
public void newTransaction() {}
@Before("newTransaction() && args(transactionDefinition,..)")
public void beforeStartOfTransaction(TransactionDefinition transactionDefinition) {
// here goes you code
long start = System.nanoTime();
System.out.println("Transaction " + transactionDefinition.getName() + " started at " + start );
}
}
It might be possible to use a filter in WEB-INF to do this - it would inject a timestamp into the request context as a named attribute. The timestamp would not be a "transaction" timestamp, but a request timestamp. But it will do what you want, I suspect.
精彩评论