Java close连接似乎无法正常工作

时间:2015-10-21 10:30:51

标签: java postgresql connection

我在storedProcedure使用PostgreSQL,我的整个SQL methods都是这样。

   private static final String HOP_DATA_GO = "{ ? =  call hop_go() }";
        public String hopData() {
            String result = "false";
            Connection conn = null;
            InitialContext itx = null;
            CallableStatement callableStatement = null;
            try {
                itx = new InitialContext();
                DataSource ods = (DataSource) itx.lookup(JNDI_NAME);
                conn = ods.getConnection();
                callableStatement = conn.prepareCall(HOP_DATA_GO);
                callableStatement.registerOutParameter(1, Types.VARCHAR);
                callableStatement.executeUpdate();
                result = callableStatement.getString(1);
            } catch (SQLException exception) {
                Logger.getLogger(HopImpl.class.getName()).log(Level.SEVERE, null, exception);
            } catch (NamingException e) {
                Logger.getLogger(HopImpl.class.getName()).log(Level.SEVERE, null, e);
            } finally {
                if (callableStatement != null) {
                    try {
                        callableStatement.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(HopImpl.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(HopImpl.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            return result;
  }

每次在方法结束时,我都会关闭连接。但是,当我从DataBase检查时:

SELECT * FROM pg_stat_database

有很多numbackendsconnections),我不知道他们来自哪里,他们为什么不关闭,或者那意味着什么? 我试试这个查询

SELECT * FROM pg_stat_activity 

这是query

的一行
backend_start: 2015-10-21 14:43:17.932223+05        
query_start: 2015-10-21 14:43:18.309034+05  
state_change: 2015-10-21 14:43:18.309246+05 
waiting: f  
state: idle         
query: SELECT nspname FROM pg_namespace

这是正常的吗?我如何更改代码?我犯错误(错误)的地方?

0 个答案:

没有答案