public SQLExceptionSubclassTranslator() { setFallbackTranslator(new SQLStateSQLExceptionTranslator()); }
/** * Return the exception translator for this instance. * <p>Creates a default {@link SQLErrorCodeSQLExceptionTranslator} * for the specified DataSource if none set, or a * {@link SQLStateSQLExceptionTranslator} in case of no DataSource. * @see #getDataSource() */ public SQLExceptionTranslator getExceptionTranslator() { SQLExceptionTranslator exceptionTranslator = this.exceptionTranslator; if (exceptionTranslator != null) { return exceptionTranslator; } synchronized (this) { exceptionTranslator = this.exceptionTranslator; if (exceptionTranslator == null) { DataSource dataSource = getDataSource(); if (dataSource != null) { exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource); } else { exceptionTranslator = new SQLStateSQLExceptionTranslator(); } this.exceptionTranslator = exceptionTranslator; } return exceptionTranslator; } }
/** * Creates a default SQLErrorCodeSQLExceptionTranslator for the specified * DataSource if none is set. * * @return the exception translator for this instance. */ protected SQLExceptionTranslator getExceptionTranslator() { synchronized(this) { if (exceptionTranslator == null) { if (dataSource != null) { exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource); } else { exceptionTranslator = new SQLStateSQLExceptionTranslator(); } } } return exceptionTranslator; }
@Test(expected = IllegalArgumentException.class) public void testTranslateNullException() throws Exception { new SQLStateSQLExceptionTranslator().translate("", "", null); }
private void doTest(String sqlState, Class<?> dataAccessExceptionType) { SQLException ex = new SQLException(REASON, sqlState); SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator(); DataAccessException dax = translator.translate(TASK, SQL, ex); assertNotNull("Translation must *never* result in a null DataAccessException being returned.", dax); assertEquals("Wrong DataAccessException type returned as the result of the translation", dataAccessExceptionType, dax.getClass()); assertNotNull("The original SQLException must be preserved in the translated DataAccessException", dax.getCause()); assertSame("The exact same original SQLException must be preserved in the translated DataAccessException", ex, dax.getCause()); }
@Before public void setUp() throws SQLException { given(connection.createStatement()).willReturn(statement); given(connection.prepareStatement(anyString())).willReturn(preparedStatement); given(statement.executeQuery(anyString())).willReturn(resultSet); given(preparedStatement.executeQuery()).willReturn(resultSet); given(resultSet.next()).willReturn(true, true, false); given(resultSet.getString(1)).willReturn("tb1", "tb2"); given(resultSet.getInt(2)).willReturn(1, 2); template.setDataSource(new SingleConnectionDataSource(connection, false)); template.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); template.afterPropertiesSet(); }
/** * Test that we see an SQLException translated using Error Code. * If we provide the SQLExceptionTranslator, we shouldn't use a connection * to get the metadata */ @Test public void testUseCustomSQLErrorCodeTranslator() throws Exception { // Bad SQL state final SQLException sqlException = new SQLException("I have a known problem", "07000", 1054); final String sql = "SELECT ID FROM CUSTOMER"; given(this.resultSet.next()).willReturn(true); given(this.connection.createStatement()).willReturn(this.preparedStatement); JdbcTemplate template = new JdbcTemplate(); template.setDataSource(this.dataSource); // Set custom exception translator template.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); template.afterPropertiesSet(); this.thrown.expect(BadSqlGrammarException.class); this.thrown.expect(exceptionCause(sameInstance(sqlException))); try { template.query(sql, (RowCallbackHandler) rs -> { throw sqlException; }); } finally { verify(this.resultSet).close(); verify(this.preparedStatement).close(); verify(this.connection).close(); } }
@SuppressWarnings("unchecked") public Mock(MockType type) throws Exception { connection = mock(Connection.class); statement = mock(Statement.class); resultSet = mock(ResultSet.class); resultSetMetaData = mock(ResultSetMetaData.class); given(connection.createStatement()).willReturn(statement); given(statement.executeQuery(anyString())).willReturn(resultSet); given(resultSet.getMetaData()).willReturn(resultSetMetaData); given(resultSet.next()).willReturn(true, false); given(resultSet.getString(1)).willReturn("Bubba"); given(resultSet.getLong(2)).willReturn(22L); given(resultSet.getTimestamp(3)).willReturn(new Timestamp(1221222L)); given(resultSet.getObject(anyInt(), any(Class.class))).willThrow(new SQLFeatureNotSupportedException()); given(resultSet.getDate(3)).willReturn(new java.sql.Date(1221222L)); given(resultSet.getBigDecimal(4)).willReturn(new BigDecimal("1234.56")); given(resultSet.wasNull()).willReturn(type == MockType.TWO); given(resultSetMetaData.getColumnCount()).willReturn(4); given(resultSetMetaData.getColumnLabel(1)).willReturn( type == MockType.THREE ? "Last Name" : "name"); given(resultSetMetaData.getColumnLabel(2)).willReturn("age"); given(resultSetMetaData.getColumnLabel(3)).willReturn("birth_date"); given(resultSetMetaData.getColumnLabel(4)).willReturn("balance"); jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(new SingleConnectionDataSource(connection, false)); jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); jdbcTemplate.afterPropertiesSet(); }
t.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
/** * Return the JDBC exception translator for this instance. * <p>Creates a default SQLStateSQLExceptionTranslator, * if no exception translator explicitly specified. */ public SQLExceptionTranslator getJdbcExceptionTranslator() { if (this.jdbcExceptionTranslator == null) { this.jdbcExceptionTranslator = new SQLStateSQLExceptionTranslator(); } return this.jdbcExceptionTranslator; }
/** * Create an appropriate SQLExceptionTranslator for the given TransactionManager. * If a DataSource is found, a SQLErrorCodeSQLExceptionTranslator for the DataSource * is created; else, a SQLStateSQLExceptionTranslator as fallback. * @param transactionManager the TransactionManager to create the translator for * @return the SQLExceptionTranslator * @see #getDataSource * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator */ public static SQLExceptionTranslator newJdbcExceptionTranslator() { return new SQLStateSQLExceptionTranslator(); }
/** * Create an appropriate SQLExceptionTranslator for the given JDOManager. * If a DataSource is found, create a SQLErrorCodeSQLExceptionTranslator for the * DataSource; else, fall back to a SQLStateSQLExceptionTranslator. * @param jdoManager the JDOManager to create the translator for * @return the SQLExceptionTranslator * @see org.exolab.castor.jdo.JDOManager#getConnectionFactory * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator */ public static SQLExceptionTranslator newJdbcExceptionTranslator(JDOManager jdoManager) { // if (jdoManager != null) { // // Check for JDOManager's DataSource. // Object cf = jdoManager.getConnectionFactory(); // if (cf instanceof DataSource) { // return new SQLErrorCodeSQLExceptionTranslator((DataSource) cf); // } // } return new SQLStateSQLExceptionTranslator(); }
public SQLExceptionSubclassTranslator() { setFallbackTranslator(new SQLStateSQLExceptionTranslator()); }
public SQLExceptionSubclassTranslator() { setFallbackTranslator(new SQLStateSQLExceptionTranslator()); }
/** * Create an appropriate SQLExceptionTranslator for the given PersistenceManagerFactory. * <p>If a DataSource is found, creates a SQLErrorCodeSQLExceptionTranslator for the * DataSource; else, falls back to a SQLStateSQLExceptionTranslator. * @param connectionFactory the connection factory of the PersistenceManagerFactory * (may be {@code null}) * @return the SQLExceptionTranslator (never {@code null}) * @see javax.jdo.PersistenceManagerFactory#getConnectionFactory() * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator */ static SQLExceptionTranslator newJdbcExceptionTranslator(Object connectionFactory) { // Check for PersistenceManagerFactory's DataSource. if (connectionFactory instanceof DataSource) { return new SQLErrorCodeSQLExceptionTranslator((DataSource) connectionFactory); } else { return new SQLStateSQLExceptionTranslator(); } }
public static SQLExceptionTranslator newJdbcExceptionTranslator(SQLSessionFactory sessionFactory) { DataSource ds = getDataSource(sessionFactory); if (ds != null) { return new SQLErrorCodeSQLExceptionTranslator(ds); } return new SQLStateSQLExceptionTranslator(); }
/** * Create an appropriate SQLExceptionTranslator for the given SessionFactory. * If a DataSource is found, a SQLErrorCodeSQLExceptionTranslator for the DataSource * is created; else, a SQLStateSQLExceptionTranslator as fallback. * @param sessionFactory the SessionFactory to create the translator for * @return the SQLExceptionTranslator * @see #getDataSource * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator */ public static SQLExceptionTranslator newJdbcExceptionTranslator(SessionFactory sessionFactory) { DataSource ds = getDataSource(sessionFactory); if (ds != null) { return new SQLErrorCodeSQLExceptionTranslator(ds); } return new SQLStateSQLExceptionTranslator(); }
/** * Return the exception translator for this instance. * <p>Creates a default {@link SQLErrorCodeSQLExceptionTranslator} * for the specified DataSource if none set, or a * {@link SQLStateSQLExceptionTranslator} in case of no DataSource. * @see #getDataSource() */ public synchronized SQLExceptionTranslator getExceptionTranslator() { if (this.exceptionTranslator == null) { DataSource dataSource = getDataSource(); if (dataSource != null) { this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource); } else { this.exceptionTranslator = new SQLStateSQLExceptionTranslator(); } } return this.exceptionTranslator; }
/** * Return the JDBC exception translator for this transaction manager. * <p>Creates a default SQLErrorCodeSQLExceptionTranslator or SQLStateSQLExceptionTranslator * for the specified SessionFactory, if no exception translator explicitly specified. * @see #setJdbcExceptionTranslator */ public SQLExceptionTranslator getJdbcExceptionTranslator() { if (this.jdbcExceptionTranslator == null) { if (getDataSource() != null) { this.jdbcExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(getDataSource()); } else { this.jdbcExceptionTranslator = new SQLStateSQLExceptionTranslator(); } } return this.jdbcExceptionTranslator; }
/** * Constructor for use as a JavaBean. * The SqlErrorCodes or DataSource property must be set. */ public SQLErrorCodeSQLExceptionTranslator() { if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_16) { setFallbackTranslator(new SQLExceptionSubclassTranslator()); } else { setFallbackTranslator(new SQLStateSQLExceptionTranslator()); } }