我想用JpaTJpaTransactionManager
/ PlatformTransactionManager
来衡量Spring管理的交易时间,以检测长待处理的交易,并警告一些听众关于这种情况。
我可以在@Transactional
方法上使用方面,但由于事务传播,它不是检测确切事务何时开始或完成的好方法。
对于像事务监听器那样可以访问启动/完成事件以及启动trasaction的对象的bean名称或只是堆栈跟踪
答案 0 :(得分:5)
在带注释的方法中使用TransactionSynchronizationManager:
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization(){
long startTime;
void beforeCommit() {
startTime = System.nanoTime();
}
void afterCommit(){
System.out.println("Transaction time: " + (System.nanoTime() - startTime));
}
});
跟踪方面的所有交易:
@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()));
}
}
答案 1 :(得分:1)
您可以考虑实施一个实现ApplicationListener<E extends ApplicationEvent>
的类并在那里执行测量。像这样:
@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());
}
}
对于JPA,您可以考虑实施类似的解决方案(根据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());
}
}
在此之后,您需要使用@EntityListeners(LogListener.class)
注释您的实体类哪些实例持久保存到数据库。