Weblogic常见故障常:JDBC Connection Pools

简介:
WebLogic Server中数据库连接池是一个经常出问题的地方,总结一下出问题的原因和解决办法。

一、数据库连接泄漏
此类问题一般都是由于开发人员没有正确关闭数据库连接造成的。比如使用完Connection后,没有调用Connection.close()方法。

1、诊断方法
在Console中,找到Connection Pools Tab 和Diagnostics,设置以下属性(不同版本可能略有区别)
Enable Connection Leak Profiling 启用连接池泄漏的监控。
Enable Connection Profiling 启用连接池监控。
Inactive Connection Timeout 100 表示100秒后强制回收无效连接。默认0,表示使用完才释放回连接池。
无需重启,查看server的log,查找“A JDBC pool connection leak was detected”,如果有,看看是哪个类引起的。下面是一个数据库连接泄漏的例子:
A JDBC pool connection leak was detected.
A connection leak occurs when a connection obtained from the pool was not closed explicitly by 
calling close() and then was disposed by the garbage collector and returned to the connection pool.
The following stack trace at create shows where the leaked connection was created.
Stack trace at connection create:
at weblogic.jdbc.wrapper.JTAConnection.init(JTAConnection.java:90)
at weblogic.jdbc.jta.DataSource.getConnection(DataSource.java:468)
at weblogic.jdbc.jta.DataSource.connect(DataSource.java:410)
at weblogic.jdbc.common.internal.RmiDataSource.getConnection(RmiDataSource.java:344)
at troubleshooting.servlets.JdbcConnections.service(JdbcConnections.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1077)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:465)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:348)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:7047)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3902)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2773)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)
问题解决后,把三个属性设置回先前的值。
2、解决方法
正确的关闭数据库连接。具体代码如下:
Connection conn = null;
ResultSet rs = null;
preparedStatement pss = null;
try {
conn = dataSource.getConnection(USERID,pASSWORD);
pss = conn.prepareStatement("SELECT SAVESERIALZEDDATA FROM SESSION.pINGSESSION3DATA WHERE 
SESSIONKEY = ?");
pss.setString(1,sessionKey);
rs = pss.executeQuery();
pss.close();
}
catch (Throwable t) {} 
finally {
try 
{
if (conn != null) conn.close();

catch (Exception e){}
}

二、数据库连接不够用

导致数据库连接不够用的原因主要有:
①某些程序占用connection时间过长,如果多个用户同时使用这些程序,则会导致连接不够用。
②线程死锁,无法释放connection。
1、诊断方法
①监控参数:Waiting For Connection High Count
[domain_name]-> Enviroment -> Servers -> [Server] -> Monitoring -> JDBC查看参数:Waiting For Connection High Count
如果没有此参数,手工添加进来,该参数表示在没有可用连接的情况下,应用程序等待连接的最大个数。调整后的连接池最大值 = 调整前的连接池最大值 + Waiting For Connection High Count。一般来说,数据库连接池的大小与最佳并发用户数相当。

②在Server Log中,明确抛出下列异常:
java.sql.SQLException: Internal error: Cannot obtain XAConnection 
weblogic.common.resourcepool.ResourceLimitException: No resources currently available in pool 
BankConnectionPool to allocate to applications, please increase the size of the pool and retry..
at weblogic.jdbc.jta.DataSource.refreshXAConnAndEnlist(DataSource.java:1493)
at weblogic.jdbc.jta.DataSource.getConnection(DataSource.java:455)
at weblogic.jdbc.jta.DataSource.connect(DataSource.java:410)
at weblogic.jdbc.common.internal.RmiDataSource.getConnection(RmiDataSource.java:344)
at troubleshooting.servlets.JdbcConnections.service(JdbcConnections.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1077)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:465)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:348)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:7047)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3902)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2773)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)
如果此时观察connection的监控,会发现所有connection 都是Active,而且还有大量请求等待connection。
2、解决方法
①提高Maximum Capacity数量,该值一般略大于峰值情况下的数据库连接数。Services > JDBC > Connection Pools > BankConnectionPool > Configuration > Connections
②重点检查synchronize代码段和涉及数据库锁的代码。如果有必要,可以查看thread dump,看看线程在忙什么和等什么。

三、数据库连接使用超时
此类问题一般是由于某些数据库操作时间比较长,超过了Inactive connection timeout的设置。
1、诊断方法
在Server Log中,明确有下列提示,并且在提示后抛出应用异常:
Forcibly releasing inactive resource "weblogic.jdbc.common.internal.ConnectionEnv@132967d" back into the pool BankConnectionPool".这里无法列出应用异常,因为每个应用都不一样,不过很有可能会抛出空指针异常,因为Connection被强制放回池中了,继续使用一个空对象会抛出该异常。
2、解决方法
在高级参数中,提高Inactive connection timeout数量。
Services > JDBC > Connection Pools > BankConnectionPool > Configuration > Connections

四、事务超时
此类问题一般是由于某些数据库操作时间比较长,超过了JTA Timeout Seconds的设置。
1、诊断方法
在Server Log中,明确抛出异常:
weblogic.transaction.internal.TimedOutException: Transaction timed out after 300 seconds
2、解决方法
提高Services > JTA Configuration > Timeout Seconds数量。
注意这个参数应该小于Inactive connection timeout的值,因为事务必须在连接超时前完成。如果想分析究竟是哪些SQL语句导致事务超时,可以打开日志AdminServer > Logging > JDBC,选中Enable JDBC Logging,并设置JDBC Log File Name。

原帖地址: http://maping930883.blogspot.com/2009/03/wls040jdbc-connection.html

目录
相关文章
|
6月前
|
SQL Java 关系型数据库
JDBC Connection详解:连接到数据库的关键
在Java中,要与数据库进行交互,需要使用Java数据库连接(JDBC)。JDBC允许您连接到不同类型的数据库,并执行SQL查询、插入、更新和删除操作。在JDBC中,连接数据库是一个重要的步骤,而Connection对象是实现这一目标的关键。本篇博客将详细解释Connection对象的作用、创建和使用方法,以及与数据库连接相关的注意事项。
79 0
|
10月前
|
Java 关系型数据库 MySQL
|
Java 关系型数据库 MySQL
jdbc连接数据库报错Connection连接错误
错误代码示例: @Test public void test1() throws ClassNotFoundException, SQLException { //1:注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2:获取链接 String url = "jdbc:mysql://localhost:3308/spj"; String user = "root"; .
199 0
jdbc连接数据库报错Connection连接错误
|
SQL Oracle Java
Jmeter系列(29)- 详解 JDBC Connection Configuration
Jmeter系列(29)- 详解 JDBC Connection Configuration
271 0
Jmeter系列(29)- 详解 JDBC Connection Configuration
|
Java 关系型数据库 MySQL
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection 问题处理
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection 问题处理
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection 问题处理
|
SQL 安全 网络协议
|
Java 数据库连接 数据库
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is jav
数据库连接超时,是数据库连接时的相关配置写错,例如:数据库密码,驱动等问题
3151 0
|
Java 数据库连接 数据库
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is jav
数据库连接超时,是数据库连接时的相关配置写错,例如:数据库密码,驱动等问题
2038 0
|
运维 安全 应用服务中间件