JBoss transaction interceptor, how?

Hi

anyone has experience with writing and installing a transaction interceptor with jboss AOP?

we have an EJB3 application running on jboss 4.2 and we need to do something before and after a transaction.

 

Thank you for any help.

 

 

Comments

the following solution was suggested by Yinon Simha (ysimha@gmail.com):

1) declare the following method inside your class:
private void interceptTransaction(Runnable beforeTxEnd, Runnable afterTxEnd)

where the 1st Runnable instance performs the logic to be performed before the TX ends, and the 2nd performs the logic after the TX ends.

2) inside this method, perform the following:

2.1) take the TransactionManager from the context like this:
Transaction tx =(Transaction) (new InitialContext()).lookup("java:/TransactionManager").getTransaction();

implement the following:
2.2) tx.registerSynchronization(new Synchronization()
{
public void beforeCompletion()
{
beforeTxEnd.run();
}

public void afterCompletion(int status)
{
if (status == Status.STATUS_COMMITTED)
{
runnable.run();
}
}
}

2.3 close the method

3) activate this method wherever you want to perform actions before and after the TX.

Thanks Elad,
Speaking from memory,this is an old issue. the requirements are to retry the whole transaction when it fails,to retry the logic. it is related to an issue with mysql that drops the transaction in certain cases. because it is a global concern for the application then working with BMT and user transactions is not an option. doing it with EJB3 interceptors didn't work because it didn't wrap the jboss transaction interceptor.
one solution that also didn't succeed was to add an interceptor to the jboss interceptor stack. don't remember why it didn't work.
there is a suggestion in jboss to add support for that but i don't know when and if they plan to release it: https://jira.jboss.org/jira/browse/EJBTHREE-1844 .

the mysql issue
http://dev.mysql.com/doc/refman/5.0/en/innodb-error-handling.html

any help is appreciated.

Are you using Jboss Seam or pure Hibernate?

Have you read this : https://www.hibernate.org/391.html

They tried using jboss aop but could not make it wrap the jboss transaction interceptor.

you can try wrapping your method with @AroundInvoke (of jboss-ejb3x.jar)
you'll receive an InvocationContext as a parameter. get the name of the method (and all of its parameters) from the context and save it when the method starts.
inside the method, you check if the TX fails, and if so - call the method again with the parameters you took from the context.