首页 > 解决方案 > 通过连接防止 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

标签: javaoraclejsporacle11gwildfly

解决方案


您的数据库配置文件可能有问题。即使您可以解决连接池设置的问题,您仍应尝试在数据库端解决此问题。如果您看到此问题一次,它很可能会影响其他用户和其他可能没有解决方法的工具。

首先,找出适用于您的用户的配置文件:

select username, profile
from dba_users
order by username;

发生这些问题的最常见方式是用户位于错误的配置文件中。确保您的应用程序帐户没有卡在用户配置文件中。DBA 通常设置至少两种配置文件——一种用于用户帐户,强制断开连接,另一种用于应用程序帐户,从不强制断开连接。

如果用户在正确的配置文件中,请检查配置文件值。您的应用程序用户应该UNLIMITED对资源CONNECT_TIMEIDLE_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;

推荐阅读