Я застрял с проблемой в течение последних 2-3 дней. Кажется, это небольшая проблема, но я не смог ее поймать.
Проблема связана с набором результатов в коде. Оператор while (rs.next ()) в методе mergeTable ведет себя неожиданно. Впервые в некоторых случаях он входит в цикл while, иногда - нет, а в некоторых случаях, когда он входит в цикл while, он неожиданно выдает исключенное исключение из результирующего набора. Я гуглил и обнаружил, что набор результатов мог быть закрыт другим потоком, обращающимся к коду. Но это простое автономное Java-приложение, которое не является многопоточным.
Я также забочусь о двух других наборах результатов, check и checktarget не мешают результатам. Я закрываю заявления и наборы результатов также.
Не могли бы вы взглянуть на код и посмотреть, не упускаю ли я что-то.
Используются два экземпляра базы данных: 10.180.22.93:1521:V3demo (называемый исходной базой данных) и 10.180.22.93:1521:fusiondb (называемая целевой базой данных). rs resultset - из исходной базы данных. Resultsets check и checktarget - из целевой базы данных. Таким образом, набор результатов rs будет взят из таблицы A из исходной базы данных, а проверка наборов результатов и checktarget будут из таблицы A из целевой базы данных.
static String mergeTable() throws Exception {
String result = "ERROR";
int error = 0;
String tableString = "<table " + tablename + ">";
PreparedStatement preparedSelect = null;
PreparedStatement preparedSelectTarget = null;
Statement selectSourceStmt = null;
ResultSet checkTarget = null;
ResultSet rs = null;
try {
logger.println("====================================================================================");
logger.println("Processing table:" + tablename);
System.out.println("====================================================================================");
System.out.println("Processing table:" + tablename);
// Create query to fetch records from the source
String sourceQuery = "SELECT * FROM " + tablename;
if (owner.trim().equals("F1") || owner.trim().equals("C1") || owner.trim().equals("CM"))
sourceQuery = sourceQuery + " WHERE OWNER_FLG='" + owner + "'";
// Get the result set
selectSourceStmt = source.createStatement();
rs = selectSourceStmt.executeQuery(sourceQuery);
System.out.println(sourceQuery);
String selectSQL = "SELECT COUNT(*) FROM " + tablename + " WHERE ";
String selectSQLTarget = "SELECT * FROM " + tablename + " WHERE "; // ankush
ResultSetMetaData metaData = rs.getMetaData();
List list = new ArrayList();
List typesList = new ArrayList();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String columnName = metaData.getColumnName(i);
list.add(columnName); // list contains the entire list of columns of the source
typesList.add(metaData.getColumnType(i));
}
for (int i = 1; i < keys.length; i++) {
if (i == 1) {
selectSQL = selectSQL + " " + keys[i] + "= ?";
selectSQLTarget = selectSQLTarget + " " + keys[i] + "= ?"; //ankush
}
else {
selectSQL = selectSQL + " AND " + keys[i] + "= ?";
selectSQLTarget = selectSQLTarget + " AND " + keys[i] + "= ?"; //ankush
}
}
logger.println("Select SQL:" + selectSQL);
logger.println("selectSQLTarget:" + selectSQLTarget); //ankush
preparedSelect = target.prepareStatement(selectSQL);
preparedSelectTarget = target.prepareStatement(selectSQLTarget); //ankush
int updateCount = 0, insertCount = 0, errorCount = 0;
// rs contains the entire table snapshot of source based on the owner flag
if (rs != null) {
while (rs.next()) {
try {
int i, count;
// check if record exists or not; keys contain the values of primary columns specified in the.lst file
for (int j = 1; j < keys.length; j++) {
preparedSelect.setObject(j, rs.getObject(keys[j])); // for every single row in source, corresponding rows are fetched from target.Here, where clause is being prepared
}
ResultSet check = preparedSelect.executeQuery(); // check is the target resultset for the primary key values in current row of source resultset
check.next();
count = check.getInt(1); // count gives the row/s fetched from target based on the values in source.
check.close();
// check if record exists or not; keys contain the values of primary columns specified in the.lst file
for (int j = 1; j < keys.length; j++) {
// for every single row in source, corresponding rows are fetched from target.Here, where clause is being prepared
preparedSelectTarget.setObject(j, rs.getObject(keys[j]));
}
// check is the target resultset for the primary key values in current row of source resultset
checkTarget = preparedSelectTarget.executeQuery();
checkTarget.next();
// if record exists UPDATE CONDITION
if (true) { // if there is a record in target for a row in source, update target
String rowString = "<row>";
String rowDiffFlag = "N";
// if merge flag is Y
if (mergeFlag.equals("Y")) {
String colDiffFlag = "";
String sourceColVal = "";
String targetColVal = "";
// list contains the column names
for (i = 0; i < list.size(); i++) {
System.out.println("value of i " + i);
}
i++; // ?????
} else {
logger.print("Did not update Record:");
}
rowString = rowString + "</row>";
if (rowDiffFlag.equals("Y")) {
tableString = tableString + rowString;
}
} else { // if there is no record in target for a row in source, insert into target
String sourceColVal = "";
String rowString = "<row>";
for (i = 0; i < list.size(); i++) { //looping through columns in a row
System.out.println("column " + i);
}
rowString = rowString + "</row>";
tableString = tableString + rowString;
}
} catch (Exception e1) {
e1.printStackTrace(logger);
}
}
}
} catch (Exception e) {
e.printStackTrace(logger);
} finally {
preparedSelect.close();
preparedSelectTarget.close();
selectSourceStmt.close();
checkTarget.close();
rs.close();
}
tableString = tableString + "</table>";
formXmlString(tableString);
if (error == 0) result = "SUCCESS";
return result;
}