How to catch Hibernate SQL EXCEPTIONS IN SPRING
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;
}
Labeo
Updated on June 04, 2022Comments
-
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 over 8 yearsi am not able to have GenericJdbcException it is asking to create a class of it
-
Labeo over 8 yearsit 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 over 8 yearsUse multiple catch block instead of using OR.
-
Labeo over 8 yearsstill i am facing same problem
-
wiretext over 8 years
-
Esty over 8 yearswould u please provide me sample code coz I am also working over Spring MVC + Hibernate. This worked fine for me.
-
Labeo over 8 yearscan you give some solution
-
Esty over 8 yearsyeah!! you are using SQLException in second catch which is a parameter used by JDBCConnectionException. So u can't use both.
-
Esty over 8 yearsActually as you are using Spring MVC, I am suggesting you to use a GlobalExceptionHandler by AOP to handle exceptions. It would be much better.