Top Description Fields Constructors Methods
org.springframework.jdbc.support

public Class SQLErrorCodesFactory

extends Object
Class Inheritance
Imports
java.sql.DatabaseMetaData, java.util.Collections, .Map, javax.sql.DataSource, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeansException, org.springframework.beans.factory.support.DefaultListableBeanFactory, org.springframework.beans.factory.xml.XmlBeanDefinitionReader, org.springframework.core.io.ClassPathResource, .Resource, org.springframework.lang.Nullable, org.springframework.util.Assert, .ConcurrentReferenceHashMap, .PatternMatchUtils, .StringUtils

Factory for creating SQLErrorCodes based on the "databaseProductName" taken from the java.sql.DatabaseMetaData.

Returns SQLErrorCodes populated with vendor codes defined in a configuration file named "sql-error-codes.xml". Reads the default file in this package if not overridden by a file in the root of the class path (for example in the "/WEB-INF/classes" directory).

Authors
Thomas Risberg, Rod Johnson, Juergen Hoeller
See Also
java.sql.DatabaseMetaData#getDatabaseProductName()

Field Summary

Modifier and TypeField and Description
private final Map<DataSource, SQLErrorCodes>
dataSourceCache

Map to cache the SQLErrorCodes instance per DataSource.

private final Map<String, SQLErrorCodes>
errorCodesMap

Map to hold error codes for all databases defined in the config file.

private static SQLErrorCodesFactory
instance

Keep track of a single instance so we can return it to classes that request it.

private static final Log
public static final String
SQL_ERROR_CODE_DEFAULT_PATH

The name of default SQL error code files, loading from the class path.

public static final String
SQL_ERROR_CODE_OVERRIDE_PATH

The name of custom SQL error codes file, loading from the root of the class path (e.g. from the "/WEB-INF/classes" directory).

Constructor Summary

AccessConstructor and Description
protected
SQLErrorCodesFactory()

Create a new instance of the SQLErrorCodesFactory class.

Method Summary

Modifier and TypeMethod and Description
private void
checkCustomTranslatorRegistry(String databaseName, SQLErrorCodes errorCodes)

Check the CustomSQLExceptionTranslatorRegistry for any entries.

public SQLErrorCodes

Returns:

the SQLErrorCodes instance for the given database (never null; potentially empty)
getErrorCodes
(String
the database name (must not be null)
databaseName
)

Return the SQLErrorCodes instance for the given database.

public SQLErrorCodes

Returns:

the corresponding SQLErrorCodes object (never null; potentially empty)
getErrorCodes
(DataSource
the DataSource identifying the database
dataSource
)

Return SQLErrorCodes for the given DataSource, evaluating "databaseProductName" from the java.sql.DatabaseMetaData, or an empty error codes instance if no SQLErrorCodes were found.

public static SQLErrorCodesFactory
getInstance()

Return the singleton instance.

private String

Returns:

the identification String
identify
(DataSource
the DataSource to introspect
dataSource
)

Build an identification String for the given DataSource, primarily for logging purposes.

protected Resource

Returns:

the resource, or null if the resource wasn't found
loadResource
(String
resource path; either a custom path or one of either SQL_ERROR_CODE_DEFAULT_PATH or SQL_ERROR_CODE_OVERRIDE_PATH.
path
)

Load the given resource from the class path.

public SQLErrorCodes

Returns:

the corresponding SQLErrorCodes object (never null)
registerDatabase
(DataSource
the DataSource identifying the database
dataSource
,
String
the corresponding database name as stated in the error codes definition file (must not be null)
databaseName
)

Associate the specified database name with the given DataSource.

public SQLErrorCodes

Returns:

the corresponding SQLErrorCodes object, or null in case of a JDBC meta-data access problem
resolveErrorCodes
(DataSource
the DataSource identifying the database
dataSource
)

Return SQLErrorCodes for the given DataSource, evaluating "databaseProductName" from the java.sql.DatabaseMetaData, or null if case of a JDBC meta-data access problem.

public SQLErrorCodes

Returns:

the corresponding SQLErrorCodes object that got removed, or null if not registered
unregisterDatabase
(DataSource
the DataSource identifying the database
dataSource
)

Clear the cache for the specified DataSource, if registered.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dataSourceCacheback to summary
private final Map<DataSource, SQLErrorCodes> dataSourceCache

Map to cache the SQLErrorCodes instance per DataSource.

errorCodesMapback to summary
private final Map<String, SQLErrorCodes> errorCodesMap

