title: Could not open Hibernate Session for transaction : org.hibernate.exception.GenericJDBCException
body: <p>I have a spring project where for that i am using mysql as the database.I have configured the Tomcat JDBC connection pool in servelet configuration file and the connection is working fine in my local machine.But recently I have recreated our remote server and there after am getting below error when i am trying to access database.</p>
  
  <pre><code>{"error":"unauthorized","error_description":"Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection"}curl: (6) Could not resolve host: application
  </code></pre>
  
  <p>So what will be the reason for this error.The same project have no issues earlier and the issue comes after recreating the server.I can access the remote server mysql database from phpmyadmin using the same username and password.Also no problem when we run the project within local machine and local mysql database.
  Below is my spring servlet configuration file.</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"&gt;
      &lt;!-- @author Nagesh.Chauhan(neel4soft@gmail.com) --&gt;
          &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com.example.myproject" &gt;
      &lt;context:exclude-filter type="assignable" expression="com.example.myproject.controller.MyController"/&gt;
      &lt;/context:component-scan&gt;
      &lt;mvc:annotation-driven&gt;
          &lt;mvc:message-converters register-defaults="false"&gt;
              &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter" /&gt;
              &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /&gt;
          &lt;/mvc:message-converters&gt;
      &lt;/mvc:annotation-driven&gt;    
           &lt;!--  in above  &lt;bean class="org.springframework.http.converter.FormHttpMessageConverter" /&gt;
              &lt;bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /&gt;    --&gt;
  
      &lt;bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;  
  
      &lt;!-- one of the properties available; the maximum file size in bytes --&gt;
       &lt;property name="maxUploadSize" value="1000000000" /&gt;
      &lt;/bean&gt;  
  
      &lt;bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/SampleDb"/&gt;
      &lt;property name="username" value="root"/&gt;
      &lt;property name="password" value="mypassword"/&gt;
      &lt;property name="validationQuery" value="SELECT 1"/&gt;
    &lt;/bean&gt;
  
    &lt;!-- Hibernate Session Factory --&gt;
    &lt;bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="myDataSource"/&gt;
      &lt;property name="packagesToScan"&gt;
        &lt;array&gt;
          &lt;value&gt;com.example.myproject&lt;/value&gt;
        &lt;/array&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
        &lt;value&gt;
          hibernate.dialect=org.hibernate.dialect.MySQLDialect
        &lt;/value&gt;
      &lt;/property&gt;
    &lt;/bean&gt;
    &lt;!-- Hibernate Transaction Manager --&gt;
    &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
    &lt;/bean&gt;
  
    &lt;!-- Activates annotation based transaction management --&gt;
     &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>This is how i am using the session in my service class :</p>
  
  <pre><code>    @Repository
  @Transactional
  public class MyMainService implements MainDao {
      @Autowired
      private SessionFactory sessionFactory;
  
      private org.hibernate.Session getCurrentSession(){
          return sessionFactory.getCurrentSession();
      }
  
      // just a sample
      @Override
      public void setUser(String idStr, Integer age) {
          UserSample newUserSam = new UserSample();
          try{
              newUserSam.setAge(age);
              newUserSam.setId(idStr);
              getCurrentSession().saveOrUpdate(newUserSam);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>Here is my pom.xml file :</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/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;MyProject&lt;/groupId&gt;
    &lt;artifactId&gt;MyProject&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;properties&gt;
      &lt;spring.version&gt;4.1.0.RELEASE&lt;/spring.version&gt;
      &lt;hibernate.version&gt;4.2.2.Final&lt;/hibernate.version&gt;
      &lt;!-- Spring Security OAuth2 --&gt;
      &lt;spring-security-oauth2-version&gt;2.0.8.RELEASE&lt;/spring-security-oauth2-version&gt;
    &lt;/properties&gt;
  
    &lt;dependencies&gt;
    &lt;!-- https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2 --&gt;
  &lt;!-- Spring Security OAuth2 --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security.oauth&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-oauth2&lt;/artifactId&gt;
              &lt;version&gt;${spring-security-oauth2-version}&lt;/version&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;${spring.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;${spring.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;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
      &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
      &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
    &lt;version&gt;${spring.version}&lt;/version&gt;
    &lt;scope&gt;compile&lt;/scope&gt;
    &lt;exclusions&gt;
      &lt;exclusion&gt;
        &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
        &lt;groupId&gt;commons-logging&lt;/groupId&gt;
      &lt;/exclusion&gt;
    &lt;/exclusions&gt;
  &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework&lt;/groupId&gt;
        &lt;artifactId&gt;spring-oxm&lt;/artifactId&gt;
        &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
        &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
        &lt;version&gt;1.9.9&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.json&lt;/groupId&gt;
          &lt;artifactId&gt;json&lt;/artifactId&gt;
          &lt;version&gt;20070829&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;net.sf.json-lib&lt;/groupId&gt;
          &lt;artifactId&gt;json-lib&lt;/artifactId&gt;
          &lt;version&gt;2.4&lt;/version&gt;
          &lt;classifier&gt;jdk15&lt;/classifier&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
          &lt;artifactId&gt;gson&lt;/artifactId&gt;
          &lt;version&gt;2.2.4&lt;/version&gt;
      &lt;/dependency&gt;  
  
      &lt;!-- Security --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
              &lt;version&gt;3.2.9.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
     &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
              &lt;version&gt;3.2.9.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-core --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
      &lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
      &lt;version&gt;3.2.9.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  
          &lt;!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-crypto --&gt;
  &lt;!--    &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-crypto&lt;/artifactId&gt;
          &lt;version&gt;3.1.0.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;        --&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;${hibernate.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Java EE --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
        &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
        &lt;version&gt;2.5&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&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;!-- Others --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.swinglabs&lt;/groupId&gt;
          &lt;artifactId&gt;swingx&lt;/artifactId&gt;
          &lt;version&gt;0.9&lt;/version&gt;
        &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
        &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
        &lt;version&gt;1.4&lt;/version&gt;
      &lt;/dependency&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.25&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;log4j&lt;/groupId&gt;
        &lt;artifactId&gt;log4j&lt;/artifactId&gt;
        &lt;version&gt;1.2.14&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
      &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
      &lt;artifactId&gt;jsr311-api&lt;/artifactId&gt;
      &lt;version&gt;0.11&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
    &lt;artifactId&gt;httpclient&lt;/artifactId&gt;
    &lt;version&gt;4.3.5&lt;/version&gt;
  &lt;/dependency&gt;
      &lt;dependency&gt;
      &lt;groupId&gt;commons-httpclient&lt;/groupId&gt;
      &lt;artifactId&gt;commons-httpclient&lt;/artifactId&gt;
      &lt;version&gt;3.1&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.sun.xml.ws&lt;/groupId&gt;
      &lt;artifactId&gt;jaxws-rt&lt;/artifactId&gt;
      &lt;version&gt;2.1.4&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.apache.tiles&lt;/groupId&gt;
      &lt;artifactId&gt;tiles-extras&lt;/artifactId&gt;
      &lt;version&gt;3.0.5&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
      &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
      &lt;version&gt;1.3.2&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;commons-io&lt;/groupId&gt;
      &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
      &lt;version&gt;2.4&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;commons-fileupload&lt;/groupId&gt;
      &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt;
      &lt;version&gt;1.2&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fifesoft&lt;/groupId&gt;
      &lt;artifactId&gt;autocomplete&lt;/artifactId&gt;
      &lt;version&gt;1.5.0&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.swinglabs.swingx&lt;/groupId&gt;
      &lt;artifactId&gt;swingx-autocomplete&lt;/artifactId&gt;
      &lt;version&gt;1.6.3&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
          &lt;groupId&gt;net.java.dev.glazedlists&lt;/groupId&gt;
          &lt;artifactId&gt;glazedlists_java15&lt;/artifactId&gt;
          &lt;version&gt;1.8.0&lt;/version&gt;
          &lt;/dependency&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.7.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
      &lt;version&gt;2.7.2&lt;/version&gt;
  &lt;/dependency&gt;
          &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
      &lt;version&gt;2.7.2&lt;/version&gt;
  &lt;/dependency&gt;
  
    &lt;/dependencies&gt;
  
    &lt;build&gt;
      &lt;testSourceDirectory&gt;src/main/test&lt;/testSourceDirectory&gt;
      &lt;resources&gt;
        &lt;resource&gt;
          &lt;directory&gt;src/main/resources&lt;/directory&gt;
          &lt;excludes&gt;
            &lt;exclude&gt;**/*.java&lt;/exclude&gt;
          &lt;/excludes&gt;
        &lt;/resource&gt;
        &lt;resource&gt;
          &lt;directory&gt;src/main/webapp&lt;/directory&gt;
          &lt;excludes&gt;
            &lt;exclude&gt;**/*.java&lt;/exclude&gt;
          &lt;/excludes&gt;
        &lt;/resource&gt;
      &lt;/resources&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.3&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;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.6&lt;/version&gt;
          &lt;configuration&gt;
            &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>