/** * Translate the given {@link SQLException} into a generic {@link DataAccessException}. * @param task readable text describing the task being attempted * @param sql the SQL query or update that caused the problem (may be {@code null}) * @param ex the offending {@code SQLException} * @return a DataAccessException wrapping the {@code SQLException} (never {@code null}) * @since 5.0 * @see #getExceptionTranslator() */ protected DataAccessException translateException(String task, @Nullable String sql, SQLException ex) { DataAccessException dae = getExceptionTranslator().translate(task, sql, ex); return (dae != null ? dae : new UncategorizedSQLException(task, sql, ex)); }
/** * Pre-checks the arguments, calls {@link #doTranslate}, and invokes the * {@link #getFallbackTranslator() fallback translator} if necessary. */ @Override @NonNull public DataAccessException translate(String task, @Nullable String sql, SQLException ex) { Assert.notNull(ex, "Cannot translate a null SQLException"); DataAccessException dae = doTranslate(task, sql, ex); if (dae != null) { // Specific exception match found. return dae; } // Looking for a fallback... SQLExceptionTranslator fallback = getFallbackTranslator(); if (fallback != null) { dae = fallback.translate(task, sql, ex); if (dae != null) { // Fallback exception match found. return dae; } } // We couldn't identify it more precisely. return new UncategorizedSQLException(task, sql, ex); }
@Override public <T> T execute(String sql, PreparedStatementCallback<T> action) throws DataAccessException { list.add(sql); try { return action.doInPreparedStatement(ps); } catch (SQLException e) { throw new UncategorizedSQLException("doInPreparedStatement", sql, e); } } };
@Override protected void doInTransactionWithoutResult(TransactionStatus status) { // something transactional assertEquals(con, DataSourceUtils.getConnection(ds)); TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds); try { assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()); // should be ignored dsProxy.getConnection().close(); } catch (SQLException ex) { throw new UncategorizedSQLException("", "", ex); } } });
@Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is new transaction", status.isNewTransaction()); Connection con = DataSourceUtils.getConnection(dsToUse); if (createStatement) { try { con.createStatement(); } catch (SQLException ex) { throw new UncategorizedSQLException("", "", ex); } } throw ex; } });
throw new UncategorizedSQLException("", "", ex); throw new UncategorizedSQLException("", "", ex);
throw new UncategorizedSQLException("", "", ex); throw new UncategorizedSQLException("", "", ex);
@Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is new transaction", status.isNewTransaction()); assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); Connection tCon = DataSourceUtils.getConnection(dsToUse); try { if (createStatement) { tCon.createStatement(); } } catch (SQLException ex) { throw new UncategorizedSQLException("", "", ex); } } });
public DataAccessException translate(String task, String sql, SQLException e) { switch (e.getErrorCode()) { case 942: return new TableDoesNotExistException("Table does not exist", e); case 959: return new TablespaceDoesNotExistException("Tablespace does not exist", e); case 1918: return new UserDoesNotExistException("User does not exist", e); } return new UncategorizedSQLException("(" + task + "): encountered SQLException [" + e.getMessage() + "]", sql, e); }
/** * Pre-checks the arguments, calls {@link #doTranslate}, and invokes the * {@link #getFallbackTranslator() fallback translator} if necessary. */ public DataAccessException translate(String task, String sql, SQLException ex) { Assert.notNull(ex, "Cannot translate a null SQLException"); if (task == null) { task = ""; } if (sql == null) { sql = ""; } DataAccessException dex = doTranslate(task, sql, ex); if (dex != null) { // Specific exception match found. return dex; } // Looking for a fallback... SQLExceptionTranslator fallback = getFallbackTranslator(); if (fallback != null) { return fallback.translate(task, sql, ex); } // We couldn't identify it more precisely. return new UncategorizedSQLException(task, sql, ex); }
/** * Pre-checks the arguments, calls {@link #doTranslate}, and invokes the * {@link #getFallbackTranslator() fallback translator} if necessary. */ @Override @NonNull public DataAccessException translate(String task, @Nullable String sql, SQLException ex) { Assert.notNull(ex, "Cannot translate a null SQLException"); DataAccessException dae = doTranslate(task, sql, ex); if (dae != null) { // Specific exception match found. return dae; } // Looking for a fallback... SQLExceptionTranslator fallback = getFallbackTranslator(); if (fallback != null) { dae = fallback.translate(task, sql, ex); if (dae != null) { // Fallback exception match found. return dae; } } // We couldn't identify it more precisely. return new UncategorizedSQLException(task, sql, ex); }
/** * Convert the given GuzzException to an appropriate exception * from the <code>org.springframework.dao</code> hierarchy. * @param ex GuzzException that occured * @return the corresponding DataAccessException instance * @see GuzzAccessor#convertGuzzAccessException * @see GuzzTransactionManager#convertGuzzAccessException */ public static DataAccessException convertGuzzAccessException(GuzzException ex) { if(ex instanceof JDBCException){ JDBCException e = (JDBCException) ex ; return new UncategorizedSQLException(e.getMessage(), e.getSQL(), e.getSQLException()) ; } return new DataAccessResourceFailureException(ex.getMessage(), ex); }