【问题标题】:PSQLException: This connection has been closed using HashMap passed from DAO layerPSQLException: 此连接已使用从 DAO 层传递的 HashMap 关闭
【发布时间】:2018-06-12 06:17:53
【问题描述】:

我有一种不好的感觉,我需要重构一些东西。本质上,我需要在 DAO 层从 PostgreSQL 获取一个数组,然后将该 JDBC 数组转换为 String[] 或 Integer[] 数组。

我的困惑是我认为我没有引用数据库连接或 ResultSet,所以我不知道为什么会收到此异常?:

例外

当我尝试将 jdbc Array 转换为 Object[] 时,会在下面抛出此异常。

org.postgresql.util.PSQLException: This connection has been closed.

DAOImpl

public HashMap<String, Object> getUser(Integer id) {
    try {
        conn = DBConnectionFactory.getDatabaseConnection("postgresql");
        ps = conn.prepareStatement(query);
        rs = ps.executeQuery();

        map = generateMapFromResultSet(rs);
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
    finally {
        try {
            if(conn != null) {
                conn.close();
            }
            if(ps != null) {
                ps.close();
            }
            if(rs != null) {
                rs.close();
            }
        } 
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
...
}

private HashMap<String, Object> generateMapFromResultSet(ResultSet rs) {

    HashMap<String, Object> returnMap = null;
    ResultSetMetaData rsmd = null; 
    Integer columns = null;

    try {
        rsmd = rs.getMetaData();
        columns = rsmd.getColumnCount();

        while(rs.next()) {
            returnMap = new HashMap<String,Object>(columns);

            for(int index = 1; index <= columns; index++) {
                returnMap.put(rsmd.getColumnName(index), rs.getObject(index));
            }
        }
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }

    return returnMap;               
}

服务层

这是采用 HashMap、迭代 HashMap、在 DAO 中执行比我想要的更多的逻辑并生成用户对象的层。

private User convertMapToUser(HashMap<String,Object> userMap) throws IllegalArgumentException {

    ...

    Iterator iterator = userMap.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry pair = (Map.Entry)iterator.next();

        String columnName = (String)pair.getKey();

        switch(columnName) {
            case COLUMN1:
                try {
                    Array col1_array = (Array)pair.getValue();
                    new_col1_array = (Integer[])col1_array.getArray(); //ERROR HERE!
                }
                catch(Exception ex) {
                    ex.printStackTrace();
                }

                break;
            case COLUMN2:
                try {
                    Array col2_array = (Array)pair.getValue();
                    new_col2_array = (Integer[])col2_array.getArray(); //ERROR HERE!
                }
                catch(Exception ex) {
                    ex.printStackTrace();
                }

                break;
            default:
                ...
        }

        // avoids a ConcurrentModificationException
        iterator.remove();
    }

    ...
}

【问题讨论】:

    标签: java postgresql


    【解决方案1】:

    在 DAO 层中有 getArray 方法。

    Object obj = rs.getObject(index);
    if (obj instanceof Array)
    {
       obj = ((Array) obj).getArray();
    }
    returnMap.put(rsmd.getColumnName(index), obj);
    

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 2015-04-25
      • 2018-08-16
      • 2018-09-17
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      相关资源
      最近更新 更多