Example usage for org.springframework.jdbc.support SQLErrorCodes SQLErrorCodes

List of usage examples for org.springframework.jdbc.support SQLErrorCodes SQLErrorCodes

Introduction

In this page you can find the example usage for org.springframework.jdbc.support SQLErrorCodes SQLErrorCodes.

Prototype

SQLErrorCodes

Source Link

Usage

From source file:org.springframework.jdbc.support.SQLErrorCodesFactory.java

/**
 * Return the {@link SQLErrorCodes} instance for the given database.
 * <p>No need for a database metadata lookup.
 * @param databaseName the database name (must not be {@code null})
 * @return the {@code SQLErrorCodes} instance for the given database
 * @throws IllegalArgumentException if the supplied database name is {@code null}
 *///w  w w  .  j av  a 2  s.c  o  m
public SQLErrorCodes getErrorCodes(String databaseName) {
    Assert.notNull(databaseName, "Database product name must not be null");

    SQLErrorCodes sec = this.errorCodesMap.get(databaseName);
    if (sec == null) {
        for (SQLErrorCodes candidate : this.errorCodesMap.values()) {
            if (PatternMatchUtils.simpleMatch(candidate.getDatabaseProductNames(), databaseName)) {
                sec = candidate;
                break;
            }
        }
    }
    if (sec != null) {
        checkCustomTranslatorRegistry(databaseName, sec);
        if (logger.isDebugEnabled()) {
            logger.debug("SQL error codes for '" + databaseName + "' found");
        }
        return sec;
    }

    // Could not find the database among the defined ones.
    if (logger.isDebugEnabled()) {
        logger.debug("SQL error codes for '" + databaseName + "' not found");
    }
    return new SQLErrorCodes();
}

From source file:org.springframework.jdbc.support.SQLErrorCodesFactory.java

/**
 * Return {@link SQLErrorCodes} for the given {@link DataSource},
 * evaluating "databaseProductName" from the
 * {@link java.sql.DatabaseMetaData}, or an empty error codes
 * instance if no {@code SQLErrorCodes} were found.
 * @param dataSource the {@code DataSource} identifying the database
 * @return the corresponding {@code SQLErrorCodes} object
 * @see java.sql.DatabaseMetaData#getDatabaseProductName()
 *///from w  ww. jav a2s.co m
public SQLErrorCodes getErrorCodes(DataSource dataSource) {
    Assert.notNull(dataSource, "DataSource must not be null");
    if (logger.isDebugEnabled()) {
        logger.debug("Looking up default SQLErrorCodes for DataSource [" + identify(dataSource) + "]");
    }

    // Try efficient lock-free access for existing cache entry
    SQLErrorCodes sec = this.dataSourceCache.get(dataSource);
    if (sec == null) {
        synchronized (this.dataSourceCache) {
            // Double-check within full dataSourceCache lock
            sec = this.dataSourceCache.get(dataSource);
            if (sec == null) {
                // We could not find it - got to look it up.
                try {
                    String name = JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName");
                    if (StringUtils.hasLength(name)) {
                        return registerDatabase(dataSource, name);
                    }
                } catch (MetaDataAccessException ex) {
                    logger.warn("Error while extracting database name - falling back to empty error codes", ex);
                }
                // Fallback is to return an empty SQLErrorCodes instance.
                return new SQLErrorCodes();
            }
        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug("SQLErrorCodes found in cache for DataSource [" + identify(dataSource) + "]");
    }

    return sec;
}