How to catch Hibernate SQL EXCEPTIONS IN SPRING

18,920

Solution 1

Use JDBCConnectionException for connection related exceptions and ConstraintViolationException for constrain violation. You should use other exceptions as per your requirement. See enter link description here

Solution 2

In Addition to @Tanjim Rahman,

The Code Snippet

In UserDAOImpl.java

Use ConstraintViolationException to catch the duplicate Entry

@Override
public String addUser(User user) 
{
    Session openSession = sessionFactory.openSession();
    String retStr = null;

    try
    {
        openSession.getTransaction().begin();
        openSession.persist(user);
        openSession.getTransaction().commit();

        retStr = "success";
    }
    catch (ConstraintViolationException e)
    {
        openSession.getTransaction().rollback();

        String[] tmpArr =  e.getSQLException().toString().split(":");
        String[] anotherTmpArr = null;

        if( tmpArr.length > 1)
        {
            anotherTmpArr = tmpArr[1].split("for key");
        }

        retStr = anotherTmpArr[0];

    }
    catch (HibernateException e) 
    {
        retStr = e.getLocalizedMessage();
    }

    finally 
    {
        openSession.close();
    }


    return retStr;
}
Share:
18,920
Labeo
Author by

Labeo

Updated on June 04, 2022

Comments

  • Labeo
    Labeo almost 2 years

    i am using hibernate to execute sql statements in spring but to handle exceptions i used hibernate exception and sql exception but i am not able to handle exceptions

    if my sql server is not on or not accessable i am not able to catch exception i am getting error

    org.hibernate.exception.JDBCConnectionException 
    

    here name.save(user);

    is hibernate command that executes sql command

    how to catch this type of exceptions

    i am getting errors

    Unreachable catch block for JDBCConnectionException.This exception is never thrown from the try statement body

    Unreachable catch block for SQLException. This exception is never thrown from the try statement body

    mycode:

    try
    {
          ApplicationContext appContext = 
                  new ClassPathXmlApplicationContext("/config/BeansInfo.xml");
        TokenBo tokenBo = (TokenBo)appContext.getBean("TokenBo");
        TokenModel token = new TokenModel();
        token.setNumber(Number);
        token.setID(ID);
    }
    
    catch(JDBCConnectionException ex  )
    {
      System.out.println(ex);
    }
    catch(SQLException e){}
    
  • Labeo
    Labeo over 8 years
    i am not able to have GenericJdbcException it is asking to create a class of it
  • Labeo
    Labeo over 8 years
    it is giving Unreachable catch block for JDBCConnectionException. This exception is never thrown from the try statement body - Unreachable catch block for JDBCConnectionException. This exception is never thrown from the try statement body
  • Esty
    Esty over 8 years
    Use multiple catch block instead of using OR.
  • Labeo
    Labeo over 8 years
    still i am facing same problem
  • wiretext
    wiretext over 8 years
  • Esty
    Esty over 8 years
    would u please provide me sample code coz I am also working over Spring MVC + Hibernate. This worked fine for me.
  • Labeo
    Labeo over 8 years
    can you give some solution
  • Esty
    Esty over 8 years
    yeah!! you are using SQLException in second catch which is a parameter used by JDBCConnectionException. So u can't use both.
  • Esty
    Esty over 8 years
    Actually as you are using Spring MVC, I am suggesting you to use a GlobalExceptionHandler by AOP to handle exceptions. It would be much better.