4 Replies Latest reply on Feb 18, 2009 6:30 PM by 843859

    how handle duplicate primary key exception?

    800299

      did java have duplicate key exception? if not how can i make my own exception for duplicate primary key?

        • 1. Re: how handle duplicate primary key exception?
          843859
          Duplicate primary key will throw with JDBC exception(eg: SQLException) and the message format of the exception will be depended on the tool.

          You just need to create a custom database exception to store the error message or error log into a POJO as below.

          public class DataAccessException extends SQLException {

          private String errorMsg;

          public DataAccessException() {
          //Overwrite the super class constructor
          }

          public void setErrorMsg(String errorMsg) { ...... }
          public String getErrorMsg() { ......}

          }

          Then you may make use of the class or this concept to capture the primary key error.

          You can have other super class to handle the primary key error and you need to check with the JDBC tool you used such as JDBC, Hibernate, iBatis or others.

          THanks,
          Jyno
          • 2. Re: how handle duplicate primary key exception?
            843859
            SQLException is thrown when duplicate key is inserted. After SQLException, check if primary key exists, then throw newly created Exception.for Duplicate Key
            • 3. Re: how handle duplicate primary key exception?
              625957
              You could do something like this:
              public class ExceptionTranslator
              {
                  public static DataAccessException translateSQLException(SQLException e)
                  {
                      if("23".equals(e.getSQLState()))
                      {
                          return new ConstraintViolationException(e);
                      }
                      else 
                      {
                          return new UnknownDataAccessException(e);
                      }
                  }
              }
              public abstract class DataAccessException extends Exception
              {
                  public DataAccessException(Throwable cause)
                  {
                      super(cause);
                  }
              }
              public class UnknownDataAccessException extends DataAccessException
              {
                  public UnknownDataAccessException(Throwable cause)
                  {
                      super(cause);
                  }
              }
              public class ConstraintViolationException extends DataAccessException
              {
                  public ConstraintViolationException(Throwable cause)
                  {
                      super(cause);
                  }
              }
              Then in your existing code:
              catch(SQLException e)
              {
                throw ExceptionTranslator.translateSQLException(e);
              }
              You can extend translateSQLException() to throw any other kinds of exception e.g. DeadlockException by checking SQLException.getSQLState() / SQLException.getErrorCode() / SQLException.getMessage()
              • 4. Re: how handle duplicate primary key exception?
                843859
                DigitalDreamer wrote:
                You could do something like this:
                if("23".equals(e.getSQLState()))
                Wrong.

                A cross-DB compatible approach to determine a constraint violation is checking if SQL state starts with 23.
                public static boolean isConstraintViolation(SQLException e) {
                    return e.getSQLState().startsWith("23");
                }