title: Prevent Oracle Database Error by Connection
body: <p>I have some trouble but not necessary.</p>
  <p>Because my <code>JSP/Servlet</code> is work fine but <strong>sometime</strong> I have these <strong>connection Error</strong> Like this</p>
  <blockquote>
  <p><strong>Connection Error Records:</strong></p>
  <pre><code>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
  </code></pre>
  </blockquote>
  <p><strong>.jsp</strong></p>
  <pre><code>&lt;%@ page import=&quot;java.sql.ResultSet&quot; %&gt;
  &lt;%@ page import=&quot;java.sql.SQLException&quot; %&gt;
  &lt;%@ page import=&quot;java.sql.Statement&quot; %&gt;
  &lt;%@ page import=&quot;java.sql.Connection&quot; %&gt;
  &lt;%@ page import=&quot;java.sql.DriverManager&quot; %&gt;
  &lt;%@ page import=&quot;my.OracleConnectionPool&quot; %&gt;
  &lt;%@ page pageEncoding=&quot;UTF-8&quot;%&gt;
  &lt;% 
  Connection connect = null;
  Statement s = null;
  try {        
     Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;);
     connect =  OracleConnectionPool.getConnection();
     s = connect.createStatement();
     String sql = &quot;SELECT * FROM table&quot;;
     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();
     }
  }
  %&gt;
  </code></pre>
  <p><strong>my.OracleConnectionPool</strong> this will get connection from <code>wildfly</code></p>
  <pre class="lang-java prettyprint-override"><code>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(&quot;java:/MYCON&quot;);
        if (ods == null) {
          throw new SQLException(&quot;OracleDataSource is null.&quot;);
        }
        return ods.getConnection();
      } catch (NamingException ex) {
        return null;
      }
    }
  }
  </code></pre>
  <p>For the Standalone.xml</p>
  <pre><code>&lt;datasource enabled=&quot;true&quot; pool-name=&quot;MYCON&quot; jndi-name=&quot;java:/MYCON&quot; use-ccm=&quot;false&quot; jta=&quot;true&quot;&gt;
     &lt;connection-url&gt;jdbc:oracle:thin:@myhost:1521:ODS&lt;/connection-url&gt;
     &lt;driver-class&gt;oracle.jdbc.OracleDriver&lt;/driver-class&gt;
     &lt;driver&gt;ojdbc6.jar&lt;/driver&gt;
     &lt;security&gt;
        &lt;user-name&gt;myuser&lt;/user-name&gt;
        &lt;password&gt;********&lt;/password&gt;
     &lt;/security&gt;
     &lt;validation&gt;
        &lt;valid-connection-checker class-name=&quot;org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker&quot;/&gt;
        &lt;background-validation&gt;true&lt;/background-validation&gt;
        &lt;stale-connection-checker class-name=&quot;org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker&quot;/&gt;
        &lt;exception-sorter class-name=&quot;org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter&quot;/&gt;
     &lt;/validation&gt;
  &lt;/datasource&gt;
  </code></pre>
  <p>How can I prevent or skip these error?
  Should I try the refresh servlet/connect test first?</p>
  <p><strong>Update:</strong> Because I can't change Oracle's PDA</p>