Catch spring transaction exception in service - spring

i'd like to catch spring transaction exception in service layer not in the service heighr layer or the service caller.
As i found i can not catch the exception in the #transaction method.i need to take an action once the the transaction failes in the same method or the same service.

Transaction is rolled back when exception is thrown from method annotated with #Transactional (or class can be annotated with #Transactional).
So you can't do post rollback actions in this method. If you want to do this logic on service layer, you can have service bean handling post rollback action call another service bean which handles transaction.

Related

Spring Boot service exception is not caught by #ControllerAdvice exception handler

I have all my api controller methods in a controller with an exception handler annotated with "ControllerAdvice". This way, I can always return a JSON response to the frontend even if an unexpected exception occurs. This works fine for exceptions raised by code in the controller, but for exceptions raised in code in a service method called by the controller, the exception handler isn't used.
I don't want to have to specifically annotate the service to use the same exception handler because it may be called by a controller that we use different exception handling for.
Is there a way to prevent the service from handling the exception itself and instead have the controller's exception handler handle it?

Rolling back a transaction in Spring ApplicationListener

Normal Spring ApplicationListners are running synchronously within the same thread of the event publisher.
Is there any way to rollback the main context transaction in the listener?
I have a method annotated #Transactional and it publishes an event, I want to make execute the listeners in the scope of the main method, and rollback the main transaction in case of exceptions.
I am using a LocalTransactionManager on Hibernate SessionFactory.
Is there any way to do this using Spring 3.3.x?

Transaction readonly=true not throwing exception when commiting transaction

We have Spring MVC based web-app having service method attributed with #Transactional(readonly=true).
I was expecting spring to throw exception because we have method which is committing data in mysql db.
Can anyone help me out why transaction attribute (Readonly) related exception is not thrown ?
below mentioned is code...
#Service
#Transactional
public class AppService {
... #Autowired Dao
public int createApplication(AppVO vo){
....
}
}
Taken straight from the Javadoc of readOnly of #Transactional is the following:
This just serves as a hint for the actual transaction subsystem; it
will not necessarily cause failure of write access attempts. A
transaction manager which cannot interpret the read-only hint will not
throw an exception when asked for a read-only transaction.
So it is not unexecpected that an exception is not thrown.

spring Transaction exception handling

Basically with spring Transaction management .We can annotate the service layer class with #Transactional.
Also i have annotated my DAO layer class with #repository to make checked exception to unchecked exception.
Now when a runtimeexception is encoutered by the service layer class it will rollback.
My question how do we go about the exception handling. I will have to put some error message to UI .So i need to capture the exception .
How do i go about it.

SessionContext RollBackOnly and MDB's

Having an MDB that receives a message in a transaction and then does several EJB calls if I call in one of those EJB's this.sessionContext.setRollbackOnly() will this trigger the JMS message's redelivery ?
All the EJB methods are marked with Requires_New transaction attribute.
No, because the MDB's transaction will be suspended while EJBs annotated with #REQUIRES_NEW are being processed — each within its own transaction. Additional assumption is that setRollbackOnly() is the only effect of rolling back an EJB's transaction, that is the EJB exited properly and did not throw any exception (after voting for rollback, further interaction with the resource may cause an exception from the javax.ejb.EJBException family to be thrown).

Resources