Class SQLErrorCodesResolver

java.lang.Object
org.axonframework.eventsourcing.eventstore.jpa.SQLErrorCodesResolver
All Implemented Interfaces:
PersistenceExceptionResolver

public class SQLErrorCodesResolver extends Object implements PersistenceExceptionResolver
SQLErrorCodesResolver is an implementation of PersistenceExceptionResolver used to resolve sql error codes to see if it is an duplicate key constraint violation.

SQL Code configuration is available for the following database engines, which may be identifier automatically via the data source:

  • HSQL DB
  • MySQL
  • Apache Derby
  • DB2
  • H2
  • Informix Dynamic Server
  • MS SQL Server
  • Oracle
  • PostgreSQL
  • Sybase

This implementation will attempt to locate these error codes in the SQLException.getErrorCode() and SQLException.getSQLState(), respectively.

Since:
0.7
Author:
Martin Tilma, Allard Buijze
  • Constructor Details

    • SQLErrorCodesResolver

      public SQLErrorCodesResolver(List<Integer> duplicateKeyCodes)
      Initializes the SQLErrorCodesResolver using the given list of SQL Codes representing Key Constraint Violations.
      Parameters:
      duplicateKeyCodes - A list of Integer containing SQL Codes representing Key Constraint Violations
    • SQLErrorCodesResolver

      public SQLErrorCodesResolver(DataSource dataSource) throws SQLException
      Initialize a SQLErrorCodesResolver, automatically detecting the database name through the given dataSource. The database product name is used to resolve the database error codes.
      Parameters:
      dataSource - The data source providing the information about the backing database.
      Throws:
      SQLException - when retrieving the database product name fails
      AxonConfigurationException - is the dataSource returns an unknown database product name. Use SQLErrorCodesResolver(java.util.Properties, javax.sql.DataSource) instead.
    • SQLErrorCodesResolver

      public SQLErrorCodesResolver(String databaseProductName)
      Initialize a SQLErrorCodesResolver, automatically detecting the database name through the given dataSource. The database product name is used to resolve the database error codes. As an alternative you could set the property databaseDuplicateKeyCodes
      Parameters:
      databaseProductName - The product name of the database
      Throws:
      AxonConfigurationException - is the dataSource returns an unknown database product name. Use SQLErrorCodesResolver(java.util.Properties, String) instead.
    • SQLErrorCodesResolver

      public SQLErrorCodesResolver(Properties properties, String databaseProductName)
      Initialize a SQLErrorCodesResolver, automatically detecting the database name through the given dataSource. The database product name is used to resolve the database error codes. As an alternative you could set the property databaseDuplicateKeyCodes

      The form of the properties is expected to be:
      databaseName.duplicateKeyCodes=keyCode[,keyCode]*
      Where databaseName is the database product name as returned by the driver, with spaces (' ') replaced by underscore ('_'). The key codes must be a comma separated list of SQL Error code numbers (int).

      Parameters:
      properties - the properties defining SQL Error Codes for Duplicate Key violations for different databases
      databaseProductName - The product name of the database
    • SQLErrorCodesResolver

      public SQLErrorCodesResolver(Properties properties, DataSource dataSource) throws SQLException
      Initialize the SQLErrorCodesResolver with the given properties and use the dataSource to automatically retrieve the database product name.

      The form of the properties is expected to be:
      databaseName.duplicateKeyCodes=keyCode[,keyCode]*
      Where databaseName is the database product name as returned by the driver, with spaces (' ') replaced by underscore ('_'). The key codes must be a comma separated list of SQL Error code numbers (int).

      Parameters:
      properties - the properties defining SQL Error Codes for Duplicate Key violations for different databases
      dataSource - The data source providing the database product name
      Throws:
      SQLException - when retrieving the database product name fails
  • Method Details

    • isDuplicateKeyViolation

      public boolean isDuplicateKeyViolation(Exception exception)
      Description copied from interface: PersistenceExceptionResolver
      Indicates whether the given exception represents a duplicate key violation. Typically, duplicate key violations indicates concurrent access to an entity in the application. Two users might be accessing the same Aggregate, for example.
      Specified by:
      isDuplicateKeyViolation in interface PersistenceExceptionResolver
      Parameters:
      exception - The exception to evaluate
      Returns:
      true if the given exception represents a Duplicate Key Violation, false otherwise.
    • isDuplicateKeyCode

      protected boolean isDuplicateKeyCode(SQLException sqlException, List<Integer> errorCodes)
      Parameters:
      sqlException - The exception to locate the error code in
      errorCodes - The error codes indicating duplicate key violations
      Returns:
      true if the error code of the sqlException is in the given list of errorCodes, otherwise false