java - java 中的突然结果集行为

标签 java jdbc resultset

过去 2-3 天我遇到了一个问题。这似乎是一个小问题,但我还没能捕获它。

问题出在代码中的结果集 rs 上。 方法 mergeTable 中的 while (rs.next()) 语句行为突然。第一次,在某些情况下它会进入 while 循环,有时不会,并且在少数情况下,当它进入 while 循环时,它会突然抛出结果集耗尽异常。我用谷歌搜索,发现结果集可能已被访问代码的其他线程关闭。但这是一个简单的独立 java 应用程序,不是多线程的。

我还注意其他两个结果集,check 和 checktarget 不会干扰结果集 rs。我也将结束报表和结果集。 你能看一下代码,看看我是否遗漏了一些东西。

使用的两个数据库实例是10.180.22.93:1521:V3demo(称为源数据库)和10.180.22.93:1521:fusiondb(称为目标数据库)。 rs 结果集来自源数据库。check 和 checktarget 结果集来自目标数据库。因此,结果集 rs 将来自源数据库的表 A,结果集 check 和 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;
}

最佳答案

哦,天哪,你这里发生的事情太多了。我看到 HTML 和 JDBC 代码混合在一起。这是个坏主意。

数据库连接不是线程安全的。您说您正在小心确保结果集不会干扰,但此错误表明情况并非如此。我会重构这段代码来隔离持久性并使一切变得更简单。这对我来说看起来是错误的,但我不愿意深入研究它来找出原因。

关于java - java 中的突然结果集行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6263623/

相关文章:

java - 如何将包含 1000 万条记录的巨大结果集转换为 java 中的列表?

java - 将 SQL 选择结果集直接打印到 HTML 网页上?

scala - 结果集到 Scala HashMap

java - Neo4j:在 JDBC 中不支持创建唯一的

java - j2ee : I have a problem regarding jboss server

java - 是否可以从 Checkstyle 中排除日志语句?

java - Guava:为什么没有 Lists.filter() 函数?

java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown column 'raj' in 'field list' at sun. 反射(reflect)

java - 尝试切换到(使用)数据库时出现 JDBC "Error in MySQL Syntax"

java - 如何进行ETL流程性能测试