@Override public <Z, D extends Database<Z>> void synchronize( SynchronizationContext<Z, D> context, TableProperties table) throws SQLException { final String tableName = table.getName().getDMLName(); final Collection<String> columns = table.getColumns(); final String commaDelimitedColumns = Strings.join(columns, Strings.PADDED_COMMA); final String selectSQL = String.format("SELECT %s FROM %s", commaDelimitedColumns, tableName); final String deleteSQL = context.getDialect().getTruncateTableSQL(table); final String insertSQL = String.format( "INSERT INTO %s (%s) VALUES (%s)", tableName, commaDelimitedColumns, Strings.join( Collections.nCopies(columns.size(), Strings.QUESTION), Strings.PADDED_COMMA)); Connection sourceConnection = context.getConnection(context.getSourceDatabase()); final Statement selectStatement = sourceConnection.createStatement(); try { selectStatement.setFetchSize(this.fetchSize); Callable<ResultSet> callable = new Callable<ResultSet>() { @Override public ResultSet call() throws SQLException { logger.log(Level.DEBUG, selectSQL); return selectStatement.executeQuery(selectSQL); } }; Future<ResultSet> future = context.getExecutor().submit(callable); Connection targetConnection = context.getConnection(context.getTargetDatabase()); Statement deleteStatement = targetConnection.createStatement(); try { logger.log(Level.DEBUG, deleteSQL); int deletedRows = deleteStatement.executeUpdate(deleteSQL); logger.log(Level.INFO, Messages.DELETE_COUNT.getMessage(), deletedRows, tableName); } finally { Resources.close(deleteStatement); } logger.log(Level.DEBUG, insertSQL); PreparedStatement insertStatement = targetConnection.prepareStatement(insertSQL); try { int statementCount = 0; ResultSet resultSet = future.get(); while (resultSet.next()) { int index = 0; for (String column : table.getColumns()) { index += 1; int type = context.getDialect().getColumnType(table.getColumnProperties(column)); Object object = context.getSynchronizationSupport().getObject(resultSet, index, type); if (resultSet.wasNull()) { insertStatement.setNull(index, type); } else { insertStatement.setObject(index, object, type); } } insertStatement.addBatch(); statementCount += 1; if ((statementCount % this.maxBatchSize) == 0) { insertStatement.executeBatch(); insertStatement.clearBatch(); } insertStatement.clearParameters(); } if ((statementCount % this.maxBatchSize) > 0) { insertStatement.executeBatch(); } logger.log(Level.INFO, Messages.INSERT_COUNT.getMessage(), statementCount, table); } catch (ExecutionException e) { throw ExceptionType.getExceptionFactory(SQLException.class).createException(e.getCause()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new SQLException(e); } finally { Resources.close(insertStatement); } } finally { Resources.close(selectStatement); } }
@Override public <Z, D extends Database<Z>> void restoreConstraints(SynchronizationContext<Z, D> context) throws SQLException { context.getSynchronizationSupport().restoreForeignKeys(); }