title: Trying to set up Tomcat and JDBC connection pooling: Communications Link Failure
body: <p>Full error message from the tomcat window:</p>
  
  <pre><code>[localhost-startStop-1] ERROR org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator - HHH000342: Could not obtain connection to query metadata: Cannot create PoolableConnectionFactory (Communications link failure)
  </code></pre>
  
  <p>Basically every time I start up tomcat it gets a communications link failure when trying to form a jdbc connection with my mysql database on a remote linux server, (on a private network that I must VPN into) and I can't figure out why.</p>
  
  <p>Can anyone tell me why I'm getting this error and how I could go about solving it. As always, thanks for the time and assistance!</p>
  
  <p><strong>Steps Already Taken</strong> </p>
  
  <p>. Configured MySQL to allow remote connections to the database. I've tried setting the bind-address in my.cnf to 0.0.0.0 and the IP of the server that the database is hosted on, but in both cases the same error. I've added a new user and used the GRANT ALL PRIVILEGES command to give that user remote access to the database from 127.0.0.1 and from the IP of the server that the database is hosted on. </p>
  
  <p>. Tried completely deactivating my firewall, to no effect </p>
  
  <p>. From intellij on my local computer I've tried forming a JDBC connection to the database using the set up user and password in the persistence menu. This works when I am already VPNed into the private network, and I am able to see the database just fine.</p>
  
  <p>. Updated the <strong>server.xml</strong> with this Resource tag in my tomcat folder: </p>
  
  <pre><code>&lt;Resource name="jdbc/mydb"
        global="jdbc/mydb"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://(IP-address of the database server):3306/DBname"
        username="user"
        password="password"
        maxActive="10"
        maxIdle="10"
        minIdle="5"
        maxWait="10000"/&gt;
  </code></pre>
  
  <p>. Updated the <strong>context.xml</strong> file in the tomcat folder with this resource link:</p>
  
  <pre><code>&lt;ResourceLink name="jdbc/mydb" 
         global="jdbc/mydb"
         auth="Container"
         type="javax.sql.DataSource" /&gt;
  </code></pre>
  
  <p>. Updated my <strong>hibernate.cfg.xml</strong> file as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration SYSTEM
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  
      &lt;!-- For Database Access --&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://(IP-address of database server):3306/DBname&lt;/property&gt;
      &lt;property name="connection.datasource"&gt;java:comp/env/jdbc/mydb&lt;/property&gt;
      &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
  
      &lt;!-- List of XML mapping files --&gt;
      &lt;mapping resource="db-full.hbm.xml"/&gt;
      &lt;mapping class="POJOs.Pojo1"/&gt;
      &lt;mapping class="POJOs.Pojo2"/&gt;
      &lt;mapping class="POJOs.Pojo3"/&gt;
      &lt;mapping class="POJOs.Pojo4"/&gt;
      &lt;mapping class="POJOs.Pojo5"/&gt;
      &lt;mapping class="POJOs.Pojo6"/&gt;
      &lt;mapping class="POJOs.Pojo7"/&gt;
  
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>. Updated my <strong>dispatcher-servlet.xml</strong> as follows:</p>
  
  <pre><code>    &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:beans="http://www.springframework.org/schema/beans"
                   xmlns:context="http://www.springframework.org/schema/context"
                   xmlns:tx="http://www.springframework.org/schema/tx"
                   xsi:schemaLocation="http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"&gt;
  
      &lt;annotation-driven /&gt;
      &lt;context:component-scan base-package="springmvc.controllers, data.service, data.DAO" /&gt;
  
      &lt;resources mapping="/resources/**" location="resources" /&gt;
  
      &lt;beans:bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"&gt;
          &lt;beans:property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;beans:property name="url" value="jdbc:mysql://(IP-address of database server):3306/DBname" /&gt;
          &lt;beans:property name="username" value="user" /&gt;
          &lt;beans:property name="password" value="password" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&gt;
          &lt;beans:property name="dataSource" ref="dataSource" /&gt;
          &lt;beans:property name="configLocation" value="WEB-INF/resources/hibernate.cfg.xml" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;tx:annotation-driven /&gt;
      &lt;beans:bean id="transactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager"&gt;
          &lt;beans:property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/beans:bean&gt;
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>. Updated my <strong>web.xml</strong> as follows:</p>
  
  <pre><code>&lt;web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd
       http://xmlns.jcp.org/xml/ns/javaee/web-common_3_1.xsd"
       version="3.1"&gt;
  
  &lt;display-name&gt;Web Application&lt;/display-name&gt;
  
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;WEB-INF/resources/application-context.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;springDispatcherServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;WEB-INF/springDispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;springDispatcherServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;!-- RESOURCE REF (TRIED RUNNING WITH AND WITHOUT) --&gt;
  &lt;!--&lt;resource-ref&gt;--&gt;
      &lt;!--&lt;description&gt;Connection Pool&lt;/description&gt;--&gt;
      &lt;!--&lt;res-ref-name&gt;jdbc/mydb&lt;/res-ref-name&gt;--&gt;
      &lt;!--&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;--&gt;
      &lt;!--&lt;res-auth&gt;Container&lt;/res-auth&gt;--&gt;
  &lt;!--&lt;/resource-ref&gt;--&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>. Updated <strong>pom.xml</strong> as follows:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.project.procect-name&lt;/groupId&gt;
      &lt;artifactId&gt;ProjectName&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;Maven Webapp&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
      &lt;properties&gt;
          &lt;org.springframework.version&gt;4.3.2.RELEASE&lt;/org.springframework.version&gt;
          &lt;hibernate.version&gt;5.2.1.Final&lt;/hibernate.version&gt;
          &lt;mysql.connector.version&gt;5.5.49&lt;/mysql.connector.version&gt;
          &lt;hibernate.version&gt;5.2.1.Final&lt;/hibernate.version&gt;
          &lt;mysql.connector.version&gt;5.5.50&lt;/mysql.connector.version&gt;
  
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;!-- JDBC Driver --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;5.1.35&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;3.8.1&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
              &lt;version&gt;3.1.0&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
              &lt;version&gt;1.10.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Hibernate --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;5.1.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-jpa-2.0-api&lt;/artifactId&gt;
              &lt;version&gt;1.0.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;5.1.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.transaction&lt;/groupId&gt;
              &lt;artifactId&gt;jta&lt;/artifactId&gt;
              &lt;version&gt;1.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- HSQL Database --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
              &lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
              &lt;version&gt;2.3.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- JSON --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.json&lt;/groupId&gt;
              &lt;artifactId&gt;json&lt;/artifactId&gt;
              &lt;version&gt;20160810&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Jackson To Convert java to JSON --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
              &lt;version&gt;2.8.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Application View --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;jstl&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Apache Commons DBCP (Connection Pooling) --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-dbcp2&lt;/artifactId&gt;
              &lt;version&gt;2.0&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Log4J --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
              &lt;version&gt;1.2.17&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;finalName&gt;ROOT&lt;/finalName&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  
                  &lt;version&gt;2.5.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.8&lt;/source&gt;
                      &lt;target&gt;1.8&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;webXml&gt;src\main\webapp\WEB-INF\web.xml&lt;/webXml&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>