春季启动和休眠5-EntityManagerFactory会话管理

时间:2018-11-12 14:24:34

标签: hibernate spring-boot dao

我正在使用Spring-boot和Hibernate开发第一个Web应用程序。

我已经这样写了我的第一个DAO:

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();

    }
}

像这样在每个方法中调用entityManagerFactory是否正确? 我已经阅读了一些有关此的文章,我了解到每次查询结束时该工具都会关闭该会话,对吗?

启动时一切都很好,但是在应用程序中进行了几次查询后,我遇到了这种崩溃:

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]

我想知道这是否来自我对Hibernate会话的实现/使用。

任何帮助将不胜感激:o) 谢谢

1 个答案:

答案 0 :(得分:0)

原因是您没有关闭会话。另外,更好的做法是正确处理事务。

如果您想手动处理会话和传译,则可以检查此方法 https://stackoverflow.com/a/36278975/3405171

如果您尚未阅读Spring @Transactional批注和Spring Data,则可能需要阅读。