Map to hold error codes for all databases defined in the config file. Key is the database product name, value is the SQLErrorCodes instance.

instanceback to summary
private static SQLErrorCodesFactory instance

Keep track of a single instance so we can return it to classes that request it. Lazily initialized in order to avoid making SQLErrorCodesFactory constructor reachable on native images when not needed.

Annotations
@Nullable
loggerback to summary
private static final Log logger
SQL_ERROR_CODE_DEFAULT_PATHback to summary
public static final String SQL_ERROR_CODE_DEFAULT_PATH

The name of default SQL error code files, loading from the class path.

SQL_ERROR_CODE_OVERRIDE_PATHback to summary
public static final String SQL_ERROR_CODE_OVERRIDE_PATH

The name of custom SQL error codes file, loading from the root of the class path (e.g. from the "/WEB-INF/classes" directory).

Constructor Detail

SQLErrorCodesFactoryback to summary
protected SQLErrorCodesFactory()

Create a new instance of the SQLErrorCodesFactory class.

Not public to enforce Singleton design pattern. Would be private except to allow testing via overriding the loadResource(String) method.

Do not subclass in application code.

See Also
loadResource(String)

Method Detail

checkCustomTranslatorRegistryback to summary
private void checkCustomTranslatorRegistry(String databaseName, SQLErrorCodes errorCodes)

Check the CustomSQLExceptionTranslatorRegistry for any entries.

getErrorCodesback to summary
public SQLErrorCodes getErrorCodes(String databaseName)

Return the SQLErrorCodes instance for the given database.

No need for a database meta-data lookup.

Parameters
databaseName:String

the database name (must not be null)

Returns:SQLErrorCodes

the SQLErrorCodes instance for the given database (never null; potentially empty)

Exceptions
IllegalArgumentException:
if the supplied database name is null
getErrorCodesback to summary
public SQLErrorCodes getErrorCodes(DataSource dataSource)

Return SQLErrorCodes for the given DataSource, evaluating "databaseProductName" from the java.sql.DatabaseMetaData, or an empty error codes instance if no SQLErrorCodes were found.

Parameters
dataSource:DataSource

the DataSource identifying the database

Returns:SQLErrorCodes

the corresponding SQLErrorCodes object (never null; potentially empty)

See Also
java.sql.DatabaseMetaData#getDatabaseProductName()
getInstanceback to summary
public static SQLErrorCodesFactory getInstance()

Return the singleton instance.

identifyback to summary
private String identify(DataSource dataSource)

Build an identification String for the given DataSource, primarily for logging purposes.

Parameters
dataSource:DataSource

the DataSource to introspect

Returns:String

the identification String

loadResourceback to summary
protected Resource loadResource(String path)

Load the given resource from the class path.

Not to be overridden by application developers, who should obtain instances of this class from the static getInstance() method.

Protected for testability.

Parameters
path:String

resource path; either a custom path or one of either SQL_ERROR_CODE_DEFAULT_PATH or SQL_ERROR_CODE_OVERRIDE_PATH.

Returns:Resource

the resource, or null if the resource wasn't found

Annotations
@Nullable
See Also
getInstance
registerDatabaseback to summary
public SQLErrorCodes registerDatabase(DataSource dataSource, String databaseName)

Associate the specified database name with the given DataSource.

Parameters
dataSource:DataSource

the DataSource identifying the database

databaseName:String

the corresponding database name as stated in the error codes definition file (must not be null)

Returns:SQLErrorCodes

the corresponding SQLErrorCodes object (never null)

See Also
unregisterDatabase(DataSource)
resolveErrorCodesback to summary
public SQLErrorCodes resolveErrorCodes(DataSource dataSource)

Return SQLErrorCodes for the given DataSource, evaluating "databaseProductName" from the java.sql.DatabaseMetaData, or null if case of a JDBC meta-data access problem.

Parameters
dataSource:DataSource

the DataSource identifying the database

Returns:SQLErrorCodes

the corresponding SQLErrorCodes object, or null in case of a JDBC meta-data access problem

Annotations
@Nullable
Since
5.2.9
See Also
java.sql.DatabaseMetaData#getDatabaseProductName()
unregisterDatabaseback to summary
public SQLErrorCodes unregisterDatabase(DataSource dataSource)

Clear the cache for the specified DataSource, if registered.

Parameters
dataSource:DataSource

the DataSource identifying the database

Returns:SQLErrorCodes

the corresponding SQLErrorCodes object that got removed, or null if not registered

Annotations
@Nullable
Since
4.3.5
See Also
registerDatabase(DataSource, String)