-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce TransactionExecutionListener
callback API
#27479
Comments
You can wrap import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class WrappedTransactionManager implements PlatformTransactionManager {
private final PlatformTransactionManager underlying;
@Override
public TransactionStatus getTransaction(TransactionDefinition transactionDefinition) throws TransactionException {
// TODO
return this.underlying.getTransaction(transactionDefinition);
}
@Override
public void commit(TransactionStatus transactionStatus) throws TransactionException {
// TODO
this.underlying.commit(transactionStatus);
}
@Override
public void rollback(TransactionStatus transactionStatus) throws TransactionException {
// TODO
this.underlying.rollback(transactionStatus);
}
} |
You can register callbacks using the Another option is using the |
@mdeinum |
|
It seems that commit/rollback is also called for stacked virtual transactions so I have to also check them:
|
WARNING registering a simple wrapper is not enough:
The original Now, the wrapper can be made compatible, but what if, in a new spring version, another spring managed interface is added to This wrapper is a WORKAROUND only, and a spring managed solution must be provided for this feature. |
You can try @Bean
static BeanPostProcessor transactionManagerPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof PlatformTransactionManager) {
ProxyFactory pf = new ProxyFactory(bean);
pf.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method m = invocation.getMethod();
if (m.getDeclaringClass() == PlatformTransactionManager.class) {
//TODO
}
return invocation.proceed();
}
});
bean = pf.getProxy();
}
return bean;
}
};
} |
@cdalexndr rather than asking for a technical solution like this, we'd very much prefer that you explain the use cases that you're trying to implement. |
My use case: I want to track database transactions (start-end) to dump them when the app health is DOWN, for debugging purposes. |
This issue maybe related to this one #18297. |
Can you try using following phases:- |
I'm introducing a dedicated This is effectively a callback interface for stateless listening to transaction creation/completion steps in a transaction manager. It is primarily meant for observation and statistics, not for resource management purposes where stateful transaction synchronizations are still the way to go. In contrast to transaction synchronizations, the transaction execution listener contract is commonly supported for thread-bound transactions as well as reactive transactions, with a common registration facility in the new |
I've settled on the term "transaction execution listener" for it, also naming the contract |
@snicoll @wilkinsona I suppose Boot could automatically inject any @Bean
public JdbcTransactionManager transactionManager(DataSource dataSource, Collection<TransactionExecutionListener> transactionExecutionListeners) {
JdbcTransactionManager tm = new JdbcTransactionManager();
tm.setDataSource(dataSource);
tm.setTransactionExecutionListeners(transactionExecutionListeners);
return tm;
} |
Thanks for the suggestion, @jhoeller. I've opened spring-projects/spring-boot#36770. Sorry for the noise of #31001 which I opened in the wrong tracker. |
TransactionExecutionListener
callback API
Affects: 5.3.10
Please provide a method to register a listener to be notified when a transaction is created/committed/rolledback/finished.
The text was updated successfully, but these errors were encountered: