title: Connection close with Hibernate and JDBC
body: <p>I have following problem. I am writing a desktop java application with database on server. I also use hibernate. When I run application everything works fine and I have correct connection with database, but after few minutes when I do some operations on database I get following error:</p>
  
  <pre><code>Exception thrown: 
  Class: model.MainRepository
  
      Stack Trace:
      org.hibernate.TransactionException: JDBC begin transaction failed: 
          at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:72)
          at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:238)
          at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:213)
          at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)
          at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1443)
          at model.MainRepository.addTrainingToUser(MainRepository.java:151)
          at model.Parser.parseFilesWatch(Parser.java:85)
          at controller.UploadDataController$1.call(UploadDataController.java:148)
          at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
          at java.util.concurrent.FutureTask.run(FutureTask.java:266)
          at java.lang.Thread.run(Thread.java:745)
      Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  
      Last packet sent to the server was 11 ms ago.
          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
          at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
          at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
          at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
          at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2985)
          at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)
          at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)
          at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
          at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
          at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
          at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4874)
          at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:67)
          ... 10 more
      Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
          at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)
          at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2882)
          ... 17 more
  
      Exception thrown: 
      Class: model.MainRepository
      Stack Trace:
      org.hibernate.TransactionException: JDBC begin transaction failed: 
          at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:72)
          at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:238)
          at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:213)
          at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)
          at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1443)
          at model.MainRepository.sendTrainingDetails(MainRepository.java:184)
          at model.Parser.parseFilesWatch(Parser.java:94)
          at controller.UploadDataController$1.call(UploadDataController.java:148)
          at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
          at java.util.concurrent.FutureTask.run(FutureTask.java:266)
          at java.lang.Thread.run(Thread.java:745)
      Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
          at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
          at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
          at com.mysql.jdbc.Util.getInstance(Util.java:381)
          at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
          at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
          at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
          at com.mysql.jdbc.ConnectionImpl.getMutex(ConnectionImpl.java:3018)
          at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4827)
          at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:67)
          ... 10 more
  </code></pre>
  
  <p>Here is my hibernate.cfg.xml file:</p>
  
  <pre><code>&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;property name="connection.pool_size"&gt;1&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://sql.aaa.nazwa.pl:1111/aaaa&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;aaa&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;bbb&lt;/property&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;mapping resource="model/models.hbm.xml"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>HibernateUtil.java</p>
  
  <pre><code>package model;
  
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  
  /**
   * Created by Piotr on 2015-10-11.
   */
  public class HibernateUtil {
  
      private static final SessionFactory sessionFactory = buildSessionFactory();
  
      private static SessionFactory buildSessionFactory() {
          try {
              // Create the SessionFactory from hibernate.cfg.xml
              return new Configuration().configure().buildSessionFactory();
  
          } catch (Throwable ex) {
              // Make sure you log the exception, as it might be swallowed
              System.err.println("Initial SessionFactory creation failed." + ex);
              throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      public static void shutdown() {
          // Close caches and connection pools
          getSessionFactory().close();
      }
  }
  </code></pre>
  
  <p>And sample operation on database:</p>
  
  <pre><code>public void addUser(User user) {
          Session session = HibernateUtil.getSessionFactory().openSession();
          Transaction tx = null;
          try {
              tx = session.beginTransaction();
              session.save(user);
              tx.commit();
          } catch (Exception e) {
              if (tx != null) tx.rollback();
              Logger.saveToLogFile(e, this.getClass().getName());
          } finally {
              session.close();
          }
      }
  </code></pre>