spring boot and hibernate 5 - EntityManagerFactory session management












0














I'm working on my first web app with Spring-boot and hibernate.



I've written my first DAO like this :



package myapplication.orderinfo;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

@Component
public class OrderInfoDaoImpl implements OrderInfoDao {

@Autowired
private EntityManagerFactory entityManagerFactory;

public List<OrderInfo> getOrderInfoDetails() {
Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(OrderInfo.class);
Root contactRoot = criteria.from(OrderInfo.class);
criteria.select(contactRoot);
return session.createQuery(criteria).getResultList();

}

public List<OrderInfo> getOrderInfo(String typeName) {
Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(OrderInfo.class);
Root contactRoot = criteria.from(OrderInfo.class);
criteria.where(builder.equal(contactRoot.get("typeName"), typeName));
return session.createQuery(criteria).getResultList();

}
}


Is it correct to call the entityManagerFactory in each method like this?
I've read some articles on this, I understood that the session is closed by the tool at the end of each query, is this correct?



Everything looks fine at startup, but after a few queries in the application I've got this kind of crash :



Hibernate: select distinct orderinfo0_.projectLeader as col_0_0_ from t_orderInfo orderinfo0_
2018-11-12 15:12:11.746 WARN 11360 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: null
2018-11-12 15:12:11.747 ERROR 11360 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : HikariPool-1 - Connection is not available, request timed out after 30000ms.
2018-11-12 15:12:11.752 ERROR 11360 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause

java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.
at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:669) ~[HikariCP-2.7.9.jar:na]


I'm wondering if this coming from my implementation / usage of the Hibernate session.



Any help will be appreciate :o)
Thanks










