Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where and how handle spring+hibernate exceptions?

im using spring+hibernate for a desktop application.

I'm trying to build it with a layered implementation, so i have:

GUI layer --call--> Service layer --call--> DAO layer

A small example to better exaplain my situation:

// In GUI layer
private void actionPerformed(ActionEvent evt){
    addUser();
}

private void addUser(){
    // Check gui validation for user inputs
    if(inputIsValid()){
        String username=nameText.getText();
        String pass=passText.getText();
        //Now call service layer
        userService.createUser(username, pass);
        // Now here i want to show a message to user like
        // "Operation successful" or "Operation failed"
        // or more sofisticated message like "User with same name already exists"
    }
}


// Service layer
@Transactional
public void createUser(String name, String pass){
    User user=new User(name, pass);
    userDao.save(user);
}

// Another service layer example, 
@Transactional
public boolean createUser(String name, String pass){
    User user=new User(name, pass);
    try{
        userDao.save(user);
    }
    catch(Exception ex){
        Log(ex);
        return false;
    }
    return true;
    // In this case GUI layer can know if save is succesful, but it can't know WHY
    // the save is failed : some username? DB service shutdown? etc..
}

The problem is: who throw exception and who handle it?

I think DAO have to throw first exception, and service layer rethrow it, and finally GUI layer handle exception, so i can show message to user, is this good? There is a way to build some ExceptionHandler using spring?

What is the best practice to manage exceptions using spring+hibernate?

Thanks.

like image 837
blow Avatar asked Nov 13 '10 14:11

blow


People also ask

How exceptions are handled in Hibernate?

Hibernate provides better handle than the JDBCException . Developers can use the try and catch block to handle the exceptions. Put the line of code that may cause an exception in a try block and according to that exception put the exception handler in the catch block.

How does spring boot handle exceptions?

Exception HandlerThe @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.

What are the Hibernate exceptions?

Hibernate Exception Overview These can be mapping errors, infrastructure problems, SQL errors, data integrity violations, session problems, and transaction errors. These exceptions mostly extend from HibernateException.

How do I fix optimistic lock exception?

To resolve this error we have two ways: Get the latest object from the database and set the old object values if you need those values to be persisted to the new object and merge it. For the old object set the latest version from Database.


2 Answers

I would suggest wrapping the thrown exceptions in your own exception class and letting them bubble up to the GUI layer.

like image 123
Tyler Treat Avatar answered Oct 03 '22 21:10

Tyler Treat


If you're using Spring MVC, then there's a solution of writing ExcpetionHandlerResolver, take at the look on the documentation

If you're not working with Spring MVC. i would suggest throwing the exception from DAO to Service and then to View layer. Only the view layer can really provide valuable information to the user based on the exception caught.

like image 20
Bivas Avatar answered Oct 03 '22 21:10

Bivas