简体   繁体   English

显示数据库中无效连接的连接池

[英]Connection pooling showing INACTIVE connections in database

I am using connection pooling with SharedPoolDataSource in struts application. 我在Struts应用程序中使用SharedPoolDataSource连接池。 Application user pass the Connection parameters dynamically through user interface. 应用程序用户通过用户界面动态传递连接参数。 I am creating object of DriverAdapterCPDS and assigning it to SharedPoolDataSource obj props. 我正在创建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);

I am storing it in the hashtable of DBUtil class with company name as key. 我将其存储在DBUtil类的哈希表中,并且公司名称为键。 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. 用户从另一个程序(Swing App)发送公司名称。 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 我在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);
    }

Is there any problem of using the connection pooling? 使用连接池是否有任何问题? After using this application database showing lot of INACTIVE connections associated to my computer. 使用此应用程序数据库后,显示出许多与我的计算机关联的INACTIVE连接。 Even after closing the connection it is showing like that. 即使在关闭连接后,它仍会显示为那样。 I set only 2 MAXIDLE connections for the database. 我只为数据库设置了2个MAXIDLE连接。

What is the problem? 问题是什么? Why it is showing that much of INACTIVE connections? 为什么它显示了很多不活动的连接? Is there any problem if we put datasource in hashtable? 如果将数据源放在哈希表中有什么问题吗?

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 检查http://asktom.oracle.com/pls/apex/f?p=100:11:0:::::P11_QUESTION_ID:914029001168

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM