title: Database Connection Timeout Tomcat7
body: <p><strong>Where can I set timeout for getting database connection:</strong> <code>javax.sql.DataSource.getConnection()</code> <strong>in tomcat 7 application server ?</strong></p>
  
  <pre><code>Context initContext = new InitialContext();
  Context envContext = (Context) initContext.lookup(DBManagerConstants.CONTEXT_NAME);
  DataSource ds = (DataSource) envContext.lookup(DBManagerConstants.DS_NAME);
  conn = ds.getConnection();
  </code></pre>
  
  <p>I have java web application on Tomcat 7 which connection to Oracle database. One day our Net Admin changed something in Firewall after this application was unable to get database connection but still there was no timeout and error. After removing this change(after 1,5 hours) in Firewall application continued to work as if nothing happened. These delayed connections finished all operations successfully. </p>
  
  <p>This is my datasource</p>
  
  <pre><code>&lt;Context&gt;
       &lt;Resource
          name="jdbc/pds"
          auth="Container"
          driverClassName="oracle.jdbc.driver.OracleDriver"
          factory="our.encryptor.EncryptedDataSourceFactory"
          logAbandoned="true"
          maxActive="30"
          maxIdle="10"
          maxWait="1000"
          password="my_password"
          removeAbandoned="true"
          removeAbandonedTimeout="60"
          type="javax.sql.DataSource"
          url="jdbc:oracle:thin:@127.0.0.1:8080:SID"
          username="my_user"
          accessToUnderlyingConnectionAllowed="true"
      validationQuery="select 1 from dual" /&gt;
  &lt;/Context&gt;
  </code></pre>