问题
I would like to measure time of transacactions managed by Spring with JpaTJpaTransactionManager
/PlatformTransactionManager
to detect long pending transactions and warn some listener about that condition.
I could use aspect on @Transactional
methods but due to transaction propagation it's not a good way to detect when exact transaction is started or finished.
It would be good to something like transaction listener with access to start/finish events together with some bean name of object starting the trasaction or just a stack trace
回答1:
Use TransactionSynchronizationManager inside annotated method:
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization(){
long startTime;
void beforeCommit() {
startTime = System.nanoTime();
}
void afterCommit(){
System.out.println("Transaction time: " + (System.nanoTime() - startTime));
}
});
Track all transactions with aspect:
@Aspect
class TransactionAspect extends TransactionSynchronizationAdapter {
@Before("@annotation(org.springframework.transaction.annotation.Transactional)")
public void registerTransactionSyncrhonization() {
TransactionSynchronizationManager.registerSynchronization(this);
}
ThreadLocal<Long> startTime = new ThreadLocal<>();
void beforeCommit() {
startTime.set(System.nanoTime());
}
void afterCommit(){
System.out.println("Transaction time: " + (System.nanoTime() - startTime.get()));
}
}
回答2:
You may consider implementing a class which implements ApplicationListener<E extends ApplicationEvent>
and perform measuring there. Something like this:
@Component
public class TransactionListener implements ApplicationListener<Neo4jDataManipulationEvent> {
private Long transactionStart;
private static final Logger log = LoggerFactory.getLogger(TransactionListener.class);
@Override
public void onApplicationEvent(Neo4jDataManipulationEvent event) {
if (event instanceof BeforeSaveEvent) {
transactionStart = System.currentTimeMillis();
}
if (event instanceof AfterSaveEvent) {
log.debug("Transaction time in ms: ", System.currentTimeMillis() - transactionStart);
log.debug("Source: ", event.getSource());
}
}
For JPA you may consider implementing a similar solution (according to this tutorial):
public class LogListener {
private Long transactionStart;
@PrePersist
@PreUpdate
@PreRemove
private void before(Object object) {
transactionStart = System.currentTimeMillis();
}
@PostPersist
@PostUpdate
@PostRemove
private void after(Object object) {
System.out.println("Transaction time in ms: ", System.currentTimeMillis() - transactionStart);
System.out.println("Source: ", object.getClass());
}
}
You will need to annotate your entity classes which instances are persisted to the DB with @EntityListeners(LogListener.class)
after that.
来源:https://stackoverflow.com/questions/40071088/is-it-possible-to-measure-time-of-all-spring-managed-transactions-with-transact