spring Transaction exception handling - spring

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.

Related

Does it make sense to use PersistenceExceptionTranslationPostProcessor with Spring's JdbcTemplate?

Title speaks for itself.
Is PersistenceExceptionTranslationPostProcessor used solely for JPA implementations or is it relevant to use it with Spring's JdbcTemplate too?
And if there are two datasources needed each with their own JPA entity manager and transaction manager, do I still only need to specify one PersistenceExceptionTranslationPostProcessor for the entire application?
The auto-award bounty answer is wrong
~~~The correct answer is as follows~~~
I believe I've discovered the answer here:
http://www.deroneriksson.com/tutorials/java/spring/introduction-to-the-spring-framework/component-scanning-and-repository
The #Repository annotation can have a special role when it comes to converting exceptions to Spring-based unchecked exceptions. Recall that the JdbcTemplate handled this task for us. When we work with Hibernate, we’re not going to work with a Spring template that handles this conversion of Hibernate-based exceptions to Spring-based exceptions. As a result, in order to handle this conversion automatically, Hibernate DAOs annotated with #Repository will have their Hibernate exceptions rethrown as Spring exceptions using a PersistenceExceptionTranslationPostProcessor.
Further reading: http://www.deroneriksson.com/tutorials/java/spring/introduction-to-the-spring-framework/hibernate-daos
The paragraph above explicitly says:
Recall that the JdbcTemplate handled this task for us
So, to answer my own question, there is no need to use PersistenceExceptionTranslationPostProcessor with jdbcTemplate
Yes you can, The Spring exception translation mechanism can be applied transparently to all beans annotated with #Repository – by defining an exception translation bean post processor bean in the Context:
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
As per it doc
Bean post-processor that automatically applies persistence exception translation to any bean marked with Spring's #Repository
annotation, adding a corresponding
PersistenceExceptionTranslationAdvisor to the exposed proxy (either an
existing AOP proxy or a newly generated proxy that implements all of
the target's interfaces).
Translates native resource exceptions to Spring's DataAccessException hierarchy. Autodetects beans that implement the
PersistenceExceptionTranslator interface, which are subsequently asked
to translate candidate exceptions.
All of Spring's applicable resource factories (e.g. LocalContainerEntityManagerFactoryBean) implement the
PersistenceExceptionTranslator interface out of the box. As a
consequence, all that is usually needed to enable automatic exception
translation is marking all affected beans (such as Repositories or
DAOs) with the #Repository annotation, along with defining this
post-processor as a bean in the application context.
So you can use it with Jdbctemplate as well as with any Jpa vendor implementation
As per is doc All of Spring's applicable resource factories (e.g. LocalContainerEntityManagerFactoryBean) implement the PersistenceExceptionTranslator interface out of the box, I think you still need to use PersistenceExceptionTranslationPostProcessor, because it used to translate all errors generated during the persistence process (HibernateExceptions, PersistenceExceptions...) into DataAccessException objects.

Is there a generic spring-data Exception class to handle exceptions thrown in the repo layer?

I'm using spring-data-jpa to read/write to my DB. I've read through the documentation for spring-data and the one thing that I can't seem to locate is how it handles exceptions.
I am looking to catch any type of exception thrown by a repo action. Is there a way of doing this short of just wrapping the individual repo action in a try/catch and catching any RuntimeException? I'd rather avoid that as that is an even larger catch-all than I'd like.
Does spring-data wrap all exceptions in its own SpringDataException() or something similar? I've looked through the spring-data-commons jar as well as the spring-data-jpa jar and didn't find anything applicable.
Does spring-data wrap all exceptions in its own SpringDataException() or something similar
Yes, sort of. spring-data(-jpa) enable exception translation from JPA exceptions to Spring’s DataAccessException hierarchy:
From spring-data-jpa reference implementation:
...Beyond that, it activates persistence exception translation for all beans annotated with #Repository to let exceptions being thrown by the JPA persistence providers be converted into Spring’s DataAccessException hierarchy.
From Spring reference implementation:
Spring provides a convenient translation from technology-specific exceptions like SQLException to its own exception class hierarchy with the DataAccessException as the root exception. These exceptions wrap the original exception so there is never any risk that one might lose any information as to what might have gone wrong...
For more read about Spring DAO support

Catch spring transaction exception in service

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.

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.

Can I use spring DataAccessException directly in service layer

Can I use spring DataAccessException directly in service layer?. Is this a good practice/design to spread a frame work class in service layer?.
OR
should i catch DataAccessException in dao layer and rethrow it as some more generic Exception?
I think it's fine for the DAO layer to throw that exception. The service layer already knows about the persistence layer, so no additional dependencies are created.
It is good to map/wrap the DataAccessException (thrown from DAO layer) into application specific exception hierarchy (in service layer) so that the dependent/calling layer has to just deal with your application specific exception classes.
Personally, I catch all checked exceptions in the service layer and throw my own ServiceExceptions initializing them with the catched exception. This way, the exception information is not lost and Controllers don't need to deal with low-level exceptions. But there's no need to do this in the DAO layer.
There's a pretty good chapter on exception handling in "Effective Java" (J. Bloch), it's a good read, as well as the rest of the book. Item 61 deals with this question.
http://books.google.de/books/about/Effective_Java.html?id=Ft8t0S4VjmwC

Resources