简体   繁体   English

通过连接防止 Oracle 数据库错误

[英]Prevent Oracle Database Error by Connection

I have some trouble but not necessary.我有一些麻烦但没有必要。

Because my JSP/Servlet is work fine but sometime I have these connection Error Like this因为我的JSP/Servlet工作正常,但有时我有这些连接错误像这样

Connection Error Records:连接错误记录:

 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 .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 this will get connection from wildfly 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;
    }
  }
}

For the Standalone.xml对于 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>

How can I prevent or skip these error?如何防止或跳过这些错误? Should I try the refresh servlet/connect test first?我应该先尝试刷新 servlet/connect 测试吗?

Update: Because I can't change Oracle's PDA更新:因为我无法更改 Oracle 的 PDA

There might be a problem with your database profiles.您的数据库配置文件可能有问题。 Even if you can workaround the problem with connection pool settings, you should still try to fix this on the database side.即使您可以解决连接池设置的问题,您仍然应该尝试在数据库端解决此问题。 If you see this problem once, chances are it's impacting other users and other tools that may not have a workaround.如果您曾经看到过这个问题,很可能它会影响其他用户和其他可能没有解决方法的工具。

First, find out which profile applies to your user:首先,找出适用于您的用户的配置文件:

select username, profile
from dba_users
order by username;

The most common way for these problems to occurs is for users to be in the wrong profile.发生这些问题的最常见方式是用户处于错误的配置文件中。 Make sure your application account is not stuck in a user profile.确保您的应用程序帐户未卡在用户配置文件中。 DBAs typically set up at least two kinds of profiles - one for user accounts, that forces disconnects, and one for application accounts, that never forces disconnects. DBA 通常设置至少两种配置文件 - 一种用于用户帐户,强制断开连接,另一种用于应用程序帐户,从不强制断开连接。

If the user is in the right profile, check the profile values.如果用户在正确的配置文件中,请检查配置文件值。 Your application user should probably have a value of UNLIMITED for the resources CONNECT_TIME and IDLE_TIME .对于资源CONNECT_TIMEIDLE_TIME您的应用程序用户可能应该具有UNLIMITED值。

select profile, resource_name, limit
from dba_profiles
where resource_name in ('IDLE_TIME', 'CONNECT_TIME')
order by 1,2;

If your application profile is set to a small value, you may need to run a command like this:如果您的应用程序配置文件设置为较小的值,您可能需要运行如下命令:

alter profile your_profile limit connect_time unlimited;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM