连接池显示数据库中的非活动连接

发布于 2024-12-03 15:35:24 字数 2160 浏览 5 评论 0原文

我在 struts 应用程序中使用带有 SharedPoolDataSource 的连接池。应用程序用户通过用户界面动态传递连接参数。我正在创建 DriverAdapterCPDS 对象并将其分配给 SharedPoolDataSource obj props。

SharedPoolDataSource tds = new SharedPoolDataSource();
            tds.setConnectionPoolDataSource(cpds);
            tds.setMaxActive(5);
            tds.setMaxIdle(2);
            tds.setMaxWait(50);
            tds.setTestOnBorrow(true);
            tds.setValidationQuery(validationQuery);

我将其存储在 DBUtil 类的哈希表中,以公司名称作为键。像这个应用程序一样,用户分配与每个公司相关的大量数据库详细信息。

dbUtil.DSht.put(comp, tds);

从另一个程序(Swing App)用户发送公司名称。因此,我需要获取与公司相关的数据源对象,并对分配给用户的数据库执行查询。并将结果作为对象返回。

所以我写了一个像这样的函数

public static Connection getClientConnection(String comp){
    Connection con = null;
    try{
        if(!dbUtil.DSht.contains(comp)){
            loadClientDataSource(comp);
        }
        DataSource ds = (DataSource)dbUtil.DSht.get(comp);            
        Idle:"+bds.getNumIdle());
        con = ds.getConnection();
    }catch(Exception e){
        e.printStackTrace();
        throw new DBUtilException("Unable to get the client connection object. Cause: "+e.getMessage());
    }        
    return con;
}

来关闭数据库连接

public static void closeConnection(Connection con){
    try{
        if(con != null)
            con.close();
    }catch(Exception e){
        e.printStackTrace();
    }        
}

我在finally块中调用这个closeConnection函数

try{
        con = DBUtil.getClientConnection(comp);
        pstmt = con.prepareStatement(sqlQuery);            
        pstmt.executeQuery();
    }catch(SQLException sqle) {
        sqle.printStackTrace();
        throw new QueryException("Unable to add query. Cause: "+sqle.getMessage());
    }finally{
        DBUtil.closeStatement(pstmt);
        DBUtil.closeConnection(con);
    }

使用连接池有什么问题吗?使用此应用程序数据库后,显示与我的计算机关联的大量INACTIVE连接。即使关闭连接后它还是这样显示。我只为数据库设置了 2 个 MAXIDLE 连接。

问题是什么?为什么它显示大量INACTIVE连接?如果我们把数据源放在哈希表中会有什么问题吗?

I am using connection pooling with SharedPoolDataSource in struts application. Application user pass the Connection parameters dynamically through user interface. I am creating object of DriverAdapterCPDS and assigning it to SharedPoolDataSource obj props.

SharedPoolDataSource tds = new SharedPoolDataSource();
            tds.setConnectionPoolDataSource(cpds);
            tds.setMaxActive(5);
            tds.setMaxIdle(2);
            tds.setMaxWait(50);
            tds.setTestOnBorrow(true);
            tds.setValidationQuery(validationQuery);

I am storing it in the hashtable of DBUtil class with company name as key. Like this app user assigns lot of database details associated with each company.

dbUtil.DSht.put(comp, tds);

From the another program (Swing App) user send company name. So that I need to get the datasource object respective to company and execute queries on that database which are assigned to the user. And return result as object.

So I wrote a function like this

public static Connection getClientConnection(String comp){
    Connection con = null;
    try{
        if(!dbUtil.DSht.contains(comp)){
            loadClientDataSource(comp);
        }
        DataSource ds = (DataSource)dbUtil.DSht.get(comp);            
        Idle:"+bds.getNumIdle());
        con = ds.getConnection();
    }catch(Exception e){
        e.printStackTrace();
        throw new DBUtilException("Unable to get the client connection object. Cause: "+e.getMessage());
    }        
    return con;
}

to close the database connection

public static void closeConnection(Connection con){
    try{
        if(con != null)
            con.close();
    }catch(Exception e){
        e.printStackTrace();
    }        
}

I am calling this closeConnection function in finally block

try{
        con = DBUtil.getClientConnection(comp);
        pstmt = con.prepareStatement(sqlQuery);            
        pstmt.executeQuery();
    }catch(SQLException sqle) {
        sqle.printStackTrace();
        throw new QueryException("Unable to add query. Cause: "+sqle.getMessage());
    }finally{
        DBUtil.closeStatement(pstmt);
        DBUtil.closeConnection(con);
    }

Is there any problem of using the connection pooling? After using this application database showing lot of INACTIVE connections associated to my computer. Even after closing the connection it is showing like that. I set only 2 MAXIDLE connections for the database.

What is the problem? Why it is showing that much of INACTIVE connections? Is there any problem if we put datasource in hashtable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-12-10 15:35:24

不用担心,会话处于 INACTIVE 状态并不是异常情况。检查 http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:914029001168

Do not worry about it, having session in INACTIVE state is not a anomaly. Check http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:914029001168

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文