I am developing web application with spring + hibernate. As per my knowledge, it is best practice to put @transactional in service layer. Spring throws DataAccessException in case of any exception while updating data into database.
Below is my high level class structure.
@Transactional
class OrderService {
public void createOrder() {
try {
orderDAO.createOrder();
} catch (DataAccessException e) {
// convert into business exception and sends back to presentation logic.
}
}
}
What happens here is data access exception is thrown only after completion of method. so if any exception occurs, I am not able to convert it into business exception in catch block.
Work around is to flush the hibernate session in dao method but I do not like this approach. Is there any better approach to this?