share|improve this question



























    0














    I'm working on my first web app with Spring-boot and hibernate.



    I've written my first DAO like this :



    package myapplication.orderinfo;

    import java.util.List;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    import javax.persistence.EntityManagerFactory;
    import javax.persistence.TypedQuery;
    import javax.persistence.criteria.CriteriaBuilder;
    import javax.persistence.criteria.CriteriaQuery;
    import javax.persistence.criteria.Root;

    @Component
    public class OrderInfoDaoImpl implements OrderInfoDao {

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    public List<OrderInfo> getOrderInfoDetails() {
    Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery criteria = builder.createQuery(OrderInfo.class);
    Root contactRoot = criteria.from(OrderInfo.class);
    criteria.select(contactRoot);
    return session.createQuery(criteria).getResultList();

    }

    public List<OrderInfo> getOrderInfo(String typeName) {
    Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery criteria = builder.createQuery(OrderInfo.class);
    Root contactRoot = criteria.from(OrderInfo.class);
    criteria.where(builder.equal(contactRoot.get("typeName"), typeName));
    return session.createQuery(criteria).getResultList();

    }
    }


    Is it correct to call the entityManagerFactory in each method like this?
    I've read some articles on this, I understood that the session is closed by the tool at the end of each query, is this correct?



    Everything looks fine at startup, but after a few queries in the application I've got this kind of crash :



    Hibernate: select distinct orderinfo0_.projectLeader as col_0_0_ from t_orderInfo orderinfo0_
    2018-11-12 15:12:11.746 WARN 11360 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: null
    2018-11-12 15:12:11.747 ERROR 11360 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : HikariPool-1 - Connection is not available, request timed out after 30000ms.
    2018-11-12 15:12:11.752 ERROR 11360 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause

    java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.
    at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:669) ~[HikariCP-2.7.9.jar:na]


    I'm wondering if this coming from my implementation / usage of the Hibernate session.



    Any help will be appreciate :o)
    Thanks










    share|improve this question

























      0












      0








      0







      I'm working on my first web app with Spring-boot and hibernate.



      I've written my first DAO like this :



      package myapplication.orderinfo;

      import java.util.List;

      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.Transaction;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Component;

      import javax.persistence.EntityManagerFactory;
      import javax.persistence.TypedQuery;
      import javax.persistence.criteria.CriteriaBuilder;
      import javax.persistence.criteria.CriteriaQuery;
      import javax.persistence.criteria.Root;

      @Component
      public class OrderInfoDaoImpl implements OrderInfoDao {

      @Autowired
      private EntityManagerFactory entityManagerFactory;

      public List<OrderInfo> getOrderInfoDetails() {
      Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
      CriteriaBuilder builder = session.getCriteriaBuilder();
      CriteriaQuery criteria = builder.createQuery(OrderInfo.class);
      Root contactRoot = criteria.from(OrderInfo.class);
      criteria.select(contactRoot);
      return session.createQuery(criteria).getResultList();

      }

      public List<OrderInfo> getOrderInfo(String typeName) {
      Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
      CriteriaBuilder builder = session.getCriteriaBuilder();
      CriteriaQuery criteria = builder.createQuery(OrderInfo.class);
      Root contactRoot = criteria.from(OrderInfo.class);
      criteria.where(builder.equal(contactRoot.get("typeName"), typeName));
      return session.createQuery(criteria).getResultList();

      }
      }


      Is it correct to call the entityManagerFactory in each method like this?
      I've read some articles on this, I understood that the session is closed by the tool at the end of each query, is this correct?



      Everything looks fine at startup, but after a few queries in the application I've got this kind of crash :



      Hibernate: select distinct orderinfo0_.projectLeader as col_0_0_ from t_orderInfo orderinfo0_
      2018-11-12 15:12:11.746 WARN 11360 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: null
      2018-11-12 15:12:11.747 ERROR 11360 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : HikariPool-1 - Connection is not available, request timed out after 30000ms.
      2018-11-12 15:12:11.752 ERROR 11360 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause

      java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.
      at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:669) ~[HikariCP-2.7.9.jar:na]


      I'm wondering if this coming from my implementation / usage of the Hibernate session.



      Any help will be appreciate :o)
      Thanks










      share|improve this question













      I'm working on my first web app with Spring-boot and hibernate.



      I've written my first DAO like this :



      package myapplication.orderinfo;

      import java.util.List;

      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.Transaction;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Component;

      import javax.persistence.EntityManagerFactory;
      import javax.persistence.TypedQuery;
      import javax.persistence.criteria.CriteriaBuilder;
      import javax.persistence.criteria.CriteriaQuery;
      import javax.persistence.criteria.Root;

      @Component
      public class OrderInfoDaoImpl implements OrderInfoDao {

      @Autowired
      private EntityManagerFactory entityManagerFactory;

      public List<OrderInfo> getOrderInfoDetails() {
      Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
      CriteriaBuilder builder = session.getCriteriaBuilder();
      CriteriaQuery criteria = builder.createQuery(OrderInfo.class);
      Root contactRoot = criteria.from(OrderInfo.class);
      criteria.select(contactRoot);
      return session.createQuery(criteria).getResultList();

      }

      public List<OrderInfo> getOrderInfo(String typeName) {
      Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
      CriteriaBuilder builder = session.getCriteriaBuilder();
      CriteriaQuery criteria = builder.createQuery(OrderInfo.class);
      Root contactRoot = criteria.from(OrderInfo.class);
      criteria.where(builder.equal(contactRoot.get("typeName"), typeName));
      return session.createQuery(criteria).getResultList();

      }
      }


      Is it correct to call the entityManagerFactory in each method like this?
      I've read some articles on this, I understood that the session is closed by the tool at the end of each query, is this correct?



      Everything looks fine at startup, but after a few queries in the application I've got this kind of crash :



      Hibernate: select distinct orderinfo0_.projectLeader as col_0_0_ from t_orderInfo orderinfo0_
      2018-11-12 15:12:11.746 WARN 11360 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: null
      2018-11-12 15:12:11.747 ERROR 11360 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : HikariPool-1 - Connection is not available, request timed out after 30000ms.
      2018-11-12 15:12:11.752 ERROR 11360 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause

      java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.
      at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:669) ~[HikariCP-2.7.9.jar:na]


      I'm wondering if this coming from my implementation / usage of the Hibernate session.



      Any help will be appreciate :o)
      Thanks







      hibernate spring-boot dao






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 14:24









      alak

      247




      247
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The reason is that you don't close a session. Also, better, to properly work with transactions.



          You can check this approach, if you want to work with the session and tranaxctions manually
          https://stackoverflow.com/a/36278975/3405171



          Probably you will need to read about Spring @Transactional annotation and Spring Data, if you have not done it yet.






          share|improve this answer























          • Well I "don't want to" work with transaction manually, I found an example with implementation of DAO and used it. As suggested, I read Spring Data related articles and then I saw that people seems to prefer repository usage instead of DAO. So I switched to repo, It's look fine now. Thank you for pointing me the direction!
            – alak
            Nov 14 at 10:38












          • @alak You are welcome.
            – v.ladynev
            Nov 14 at 12:29











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264199%2fspring-boot-and-hibernate-5-entitymanagerfactory-session-management%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The reason is that you don't close a session. Also, better, to properly work with transactions.



          You can check this approach, if you want to work with the session and tranaxctions manually
          https://stackoverflow.com/a/36278975/3405171



          Probably you will need to read about Spring @Transactional annotation and Spring Data, if you have not done it yet.






          share|improve this answer























          • Well I "don't want to" work with transaction manually, I found an example with implementation of DAO and used it. As suggested, I read Spring Data related articles and then I saw that people seems to prefer repository usage instead of DAO. So I switched to repo, It's look fine now. Thank you for pointing me the direction!
            – alak
            Nov 14 at 10:38












          • @alak You are welcome.
            – v.ladynev
            Nov 14 at 12:29
















          0














          The reason is that you don't close a session. Also, better, to properly work with transactions.



          You can check this approach, if you want to work with the session and tranaxctions manually
          https://stackoverflow.com/a/36278975/3405171



          Probably you will need to read about Spring @Transactional annotation and Spring Data, if you have not done it yet.






          share|improve this answer























          • Well I "don't want to" work with transaction manually, I found an example with implementation of DAO and used it. As suggested, I read Spring Data related articles and then I saw that people seems to prefer repository usage instead of DAO. So I switched to repo, It's look fine now. Thank you for pointing me the direction!
            – alak
            Nov 14 at 10:38












          • @alak You are welcome.
            – v.ladynev
            Nov 14 at 12:29














          0












          0








          0






          The reason is that you don't close a session. Also, better, to properly work with transactions.



          You can check this approach, if you want to work with the session and tranaxctions manually
          https://stackoverflow.com/a/36278975/3405171



          Probably you will need to read about Spring @Transactional annotation and Spring Data, if you have not done it yet.






          share|improve this answer














          The reason is that you don't close a session. Also, better, to properly work with transactions.



          You can check this approach, if you want to work with the session and tranaxctions manually
          https://stackoverflow.com/a/36278975/3405171



          Probably you will need to read about Spring @Transactional annotation and Spring Data, if you have not done it yet.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 15:35

























          answered Nov 12 at 15:20









          v.ladynev

          11.6k22044




          11.6k22044












          • Well I "don't want to" work with transaction manually, I found an example with implementation of DAO and used it. As suggested, I read Spring Data related articles and then I saw that people seems to prefer repository usage instead of DAO. So I switched to repo, It's look fine now. Thank you for pointing me the direction!
            – alak
            Nov 14 at 10:38












          • @alak You are welcome.
            – v.ladynev
            Nov 14 at 12:29


















          • Well I "don't want to" work with transaction manually, I found an example with implementation of DAO and used it. As suggested, I read Spring Data related articles and then I saw that people seems to prefer repository usage instead of DAO. So I switched to repo, It's look fine now. Thank you for pointing me the direction!
            – alak
            Nov 14 at 10:38












          • @alak You are welcome.
            – v.ladynev
            Nov 14 at 12:29
















          Well I "don't want to" work with transaction manually, I found an example with implementation of DAO and used it. As suggested, I read Spring Data related articles and then I saw that people seems to prefer repository usage instead of DAO. So I switched to repo, It's look fine now. Thank you for pointing me the direction!
          – alak
          Nov 14 at 10:38






          Well I "don't want to" work with transaction manually, I found an example with implementation of DAO and used it. As suggested, I read Spring Data related articles and then I saw that people seems to prefer repository usage instead of DAO. So I switched to repo, It's look fine now. Thank you for pointing me the direction!
          – alak
          Nov 14 at 10:38














          @alak You are welcome.
          – v.ladynev
          Nov 14 at 12:29




          @alak You are welcome.
          – v.ladynev
          Nov 14 at 12:29


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264199%2fspring-boot-and-hibernate-5-entitymanagerfactory-session-management%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine