/** * Set the DataSource for this translator. * <p>Setting this property will cause a Connection to be obtained from * the DataSource to get the meta-data. * @param dataSource the DataSource to use to find meta-data and establish * which error codes are usable * @see SQLErrorCodesFactory#getErrorCodes(javax.sql.DataSource) * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public void setDataSource(DataSource dataSource) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource); }
/** * Set the database product name for this translator. * <p>Setting this property will avoid obtaining a Connection from the DataSource * to get the meta-data. * @param dbName the database product name that identifies the error codes entry * @see SQLErrorCodesFactory#getErrorCodes(String) * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public void setDatabaseProductName(String dbName) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(dbName); }
/** * Associate the specified database name with the given {@link DataSource}. * @param dataSource the {@code DataSource} identifying the database * @param databaseName the corresponding database name as stated in the error codes * definition file (must not be {@code null}) * @return the corresponding {@code SQLErrorCodes} object (never {@code null}) * @see #unregisterDatabase(DataSource) */ public SQLErrorCodes registerDatabase(DataSource dataSource, String databaseName) { SQLErrorCodes sec = getErrorCodes(databaseName); if (logger.isDebugEnabled()) { logger.debug("Caching SQL error codes for DataSource [" + identify(dataSource) + "]: database product name is '" + databaseName + "'"); } this.dataSourceCache.put(dataSource, sec); return sec; }
/** * Check that a known database produces recognizable codes. */ @Test public void testDefaultInstanceWithOracle() { SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("Oracle"); assertIsOracle(sec); }
@Test public void testGetFromDataSourceWithSQLException() throws Exception { SQLException expectedSQLException = new SQLException(); DataSource dataSource = mock(DataSource.class); given(dataSource.getConnection()).willThrow(expectedSQLException); SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource); assertIsEmpty(sec); }
@Test @SuppressWarnings("resource") public void customErrorCodeTranslation() { new ClassPathXmlApplicationContext("test-custom-translators-context.xml", CustomSQLExceptionTranslatorRegistrarTests.class); SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2"); SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(); sext.setSqlErrorCodes(codes); DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000)); assertNotNull("Should have been translated", exFor4200); assertTrue("Should have been instance of BadSqlGrammarException", BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass())); DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2)); assertNotNull("Should have been translated", exFor2); assertTrue("Should have been instance of TransientDataAccessResourceException", TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass())); DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3)); assertNull("Should not have been translated", exFor3); }
@Test public void testDataSourceWithNullMetadata() throws Exception { Connection connection = mock(Connection.class); DataSource dataSource = mock(DataSource.class); given(dataSource.getConnection()).willReturn(connection); SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource); assertIsEmpty(sec); verify(connection).close(); }
private SQLErrorCodes getErrorCodesFromDataSource(String productName, SQLErrorCodesFactory factory) throws Exception { DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class); given(databaseMetaData.getDatabaseProductName()).willReturn(productName); Connection connection = mock(Connection.class); given(connection.getMetaData()).willReturn(databaseMetaData); DataSource dataSource = mock(DataSource.class); given(dataSource.getConnection()).willReturn(connection); SQLErrorCodesFactory secf = null; if (factory != null) { secf = factory; } else { secf = SQLErrorCodesFactory.getInstance(); } SQLErrorCodes sec = secf.getErrorCodes(dataSource); SQLErrorCodes sec2 = secf.getErrorCodes(dataSource); assertSame("Cached per DataSource", sec2, sec); verify(connection).close(); return sec; }
/** * Check that a default instance returns empty error codes for an unknown database. */ @Test public void testDefaultInstanceWithNoSuchDatabase() { SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("xx"); assertTrue(sec.getBadSqlGrammarCodes().length == 0); assertTrue(sec.getDataIntegrityViolationCodes().length == 0); }
/** * Associate the specified database name with the given {@link DataSource}. * @param dataSource the {@code DataSource} identifying the database * @param dbName the corresponding database name as stated in the error codes * definition file (must not be {@code null}) * @return the corresponding {@code SQLErrorCodes} object */ public SQLErrorCodes registerDatabase(DataSource dataSource, String dbName) { synchronized (this.dataSourceCache) { SQLErrorCodes sec = getErrorCodes(dbName); this.dataSourceCache.put(dataSource, sec); return sec; } }
/** * Set the DataSource for this translator. * <p>Setting this property will cause a Connection to be obtained from * the DataSource to get the meta-data. * @param dataSource the DataSource to use to find meta-data and establish * which error codes are usable * @see SQLErrorCodesFactory#getErrorCodes(javax.sql.DataSource) * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public void setDataSource(DataSource dataSource) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource); }
/** * Set the DataSource for this translator. * <p>Setting this property will cause a Connection to be obtained from * the DataSource to get the metadata. * @param dataSource DataSource to use to find metadata and establish * which error codes are usable * @see SQLErrorCodesFactory#getErrorCodes(javax.sql.DataSource) * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public void setDataSource(DataSource dataSource) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource); }
/** * Set the database product name for this translator. * <p> * Setting this property will avoid obtaining a Connection from the DataSource to get the meta-data. * * @param dbName the database product name that identifies the error codes entry. * @see SQLErrorCodesFactory#getErrorCodes(String) * @see io.r2dbc.spi.ConnectionFactoryMetadata#getName() */ public void setDatabaseProductName(String dbName) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(dbName); }
/** * Set the database product name for this translator. * <p>Setting this property will avoid obtaining a Connection from the DataSource * to get the metadata. * @param dbName the database product name that identifies the error codes entry * @see SQLErrorCodesFactory#getErrorCodes(String) * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public void setDatabaseProductName(String dbName) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(dbName); }
/** * Set the database product name for this translator. * <p> * Setting this property will avoid obtaining a Connection from the DataSource to get the meta-data. * * @param dbName the database product name that identifies the error codes entry. * @see SQLErrorCodesFactory#getErrorCodes(String) * @see io.r2dbc.spi.ConnectionFactoryMetadata#getName() */ public void setDatabaseProductName(String dbName) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(dbName); }
/** * Set the database product name for this translator. * <p>Setting this property will avoid obtaining a Connection from the DataSource * to get the meta-data. * @param dbName the database product name that identifies the error codes entry * @see SQLErrorCodesFactory#getErrorCodes(String) * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public void setDatabaseProductName(String dbName) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(dbName); }
/** * Associate the specified database name with the given {@link DataSource}. * @param dataSource the {@code DataSource} identifying the database * @param databaseName the corresponding database name as stated in the error codes * definition file (must not be {@code null}) * @return the corresponding {@code SQLErrorCodes} object (never {@code null}) * @see #unregisterDatabase(DataSource) */ public SQLErrorCodes registerDatabase(DataSource dataSource, String databaseName) { SQLErrorCodes sec = getErrorCodes(databaseName); if (logger.isDebugEnabled()) { logger.debug("Caching SQL error codes for DataSource [" + identify(dataSource) + "]: database product name is '" + databaseName + "'"); } this.dataSourceCache.put(dataSource, sec); return sec; }
/** * * @param conn * @return * @since 0.8.2 */ protected SQLExceptionTranslator getSQLExceptionTranslator(Connection conn) { DataSource dataSource = DbcHelper.getDataSource(conn); SQLExceptionTranslator translator = cachedSQLExceptionTranslators.get(dataSource); if (translator == null) { translator = new SQLErrorCodeSQLExceptionTranslator( sqlErrorCodesFactory.getErrorCodes(dataSource)); SQLExceptionTranslator existing = cachedSQLExceptionTranslators.putIfAbsent(dataSource, translator); if (existing != null) { translator = existing; } } return translator; }
/** * Set the DataSource for this translator. * <p> * Setting this property will cause a Connection to be obtained from the DataSource to get the meta-data. * * @param connectionFactory {@link ConnectionFactory} to use to find meta-data and establish which error codes are * usable. * @see SQLErrorCodesFactory#getErrorCodes(String) * @see io.r2dbc.spi.ConnectionFactoryMetadata#getName() */ public void setConnectionFactory(ConnectionFactory connectionFactory) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(connectionFactory.getMetadata().getName()); }
/** * Set the DataSource for this translator. * <p> * Setting this property will cause a Connection to be obtained from the DataSource to get the meta-data. * * @param connectionFactory {@link ConnectionFactory} to use to find meta-data and establish which error codes are * usable. * @see SQLErrorCodesFactory#getErrorCodes(String) * @see io.r2dbc.spi.ConnectionFactoryMetadata#getName() */ public void setConnectionFactory(ConnectionFactory connectionFactory) { this.sqlErrorCodes = SQLErrorCodesFactory.getInstance().getErrorCodes(connectionFactory.getMetadata().getName()); }