title: Hibernate: Cannot open connection
body: <p>I am getting message cannot open connection,nothing specific log is being generated too</p>
  
  <p><a href="http://i.stack.imgur.com/0zjGK.png" rel="nofollow">connection cannot open</a></p>
  
  <p>and my hibernate.cfg.xml is:-</p>
  
  <pre><code>&lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  
      &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/hibernate_oar&lt;/property&gt;
      &lt;property name="hibernate.connection.user"&gt; root&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;dinga&lt;/property&gt;
  
      &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
      &lt;property name="hibernate.hbm2ddl.auto"&gt;create&lt;/property&gt;
      &lt;property name="use_sql_comments"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.validator.apply_to_ddl"&gt;false&lt;/property&gt;
      &lt;property name="hibernate.validator.autoregister_listeners"&gt;false&lt;/property&gt;
      &lt;property name="minPoolSize"&gt; 5&lt;/property&gt;
      &lt;property name="maxPoolSize"&gt;100&lt;/property&gt;
  
      &lt;property name="initialPoolSize"&gt;5&lt;/property&gt;
      &lt;property name="validateConnectionOnBorrow"&gt;true&lt;/property&gt;
      &lt;property name="maxStatements"&gt;10&lt;/property&gt;
  
      &lt;mapping class="com.model.Student" /&gt;
  &lt;/session-factory&gt;
  </code></pre>
  
  <p></p>
  
  <p>I am generating session</p>
  
  <pre><code>public class DataAccessObject {
  private static Session session = null;
  
      public static Session getSession() {
  
          AnnotationConfiguration cfg = new AnnotationConfiguration().configure();
          //cfg.addAnnotatedClass(hello.Message.class);
          SessionFactory annotedSessionFactory = cfg.buildSessionFactory();
          Session session = annotedSessionFactory.openSession();
          return session;
      }
  }
  </code></pre>
  
  <p>and I am using session to retrieve data from mysql database</p>
  
  <pre><code>public class CrudDao {
  
  private Session session;
  public CrudDao() {
      session = DataAccessObject.getSession();
  }
  
  public List&lt;Student&gt; getAllStudents() {
  
      Transaction tx = null;
      Query query = null;
  
      List&lt;Student&gt; students = new ArrayList();
      String myquery = "select s.studentId,s.name,s.department,s.emailId,s.status from Student s";
      try {
          query = session.createQuery(myquery);
  
          List&lt;Student&gt; list = query.list();
          System.out.println(list);
          Iterator&lt;Student&gt; itr = list.iterator();
          Student newstudent = new Student();
          while (itr.hasNext()) {
              Student mystudent = itr.next();
              newstudent.setStudentId(mystudent.getStudentId());
  
              newstudent.setName(mystudent.getName());
              newstudent.setDepartment(mystudent.getDepartment());
              newstudent.setEmailId(mystudent.getEmailId());
              newstudent.setStatus(mystudent.getStatus());
              students.add(newstudent);
              System.out.println(students);
          }
      } catch (HibernateException e) {
          System.err.println(e.getMessage());
      } finally {
          session.close();
      }
      return students;
  }
  }
  </code></pre>