连接突然关闭

时间:2018-03-09 15:42:59

标签: java postgresql jdbc

我正在开发一个Java程序,它将获取源端和目标端的所有表的总行数,以进行数据计数验证。 目前,我已经编写了代码来从Source表中获取计数。 以下是连接类:

public static Connection getGpConnection() {
        try {
            Class.forName("org.postgresql.Driver");
            if(gpConnection == null) {
                gpConnection = DriverManager.getConnection("hostname", "username", "password");
                return gpConnection;
            } else {
            return gpConnection;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

获取所有源表的行数的方法:

public Map<String,Long> getGpTableCount() throws SQLException {
        Map<String, String> gpTableMap = getScopeTableList();
        Set<String> gpTableSet = gpTableMap.keySet();
        gpCountMap = new HashMap<String, Long>();
        Iterator<String> keySetIterator_gpTables = gpTableSet.iterator();
        Connection gpAnalyticsCon = (Connection) DbManager.getGpConnection();
        while(keySetIterator_gpTables.hasNext()) {
            String gpSchemaTableName = keySetIterator_gpTables.next();  // Ex: schema.tablename
            String[] gpparts = gpSchemaTableName.split("\\.");  // schema, tablename
            String gpTable = gpparts[1];
            String gpCountQuery = "select '" + gpTable + "' as TableName, count(*) as Count, source_system_name from " + gpSchemaTableName + " group by source_system_name order by source_system_name limit 30";
            // resultSet(1): TableName, resultSet(2): count, resultSet(3): source_system_name
            try {
                gpAnalyticsCon.setAutoCommit(false);
                PreparedStatement gp_pstmnt = gpAnalyticsCon.prepareStatement(gpCountQuery);
                ResultSet gpCountRs = gp_pstmnt.executeQuery();
                while(gpCountRs.next()) {
                    gpCountMap.put(gpTable + ": " + gpCountRs.getString(3), gpCountRs.getLong(2));
                }
            } catch(SQLException e) {
                e.printStackTrace();
            } catch(Exception e) {
                e.printStackTrace();
            } finally {
                if(gpAnalyticsCon != null) {
                //  gpAnalyticsCon.setAutoCommit(true);
                    gpAnalyticsCon.commit();
                    System.out.println("Closing the connection: Divya");
                    gpAnalyticsCon.close();
                }
            }
        }
        return gpCountMap;
    }

问题是,在对所有源系统中的一个表执行后,连接将关闭,但出现以下异常:

Tablename: table1 Source System Name: source1, Count: 4
Tablename: table1 Source System Name: source2, Count: 9
Tablename: table1 Source System Name: source3, Count: 1
Tablename: table1 Source System Name: source4, Count: 88
Tablename: table1 Source System Name: source5, Count: 15
Tablename: table1 Source System Name: source6, Count: 2
Tablename: table1 Source System Name: source7, Count: 1521
Tablename: table1 Source System Name: source8, Count: 1288
Tablename: table1 Source System Name: source9, Count: 24569
Exception in thread "main" org.postgresql.util.PSQLException: Cannot commit when autoCommit is enabled.
    at org.postgresql.jdbc.PgConnection.commit(PgConnection.java:756)
    at com.recordcount.dao.GpHiveRecords.getGpTableCount(GpHiveRecords.java:82)
    at com.recordcount.entry.StartCount.main(StartCount.java:12)

我知道我错误地使用了连接,但我可以识别错误。 任何人都可以让我知道我在这里做的错误是什么。

0 个答案:

没有答案