没错,Hibernate
4.3.x中似乎存在一个错误,该错误中,由Hibernate的默认连接池生成的线程在关闭时不会被清除。我在这里提交了一个错误(请投票!):
https://hibernate.atlassian.net/browse/HHH-8896
在修复之前,您有两种选择。您可以在HibernateUtil中添加一个方法,并使用它来强制连接池在应用执行结束时自行清理:
public static void stopConnectionProvider() { final SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory; ConnectionProvider connectionProvider = sessionFactoryImplementor.getConnectionProvider(); if (Stoppable.class.isInstance(connectionProvider)) { ((Stoppable) connectionProvider).stop(); } }
这可以工作,但是很丑陋,很笨拙,使用了不推荐使用的方法,等等。更好的解决方案是只使用“真实的”连接池,例如c3p0,只需将以下属性添加到hibernate.cfg中就可以启用它。
xml:
<property name="hibernate.c3p0.acquire_increment">1</property><property name="hibernate.c3p0.idle_test_period">100</property><property name="hibernate.c3p0.max_size">10</property><property name="hibernate.c3p0.max_statements">10</property><property name="hibernate.c3p0.min_size">10</property><property name="hibernate.c3p0.timeout">100</property>
请注意,如果您使用其他连接池,则应删除当前配置中的该连接池属性:
<!-- JDBC connection pool (use the built-in) --><property name="connection.pool_size">1</property>
编辑:要使用c3p0连接池,您还需要hibernate-c3p0依赖项。Hibernate快照存储库中的4.3.0-SNAPSHOT的Maven示例:
<repositories> ... <repository> <id>hibernate-snapshots</id> <url>http://snapshots.jboss.org/maven2/</url> </repository> ...</repositories><dependencies> ... <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.3.0-SNAPSHOT</version> </dependency> ...<dependencies>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)