title: Could not open connection caused by This ResultSet is closed
body: <p>I am trying to make a web app using Maven Spring MVC JPA and Postgres. But I am still not able to persist an object in the database. I have an error while starting a transaction:</p>
  
  <pre><code>org.hibernate.exception.GenericJDBCException: Could not open connection
  caused by org.postgresql.util.PSQLException: This ResultSet is closed.
  </code></pre>
  
  <p>Here is the code</p>
  
  <pre><code>@Controller
  public class ExamController {
  
  @GetMapping("/hello")
  public String hello(Model model) {
  
      // EntityManager entityManager = 
  EntityManagerUtil.getEntityManager();
  
      try {
          EntityManager entityManager = 
              Persistence.createEntityManagerFactory("examinatorpu").createEntityManager();
  
          entityManager.getTransaction().begin();
          entityManager.persist(new Exam());
          entityManager.getTransaction().commit();
          entityManager.close();
          System.out.println("_exam_ok_");
  
      } catch (Exception e) {
          // entityManager.getTransaction().rollback();
          e.printStackTrace();
          System.out.println("_exam_ko_");
      }
  
      model.addAttribute("examId", "1");
  
      return "welcome";
      }
  }
  </code></pre>
  
  <p>Here is my Entity</p>
  
  <pre><code>@Entity
  @Table(name = "EXAM")
  public class Exam {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "EXAM_ID")
      private long id;
  
      @Column(name = "TITLE") 
      private String title;
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
  }
  </code></pre>
  
  <p>and here is my persistence.xml</p>
  
  <pre><code>&lt;persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"&gt;
  &lt;persistence-unit name="examinatorpu"
      transaction-type="RESOURCE_LOCAL"&gt;
      &lt;class&gt;examinator.entity.Exam&lt;/class&gt;
      &lt;properties&gt;
          &lt;property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /&gt; &lt;!-- DB Driver --&gt;
          &lt;property name="javax.persistence.jdbc.url"
              value="jdbc:postgresql://localhost:5432/examinatordb" /&gt;
          &lt;property name="javax.persistence.jdbc.user" value="postgres" /&gt; &lt;!-- DB User --&gt;
          &lt;property name="javax.persistence.jdbc.password" value="admin" /&gt; &lt;!-- DB Password --&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /&gt; &lt;!-- DB Dialect --&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt; &lt;!-- create / create-drop / update --&gt;
          &lt;property name="hibernate.show_sql" value="true" /&gt; &lt;!-- Show SQL in console --&gt;
          &lt;property name="hibernate.format_sql" value="true" /&gt; &lt;!-- Show SQL formatted --&gt;
          &lt;!-- &lt;property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" 
              /&gt; --&gt;
  
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>and here is the error</p>
  
  <pre><code>javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)
      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1371)
      at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:60)
      at examinator.controller.ExamController.hello(ExamController.java:23)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:220)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:52)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
      at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
      at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
      at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1263)
      at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:57)
      ... 40 more
  Caused by: org.postgresql.util.PSQLException: This ResultSet is closed.
      at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkClosed(AbstractJdbc2ResultSet.java:2674)
      at org.postgresql.jdbc2.AbstractJdbc2ResultSet.setFetchSize(AbstractJdbc2ResultSet.java:1791)
      at org.postgresql.jdbc4.Jdbc4Statement.createResultSet(Jdbc4Statement.java:39)
      at org.postgresql.jdbc2.AbstractJdbc2Statement$StatementResultHandler.handleResultRows(AbstractJdbc2Statement.java:211)
      at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1772)
      at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
      at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
      at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
      at org.postgresql.jdbc2.AbstractJdbc2Connection.execSQLUpdate(AbstractJdbc2Connection.java:263)
      at org.postgresql.jdbc2.AbstractJdbc2Connection.getTransactionIsolation(AbstractJdbc2Connection.java:775)
      at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:182)
      at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:276)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
      ... 45 more
  </code></pre>