title: Losing connection to MySQL after a while, and not reconnecting
body: <p>I'm developing a standalone server which uses JPA+Hibernate to access a MySQL database.</p>
  
  <p>When I start the server, everything is working fine. However, after a while (usually the next morning, if I start it in the afternoon) it will stop working because apparently the connection to MySQL was closed (I see lots of <code>SocketException</code>s in the logs). This is probably caused by idling, the server is in development and nobody uses it at night.</p>
  
  <p>I thought Hibernate, JDBC or some other layer below my app would manage the connection and reopen it if neccessary, but apparently not. Is there a configuration parameter I missed?</p>
  
  <p><strong><code>persistence.xml</code></strong></p>
  
  <p>http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0"></p>
  
  <pre><code>&lt;persistence-unit name="manager"&gt;
  
  &lt;class&gt;example.data.entities.User&lt;/class&gt;
  &lt;class&gt;example.data.entities.Player&lt;/class&gt;
  
  &lt;properties&gt;
      &lt;property name="hibernate.dialect" value="example.data.HibernateDialect" /&gt;
      &lt;property name="hibernate.max_fetch_depth" value="3" /&gt;
      &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
  
      &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /&gt;
  
  &lt;/properties&gt;
  
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong><code>EntityManagerFactory</code> creation</strong></p>
  
  <pre><code>    log.info("Connecting to database @ " + dbUrl + " using " + dbUser + "/" + dbPass);
  
      emf = Persistence.createEntityManagerFactory("manager", Maps.create(
              "javax.persistence.jdbc.user", dbUser,
              "javax.persistence.jdbc.password", dbPass,
              "javax.persistence.jdbc.url", dbUrl
      ));
  </code></pre>
  
  <p><strong>A query</strong></p>
  
  <pre><code>            try
              {
  
                  TypedQuery&lt;User&gt; q = em.createQuery("SELECT u FROM User u WHERE u.email = :mail", User.class);
                  q.setParameter("mail", email);
                  try {
                      u = q.getSingleResult();
                      log.info("Authenticating: " + u);
                  } catch (NoResultException e) {
                      return false;
                  }
  
              } finally {
                  em.close();
              }
  </code></pre>