【问题标题】:Hibernate DAO leaves open connectionsHibernate DAO 留下打开的连接
【发布时间】:2018-01-30 15:30:44
【问题描述】:

我正在使用 Hibernate 来管理一个非常简单的单表数据库。 我曾经使用过基于 eclipse-link 的示例,但这给我带来了问题,所以我切换到了 hibernate。

我确实按照这个教程hibernate-jpa-dao-example

看起来很复杂,但问题很简单: 我的服务器充满了剩余连接。 我将应用程序托管在 HEROKU 上。它可以工作,但在几次请求之后,连接池 (20) 都被吃光了,应用程序停止响应,我什至无法通过 CLI 连接。 即使在同一个函数中从客户端发起 3 个请求也会占用 3 个连接。

SEVERE: Connection error: 
org.postgresql.util.PSQLException: FATAL: too many connections for role "fjodybratyjsdp"

有人可以纠正我的 DAO 吗?我无法弄清楚为什么连接没有关闭

DAO

public class ParksDAO {

    private static final String HIBERNATE_CFG_XML = "hibernateAPP1.cfg.xml";
private static ParksDAO instance;

private Session currentSession;

private Transaction currentTransaction;

private ParksDAO() {

}

public static ParksDAO getInstance() {
    if (instance == null) {
        instance = new ParksDAO();
    }
    return instance;
}

    public Session openCurrentSession() {
        currentSession = getSessionFactory().openSession();
        return currentSession;
    }

    public Session openCurrentSessionwithTransaction() {
        currentSession = getSessionFactory().openSession();
        currentTransaction = currentSession.beginTransaction();
        return currentSession;
    }

    public void closeCurrentSession() {
        currentSession.close();
    }

    public void closeCurrentSessionwithTransaction() {
        currentTransaction.commit();
        currentSession.close();
    }

    private static SessionFactory getSessionFactory() {
        Configuration configuration = new Configuration().configure(HIBERNATE_CFG_XML);
        configuration.addAnnotatedClass(Park.class);
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
        return sessionFactory;
    }

    public Session getCurrentSession() {
        return currentSession;
    }

    public void setCurrentSession(Session currentSession) {
        this.currentSession = currentSession;
    }

    public Transaction getCurrentTransaction() {
        return currentTransaction;
    }

    public void setCurrentTransaction(Transaction currentTransaction) {
        this.currentTransaction = currentTransaction;
    }

    public Park persist(Park entity) {
        getCurrentSession().save(entity);
        return entity;
    }

    public void update(Park entity) {
        getCurrentSession().update(entity);
    }

    public Park merge(Park entity) {
        return (Park) getCurrentSession().merge(entity);
    }

    public Park findById(String id) {
        Park item = (Park) getCurrentSession().get(Park.class, id);
        return item; 
    }

    public void delete(Park entity) {
        getCurrentSession().delete(entity);
    }

}

持久化服务

public class ParkPersistencyService {
    private static ParksDAO parkDAO;

    public static Park savePark(Park entity) {
        parkDAO = ParksDAO.getInstance();
        parkDAO.openCurrentSessionwithTransaction();
        Park p = parkDAO.persist(entity);
        parkDAO.closeCurrentSessionwithTransaction();
        return p;
    }

    public static Park updatePark(Park entity) {
        parkDAO = ParksDAO.getInstance();
        parkDAO.openCurrentSessionwithTransaction();
        Park newEntity = parkDAO.merge(entity);
        parkDAO.closeCurrentSessionwithTransaction();
        return newEntity;
    }

    public static void removePark(Park p) {
        parkDAO = ParksDAO.getInstance();
        parkDAO.openCurrentSessionwithTransaction();
        parkDAO.delete(p);
        parkDAO.closeCurrentSessionwithTransaction();
    }

    public static Park getParkById(int id) {
        parkDAO = ParksDAO.getInstance();
        parkDAO.openCurrentSession();
        Park book = (Park) parkDAO.getCurrentSession().get(Park.class, id);
        parkDAO.closeCurrentSession();
        return book;
    }

    public static List<Park> getAllParks() {
        parkDAO = ParksDAO.getInstance();
        parkDAO.openCurrentSession();
        List<Park> books = (List<Park>) parkDAO.getCurrentSession().createQuery("from Park").list();
        parkDAO.closeCurrentSession();
        return books;
    }

hibernateAPP1.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
   <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>

   <property name="hibernate.connection.url">jdbc:postgresql://ec2-54-247-101-191.eu-west-1.compute.amazonaws.com:5432/diaqjdbn90g1o</property>
   <property name="hibernate.connection.username">dmwykddcycwmtm</property>
   <property name="hibernate.connection.password">1a56ef9ccd4f3535f09a10a0c846518d7608bb6b677a1649d5036d02d3ae4aaa</property>
   <property name="hibernate.hbm2ddl.auto">update</property>
   <property name="show_sql">false</property>
   <property name="hibernate.current_session_context_class">thread</property>

   <property name="connection.pool_size">20</property>
</session-factory>
</hibernate-configuration>

【问题讨论】:

  • 我的建议是在 Heroku 上使用 Spring Boot,它将提供您需要的一切:devcenter.heroku.com/articles/…
  • @SimonMartinelli 我现在也发布了 CFG 文件
  • 我担心我不能使用 spring boot,因为我可能仅限于使用 heroku CLI 并通过上传 .WAR 文件来推送修订
  • 你读过我关于 Heroku 上 Spring Boot 的文章吗?这适用于 CLI。顺便提一句。希望您没有在问题中发布真实密码...
  • @SimonMartinelli 在#timeout 之后关闭非活动连接不是一个选项,因为我必须开发一个包含多个休眠调用的请求

标签: java hibernate heroku dao


【解决方案1】:

不要在你的函数中每次都创建 parkDAO 怎么样。而是在您的构造函数中创建它并像这样重用它:

public ParkPersistencyService() {
   parkDAO = new ParksDAO();
}

public static Park savePark(Park entity) {
    parkDAO.openCurrentSessionwithTransaction();
    Park p = parkDAO.persist(entity);
    parkDAO.closeCurrentSessionwithTransaction();
    return p;
}

【讨论】:

  • 我只是尝试使用单例范式应用您的解决方案,但没有任何改变。我也确实更新了我的问题中的代码。
  • 但是你已经有了parkDAO,那你为什么还要用ParksDAO.getInstance() 呢?
猜你喜欢
  • 1970-01-01
  • 2011-03-13
  • 2014-07-11
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 2011-03-05
相关资源
最近更新 更多