From 0756d13662913a7c2df1a0a4d23cd50175dc42f4 Mon Sep 17 00:00:00 2001 From: Hari Shreedharan Date: Mon, 12 May 2014 12:23:12 -0700 Subject: [PATCH] SQOOP-1320. Sqoop2: Log entire SQLExceptions chain in GenericJdbcExecutor (Jaroslav Cecho via Hari Shreedharan) --- .../connector/jdbc/GenericJdbcExecutor.java | 65 +++++++++++-------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/connector/connector-generic-jdbc/src/main/java/org/apache/sqoop/connector/jdbc/GenericJdbcExecutor.java b/connector/connector-generic-jdbc/src/main/java/org/apache/sqoop/connector/jdbc/GenericJdbcExecutor.java index 9fd2e4fb..5dc3c0b1 100644 --- a/connector/connector-generic-jdbc/src/main/java/org/apache/sqoop/connector/jdbc/GenericJdbcExecutor.java +++ b/connector/connector-generic-jdbc/src/main/java/org/apache/sqoop/connector/jdbc/GenericJdbcExecutor.java @@ -48,8 +48,8 @@ public GenericJdbcExecutor(String driver, String url, GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0000, driver, e); } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0001, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0001, e); } } @@ -60,8 +60,8 @@ public ResultSet executeQuery(String sql) { return statement.executeQuery(sql); } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); } } @@ -69,6 +69,7 @@ public void setAutoCommit(boolean autoCommit) { try { connection.setAutoCommit(autoCommit); } catch (SQLException e) { + logSQLException(e); throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); } } @@ -104,23 +105,21 @@ public void migrateData(String fromTable, String toTable) { GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0018); } } catch(SQLException e) { - LOG.error("Got SQLException while migrating data from: " + fromTable + - " to: " + toTable, e); - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0018, e); + logSQLException(e, "Got SQLException while migrating data from: " + fromTable + " to: " + toTable); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0018, e); } finally { if(stmt != null) { try { stmt.close(); } catch(SQLException e) { - LOG.warn("Got SQLException at the time of closing statement.", e); + logSQLException(e, "Got SQLException at the time of closing statement."); } } if(oldAutoCommit != null) { try { connection.setAutoCommit(oldAutoCommit); } catch(SQLException e) { - LOG.warn("Got SQLException while setting autoCommit mode.", e); + logSQLException(e, "Got SQLException while setting autoCommit mode."); } } } @@ -139,7 +138,7 @@ public long getTableRowCount(String tableName) { if(resultSet != null) resultSet.close(); } catch(SQLException e) { - LOG.warn("Got SQLException while closing resultset.", e); + logSQLException(e, "Got SQLException while closing resultset."); } } } @@ -151,8 +150,8 @@ public void executeUpdate(String sql) { statement.executeUpdate(sql); } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); } } @@ -162,8 +161,8 @@ public void beginBatch(String sql) { ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); } } @@ -174,8 +173,8 @@ public void addBatch(Object[] array) { } preparedStatement.addBatch(); } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); } } @@ -186,8 +185,8 @@ public void executeBatch(boolean commit) { connection.commit(); } } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); } } @@ -197,8 +196,8 @@ public void endBatch() { preparedStatement.close(); } } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0002, e); } } @@ -216,8 +215,8 @@ public String getPrimaryKey(String table) { } } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0003, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0003, e); } } @@ -237,8 +236,8 @@ public String[] getQueryColumns(String query) { return columns; } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0003, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0003, e); } } @@ -256,8 +255,8 @@ public boolean existTable(String table) { } } catch (SQLException e) { - throw new SqoopException( - GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0003, e); + logSQLException(e); + throw new SqoopException(GenericJdbcConnectorError.GENERIC_JDBC_CONNECTOR_0003, e); } } @@ -296,8 +295,18 @@ public void close() { connection.close(); } catch (SQLException e) { - // TODO: Log the exception + logSQLException(e); } } + private void logSQLException(SQLException e) { + logSQLException(e, "Caught SQLException:"); + } + + private void logSQLException(SQLException e, String message) { + LOG.error(message, e); + if(e.getNextException() != null) { + logSQLException(e.getNextException(), "Caused by:"); + } + } }