Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何确保连接在池中重用_Java_Sql Server_Websphere_Connection Pooling - Fatal编程技术网

Java 如何确保连接在池中重用

Java 如何确保连接在池中重用,java,sql-server,websphere,connection-pooling,Java,Sql Server,Websphere,Connection Pooling,我遇到一个问题,数据源没有重用连接,而是创建了一个新的连接。问题是它没有允许的连接 我正在使用Java、MSSQL和Websphere 以下是我如何打开连接: public static Connection open(boolean transaction) throws NamingException, SQLException { Context ctx; Connection con = null; ctx = new InitialContext();

我遇到一个问题,数据源没有重用连接,而是创建了一个新的连接。问题是它没有允许的连接

我正在使用Java、MSSQL和Websphere

以下是我如何打开连接:

public static Connection open(boolean transaction) throws NamingException, SQLException {
    Context ctx;
    Connection con = null;
    ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MYDB");
    if (ds == null) {
        throw new NamingException("No data source");
    }
    con = ds.getConnection();
    if (con == null) {
        throw new SQLException("No database connection");
    }else{
        con.setAutoCommit(!transaction);
    }
    return con;
}
在每个DAO函数的最后,我有一个关闭函数,它关闭所有的东西

public static void close(ResultSet rs, PreparedStatement ps, Connection con) throws SQLException{
    if (rs != null) {
        try { rs.close(); } catch (SQLException e) { throw e;}
    }
    if (ps != null) {
        try {ps.close(); } catch (SQLException e) { throw e;}
    }
    if (con != null) {
        if(con.getAutoCommit()){
            try { con.close(); } catch (SQLException e) { throw e;}
        }
    }
}
我使用的这些函数不是基于事务的连接。每次循环到达一个函数时,代码都会调用打开的连接,但不是使用已经建立的连接,而是创建一个新的连接。我正在监视在SQLServerManagementStudio的活动监视器中创建的连接

知道为什么不重用连接吗

编辑:以下是Websphere中的连接池属性:

解决方案: 所以,正如大多数情况下发生的那样,我是在打自己的脚。事实证明,上面的close函数是正确的,但它也在commit或rollback的Finally语句中被调用,它不会关闭连接。我更改了程序,在提交或回滚后将autocommit设置为true,以便它可以正确地关闭它。果不其然,这就是问题所在。
感谢您的帮助。

提供数据源的配置参数。另外,为什么在关闭连接之前会有条件?这似乎很奇怪。您使用什么类来配置数据源?从MySQL还是从Websphere?我想您只需要创建一次数据源。@BenMinton OP没有创建数据源,而是从JNDI检索的。