@Override public void prepare() throws IOException { LOG.debug( "Preparing JDBC resource source (resource={}, table={})", profile.getResourceName(), script.getTableName()); try { this.resultSet = prepareResultSet(); } catch (SQLException e) { for (SQLException ex = e; ex != null; ex = ex.getNextException()) { WGLOG.error( ex, "E03001", profile.getResourceName(), script.getName(), script.getTableName(), script.getColumnNames()); } throw new IOException( MessageFormat.format( "Failed to prepare JDBC source (resource={0}, table={1}, columns={2})", profile.getResourceName(), script.getTableName(), script.getColumnNames()), e); } LOG.debug( "Creating ResultSet support {} for {}", script.getSupport().getClass().getName(), script.getColumnNames()); support = script.getSupport().createResultSetSupport(resultSet, script.getColumnNames()); }
@Override public void close() throws IOException { LOG.debug( "Closing JDBC resource source (resource={}, table={})", profile.getResourceName(), script.getTableName()); sawNext = false; if (resultSet != null) { try { resultSet.close(); resultSet = null; support = null; } catch (SQLException e) { for (SQLException ex = e; ex != null; ex = ex.getNextException()) { WGLOG.warn( ex, "W03001", profile.getResourceName(), script.getName(), script.getTableName(), script.getColumnNames()); } } try { if (statement != null) { statement.close(); } } catch (SQLException e) { for (SQLException ex = e; ex != null; ex = ex.getNextException()) { WGLOG.warn( ex, "W03001", profile.getResourceName(), script.getName(), script.getTableName(), script.getColumnNames()); } } } try { connection.close(); } catch (SQLException e) { for (SQLException ex = e; ex != null; ex = ex.getNextException()) { WGLOG.warn(ex, "W02001", profile.getResourceName(), script.getName()); } } }
private ResultSet prepareResultSet() throws SQLException { String sql = createSql(); statement = connection.createStatement(); boolean succeed = false; try { WGLOG.info( "I03001", profile.getResourceName(), script.getName(), script.getTableName(), script.getColumnNames()); if (profile.getBatchGetUnit() != 0) { statement.setFetchSize(profile.getBatchGetUnit()); } LOG.debug("Executing SQL: {}", sql); ResultSet result = statement.executeQuery(sql); LOG.debug("Executed SQL: {}", sql); WGLOG.info( "I03002", profile.getResourceName(), script.getName(), script.getTableName(), script.getColumnNames()); succeed = true; return result; } finally { if (succeed == false) { try { statement.close(); } catch (SQLException e) { for (SQLException ex = e; ex != null; ex = ex.getNextException()) { WGLOG.warn( ex, "W03001", profile.getResourceName(), script.getName(), script.getTableName(), script.getColumnNames()); } } } } }
@Override public boolean next() throws IOException { try { sawNext = support.next(object); return sawNext; } catch (SQLException e) { sawNext = false; for (SQLException ex = e; ex != null; ex = ex.getNextException()) { WGLOG.error( ex, "E03001", profile.getResourceName(), script.getName(), script.getTableName(), script.getColumnNames()); } throw new IOException( MessageFormat.format( "Failed to fetch next object from JDBC source (resource={0}, table={1})", profile.getResourceName(), script.getTableName()), e); } }
/** * Converts {@link ResourceProfile} into {@link JdbcProfile}. * * @param profile target profile * @return the converted profile * @throws IllegalArgumentException if profile is not valid, or any parameter is {@code null} */ public static JdbcProfile convert(ResourceProfile profile) { if (profile == null) { throw new IllegalArgumentException("profile must not be null"); // $NON-NLS-1$ } String resourceName = profile.getName(); ClassLoader classLoader = profile.getContext().getClassLoader(); String driver = extract(profile, KEY_DRIVER); String url = extract(profile, KEY_URL); String user = extract(profile, KEY_USER, null); String password = extract(profile, KEY_PASSWORD, null); Map<String, String> connectionProperties = PropertiesUtil.createPrefixMap(profile.getConfiguration(), KEY_PREFIX_PROPERTIES); JdbcProfile result = new JdbcProfile( resourceName, classLoader, driver, url, user, password, connectionProperties); int batchGetUnit = extractInt(profile, KEY_BATCH_GET_UNIT, 0, DEFAULT_BATCH_GET_UNIT); long batchPutUnit = extractLong(profile, KEY_BATCH_PUT_UNIT, 1, DEFAULT_BATCH_PUT_UNIT); int connectRetryCount = extractInt(profile, KEY_CONNECT_RETRY_COUNT, 0, DEFAULT_CONNECT_RETRY_COUNT); int connectRetryInterval = extractInt(profile, KEY_CONNECT_RETRY_INTERVAL, 1, DEFAULT_CONNECT_RETRY_INTERVAL); String truncateStatement = extract(profile, KEY_TRUNCATE_STATEMENT, DEFAULT_TRUNCATE_STATEMENT); try { MessageFormat.format(truncateStatement, "dummy"); } catch (IllegalArgumentException e) { WGLOG.error("E00001", profile.getName(), KEY_TRUNCATE_STATEMENT, truncateStatement); throw new IllegalArgumentException( MessageFormat.format( "The \"{1}\" must be a valid MessageFormat: {2} (resource={0})", profile.getName(), KEY_TRUNCATE_STATEMENT, truncateStatement), e); } result.setBatchGetUnit(batchGetUnit); result.setBatchPutUnit(batchPutUnit); result.setConnectRetryCount(connectRetryCount); result.setConnectRetryInterval(connectRetryInterval); result.setTruncateStatement(truncateStatement); return result; }