Results 1 to 6 of 6

Thread: Unique constraint violation error - How should it be trated?

  1. #1
    Join Date
    Sep 2004
    Location
    Argentina
    Posts
    23

    Default Unique constraint violation error - How should it be trated?

    Hi, i have the following question.
    Given a table, suppose City, with an id field (primary key) and a name name field (with a unique constraint). If i insert twice a city named X, an SQLException will be thrown in the second insertion due to unique constraint being violated. This error should be treated as an application rather than a system error, right?

    I suppose, the SQLException will be wrapped in a DataAccessException wich is an unchecked exception (therefore a system error). Is there a way to distinguish an application error from a system error given a DataAccessException?

    Thanks in advance.
    - N!K -

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    In case of Primary Key / unique Key violation, Spring wrapps SQLException in a DataIntegrityViolationException. DataIntegrityViolationException subclasses DataAccessException.
    So you can catch DataIntegrityViolationException in your code to report the application error.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Sep 2004
    Location
    Argentina
    Posts
    23

    Default

    Thanks Omar. Is DataIntegrityViolationException going to be thrown when a foreign key violation error is genereated while trying to delete a row referenced by another table?
    - N!K -

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    well, this depends on the database / jdbc driver version you are using. I did some testing and it worked for hsqldb 1.7.2 but not postgresql 7.2 (SQLState 'null' and errorCode '0', not very clear how to translate this into DataIntegrityViolationException)
    Hopefuly it will work for your environment.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    One solution is to use a custom translation defined in sql-error-codes.xml. First create your own exception class that is a sub class of DataAccessException.
    Code:
    import org.springframework.dao.DataIntegrityViolationException;
    
    public class UniqueConstraintException extends DataIntegrityViolationException {
    
    	public UniqueConstraintException(String msg) {
    		super(msg);
    	}
    
    	public UniqueConstraintException(String msg, Throwable ex) {
    		super(msg, ex);
    	}
    }
    Then make a copy of sql-error-codes.xml and modify it to fit your needs. Add a "customTranslations" entry for your custom exceptions class. This modified version of sql-error-codes.xml must be placed on the classpath to get picked up.

    Code:
    <!DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    	
    <beans>
    
      <bean id="Oracle" class="org.springframework.jdbc.support.SQLErrorCodes">
        <property name="badSqlGrammarCodes"><value>900,903,904,917,936,942,17006</value></property>
        <property name="dataIntegrityViolationCodes"><value>1400,1722,2291</value></property>
        <property name="cannotAcquireLockCodes"><value>54</value></property>
        <property name="customTranslations">
          <list>
            <bean class="org.springframework.jdbc.support.CustomSQLErrorCodesTranslation">
              <property name="errorCodes"><value>1</value></property>
              <property name="exceptionClass"><value>UniqueConstraintException</value></property>
            </bean>
          </list>
        </property>
      </bean>
    
    </beans>
    Now you can catch this custom exception and handle it or throw an application exception.

    For PostgreSQL - they do provide error codes starting with version 7.4.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  6. #6
    Join Date
    Dec 2005
    Posts
    3

    Default

    I've used this approach of defining a UniqueConstraintException and then throwing it from my DAO and Service layer. The exception is handled in my JSF Backing Bean and an appropriate message is displayed.

    I would like to know if this is a good practice, or if there is a better approach?

Similar Threads

  1. Replies: 1
    Last Post: Oct 4th, 2005, 06:11 PM
  2. Replies: 4
    Last Post: Sep 27th, 2005, 11:31 PM
  3. Replies: 4
    Last Post: Jul 27th, 2005, 03:54 AM
  4. Integer Constraints in RulesSource?
    By steve_smith in forum Swing
    Replies: 13
    Last Post: Nov 3rd, 2004, 03:55 PM
  5. Unique constraint
    By adepue in forum Swing
    Replies: 1
    Last Post: Oct 19th, 2004, 06:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •