池连接时出现Websphere 7.0.0.17错误

时间:2016-02-22 20:37:31

标签: java oracle jndi websphere-7

我遇到了应用程序的问题,我正在使用带有oracle 11g的EclipseLink 2.4.1,我收到了一个J2CA0045E错误,因为池没有从jndi中释放任何连接池,

你有这个问题吗?显然连接没有被释放,它达到我配置的限制。

任何想法,这是我的连接池

Connection timeout: 180
Maximum connections: 10
Minimum connections: 1
Reap time: 60
Unused timeout: 180
Aged timeout: 120
Purge policy: Entire Pool

修改

我在同一服务中有一个Web服务,行为是相同的,使用相同的jndi,连接增加并且不释放任何连接

initCtx = new InitialContext(); 
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/abc");
conn = ds.getConnection();

1 个答案:

答案 0 :(得分:0)

根据提供的代码,您可能会在类范围内存储Connection引用。您应该避免存储可能跨越多个请求的Connection个对象的引用。

而是存储对DataSource

的引用

举例说明。这种方法是首选:

public class GoodService {

    DataSource ds;

    public GoodService() throws NamingException {
        // Only the DataSource reference is stored, this is allowed
        ds = new InitialContext().lookup("java:comp/env/jdbc/abc");
    }

    public void doSomething() throws Exception {
        // Get the connection in a request.
        Connection conn = ds.getConnection();
        try {
            // do something...
        } finally {
            // Always close the connection in the finally block
            // so it gets returned to the pool no matter what
            conn.close();
        }
    }
}

以下方法被视为不良做法,可能会导致您看到的J2CA0045E错误:

public class BadService {

    Connection conn; // BAD: don't store a Connection across requests!

    public BadService() throws NamingException, SQLException {
        DataSource ds = new InitialContext().lookup("java:comp/env/jdbc/abc");
        // BAD: don't leave connections open indefinitely like this
        conn = ds.getConnection();
    }

    public void doSomething() throws Exception {
        // do something using conn...
    }
}