/** * Constructor for use as a JavaBean. * The SqlErrorCodes or DataSource property must be set. */ public SQLErrorCodeSQLExceptionTranslator() { setFallbackTranslator(new SQLExceptionSubclassTranslator()); }
public SQLExceptionSubclassTranslator() { setFallbackTranslator(new SQLStateSQLExceptionTranslator()); }
/** * 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); }
/** * 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); }
/** * Specify the database product name for the DataSource that this accessor uses. * This allows to initialize a SQLErrorCodeSQLExceptionTranslator without * obtaining a Connection from the DataSource to get the meta-data. * @param dbName the database product name that identifies the error codes entry * @see SQLErrorCodeSQLExceptionTranslator#setDatabaseProductName * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public void setDatabaseProductName(String dbName) { this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dbName); }
@Test(expected = IllegalArgumentException.class) public void testTranslateNullException() throws Exception { new SQLStateSQLExceptionTranslator().translate("", "", null); }
/** * Check that a known database produces recognizable codes. */ @Test public void testDefaultInstanceWithOracle() { SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("Oracle"); assertIsOracle(sec); }
private void assertIsEmpty(SQLErrorCodes sec) { // Codes should be empty assertEquals(0, sec.getBadSqlGrammarCodes().length); assertEquals(0, sec.getDataIntegrityViolationCodes().length); }
@Override public void afterPropertiesSet() { this.translators.forEach((dbName, translator) -> CustomSQLExceptionTranslatorRegistry.getInstance().registerTranslator(dbName, translator)); }
/** * Obtain the DataSource for actual use. * @return the DataSource (never {@code null}) * @throws IllegalStateException in case of no DataSource set * @since 5.0 */ protected DataSource obtainDataSource() { DataSource dataSource = getDataSource(); Assert.state(dataSource != null, "No DataSource set"); return dataSource; }
/** * Create a SQL error code translator for the given database product name. * Invoking this constructor 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 * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public SQLErrorCodeSQLExceptionTranslator(String dbName) { this(); setDatabaseProductName(dbName); }
/** * Create a SQL error code translator for the given DataSource. * Invoking this constructor 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 */ public SQLErrorCodeSQLExceptionTranslator(DataSource dataSource) { this(); setDataSource(dataSource); }
@Test public void testDB2RecognizedFromMetadata() throws Exception { SQLErrorCodes sec = getErrorCodesFromDataSource("DB2", null); assertIsDB2(sec); sec = getErrorCodesFromDataSource("DB2/", null); assertIsDB2(sec); sec = getErrorCodesFromDataSource("DB-2", null); assertIsEmpty(sec); }
@Test public void testOracleRecognizedFromMetadata() throws Exception { SQLErrorCodes sec = getErrorCodesFromDataSource("Oracle", null); assertIsOracle(sec); }
@Test public void testHsqlRecognizedFromMetadata() throws Exception { SQLErrorCodes sec = getErrorCodesFromDataSource("HSQL Database Engine", null); assertIsHsql(sec); }
@Test public void testSQLServerRecognizedFromMetadata() throws Exception { SQLErrorCodes sec = getErrorCodesFromDataSource("MS-SQL", null); assertIsSQLServer(sec); }
@Test public void testHanaIsRecognizedFromMetadata() throws Exception { SQLErrorCodes sec = getErrorCodesFromDataSource("SAP DB", null); assertIsHana(sec); }
@Test public void testTranslateUncategorized() throws Exception { doTest("00000000", UncategorizedSQLException.class); }
/** * 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); }
@Test public void testTranslateTransientDataAccessResourceFailure() throws Exception { doTest("S1", TransientDataAccessResourceException.class); }