【问题标题】:Hibernate - Is there a possibility to wait with createEntityManager for a free connectionHibernate - 是否有可能与 createEntityManager 一起等待免费连接
【发布时间】:2019-08-07 10:25:29
【问题描述】:

我正在使用来自 Hibernate 的 EntityManagerFactory 并且 connectionPoolSize 为 20。现在我有一些更大的查询(t> 30 分钟)完全占用了我的池。当我开始一个新查询(使用 getEntityManager)时,我得到以下异常:

javax.persistence.PersistenceException: org.hibernate.HibernateException: 
The internal connection pool has reached its maximum size and no connection is currently available!
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1538)
at org.hibernate.query.Query.getResultList(Query.java:165)

是否有任何舒适的功能可以让我的线程进入睡眠状态,直到连接可用?

我只使用标准函数:

public static EntityManager getEntityManager () {
return emFactory.createEntityManager ();
}

我的 EntityManagerFactory 的创建:

public static void createEntityManagerFactory(String hostname, Integer port, String driver,
  String database, boolean validate, int maxConnections, Properties properties) {

Properties props = new Properties();
props.setProperty("hibernate.connection.url", "jdbc:" + driver + ":thin:@" + hostname + ":" + port + ":" + database);
props.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
props.setProperty("hibernate.connection.username", properties.getProperty("user")); 
props.setProperty("hibernate.connection.password", properties.getProperty("password"));
props.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");

emFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, props);

}

我的 persistence.xml 包含:

<property name="connection.pool_size" value="20" />

我阅读了有关 C3P0 设置的信息,它们可以帮助我吗?

-- 更新 -- 我在使用 C3P0 时遇到了一些问题(例如:ORA-01652:即使使用少量最大连接池大小,也无法在表空间 SYSTEM 中将临时段扩展 128),这是我在休眠时没有的香草。 -- 配置 c3p0

<property name="hibernate.c3p0.min_size" value ="2"/> 
<property name="hibernate.c3p0.max_size" value ="10"/> 
<property name="hibernate.c3p0.acquire_increment" value ="2"/> 
<property name="hibernate.c3p0.idle_test_period" value ="100"/> 
<property name="hibernate.c3p0.timeout" value ="3600"/>

【问题讨论】:

  • 要配置C3p0吗?
  • 我无法判断,我只是告诉自己。它能解决我的问题吗?
  • 嗯,我不知道,但是使用 C3P0 可以重用连接而不是单击并重新打开它,这肯定是将其导入项目的一小步
  • 我在使用 C3P0 时遇到了一些问题(例如:ORA-01652:即使在最大连接池大小很少的情况下,也无法在表空间 SYSTEM 中将临时段扩展 128),这是我在休眠时没有的香草。现在我正在测试Hikari,乍一看看起来很有希望。看起来我最初的问题已经解决了,但我仍然不能说 hikari 有多稳定,或者它是否带来了其他问题。
  • 如何配置C3p0?可以发一下代码吗?

标签: hibernate hibernate-entitymanager


【解决方案1】:

我通过使用 进行连接池解决了这个问题。

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2021-08-17
    • 2010-10-08
    • 2018-01-29
    • 1970-01-01
    • 2011-05-12
    相关资源
    最近更新 更多