java - 通过连接防止 Oracle 数据库错误
问题描述
我有一些麻烦,但没有必要。
因为我JSP/Servlet
的工作正常但有时我有这些连接错误像这样
连接错误记录:
ORA-02396: exceeded maximum idle time, please connect again ORA-01012: not logged on ORA-00604: error occurred at recursive SQL level 1 ORA-02399: exceed maximum connect time, you are being logged off
.jsp
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="my.OracleConnectionPool" %>
<%@ page pageEncoding="UTF-8"%>
<%
Connection connect = null;
Statement s = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connect = OracleConnectionPool.getConnection();
s = connect.createStatement();
String sql = "SELECT * FROM table";
ResultSet rec = s.executeQuery(sql);
//Do something
} catch (Exception e) {
out.println(e.getMessage());
e.printStackTrace();
} finally {
try {
s.close();
connect.close();
} catch (SQLException ex) {
out.println(e.getMessage());
e.printStackTrace();
}
}
%>
my.OracleConnectionPool这将从wildfly
package my;
import javax.naming.NamingException;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class OracleConnectionPool {
public OracleConnectionPool() {
}
public static Connection getConnection()
throws SQLException {
try {
Context ctx = new InitialContext();
DataSource ods = null;
ods = (DataSource)ctx.lookup("java:/MYCON");
if (ods == null) {
throw new SQLException("OracleDataSource is null.");
}
return ods.getConnection();
} catch (NamingException ex) {
return null;
}
}
}
对于 Standalone.xml
<datasource enabled="true" pool-name="MYCON" jndi-name="java:/MYCON" use-ccm="false" jta="true">
<connection-url>jdbc:oracle:thin:@myhost:1521:ODS</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<driver>ojdbc6.jar</driver>
<security>
<user-name>myuser</user-name>
<password>********</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
<background-validation>true</background-validation>
<stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
</validation>
</datasource>
如何防止或跳过这些错误?我应该先尝试刷新 servlet/connect 测试吗?
更新:因为我无法更改 Oracle 的 PDA
解决方案
您的数据库配置文件可能有问题。即使您可以解决连接池设置的问题,您仍应尝试在数据库端解决此问题。如果您看到此问题一次,它很可能会影响其他用户和其他可能没有解决方法的工具。
首先,找出适用于您的用户的配置文件:
select username, profile
from dba_users
order by username;
发生这些问题的最常见方式是用户位于错误的配置文件中。确保您的应用程序帐户没有卡在用户配置文件中。DBA 通常设置至少两种配置文件——一种用于用户帐户,强制断开连接,另一种用于应用程序帐户,从不强制断开连接。
如果用户在正确的配置文件中,请检查配置文件值。您的应用程序用户应该UNLIMITED
对资源CONNECT_TIME
和IDLE_TIME
.
select profile, resource_name, limit
from dba_profiles
where resource_name in ('IDLE_TIME', 'CONNECT_TIME')
order by 1,2;
如果您的应用程序配置文件设置为一个较小的值,您可能需要运行如下命令:
alter profile your_profile limit connect_time unlimited;
推荐阅读
- c# - 当我保存一个对象时,NHibernate 尝试将对象保存在 id=0 的数据库中
- python - 如何删除文件的特定最后两行?
- go - 如何在golang中将切片“附加”或“复制”到二维切片?
- javascript - Firebase 函数:根据数组元素发送多个通知
- javascript - TypeError:无法将属性“src”设置为 null
- google-cloud-platform - 没有云存储的 Firebase 云功能
- pygame - 如何在pygame中制作圆形表面
- php - 在 laravel 中过滤关系
- cordova - 科尔多瓦 paytm 支付网关插件中的 501 错误代码
- python - 为什么我的 Python 函数没有返回任何值?