测试环境:redhat + weblogic + MySQL
在weblogic中配置数据源,并且jndi设置为DataSource。
主要测试代码:
Context ctx;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("DataSource");
Connection conn = ds.getConnection();
Statement stmt = null;
ResultSet rs = null;
stmt = conn.createStatement();
String sql = "select * from test;";
rs = stmt.executeQuery(sql);
List<User> userList = new ArrayList<User>();
while(rs.next()){
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("name"));
User user = new User();
user.setName(rs.getString("name"));
user.setId(rs.getInt("id"));
userList.add(user);
}
request.setAttribute("userList",userList);
//conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
上面代码中连接打开后没有关闭,会导致连接泄露。
将测试项目部署至weblogic之后进行访问并重复刷新。在服务器的jdbc 监视界面观察到当前活动连接计数持续增加,达到最大活动连接计数后再访问测试代码就无法查询数据库中的数据了。
查看数据源的日志,错误信息如下:
rue,isXA=false,isJTS=false,vendorID=-1,connUsed=true,doInit=false,'null',destroyed=false,poolname=JDBC Data Source-0,appname=null,moduleName=null,connectTime=28,dirtyIsolationLevel=false,initialIsolationLevel=4,infected=false,lastSuccessfulConnectionUse=179129295880,secondsToTrustAnIdlePoolConnection=10,currentUser=java.lang.Exception
at weblogic.jdbc.common.internal.ConnectionEnv.setup(ConnectionEnv.java:352)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:364)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:330)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:487)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:380)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:132)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:165)
at weblogic.jdbc.pool.Driver.connect(Driver.java:136)
at weblogic.jdbc.jts.Driver.getNonTxConnection(Driver.java:662)
at weblogic.jdbc.jts.Driver.connect(Driver.java:129)
at weblogic.jdbc.common.internal.RmiDataSource.getConnectionInternal(RmiDataSource.java:553)
at weblogic.jdbc.common.internal.RmiDataSource.getConnection(RmiDataSource.java:518)
at weblogic.jdbc.common.internal.RmiDataSource.getConnection(RmiDataSource.java:511)
at TestEntityBean.doGet(TestEntityBean.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:338)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:221)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3284)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1513)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
根据 at TestEntityBean.doGet(TestEntityBean.java:49) 定位到项目中的代码,然后进行排查。