问题描述
我完全失去了,我正在使用hibernate和mysql运行一个批处理作业,几个小时后我得到一个例外,说我正在使用许多连接。我已经阅读了关于SO的所有文章,但似乎没有与我有关。我正在使用Tapestry-hibernate,配置非常简单,。不管我在哪里创建一个新的SessionFactory,一旦应用程序启动,我只是将hibernate Session注入我的类。
I'm at a complete loss, I'm running a batch job using both hibernate and mysql and after a few hours I get an exception saying I'm using to many connections. I've read all the articles on SO, but none seem to relate to me. I'm using Tapestry-hibernate with a very simple configuration, http://tapestry.apache.org/using-tapestry-with-hibernate.html. No where's am I creating a new SessionFactory, once the application starts up, I just inject the hibernate Session into my class.
这是我当前与mysql的连接视图。
This is my current connection view with mysql.
我的批处理作业是线程化的每次新线程触发时,threads_connected似乎都会增加。
My batch job is threaded and everytime a new thread fires off, the threads_connected seems to increment.
我的cfg.xml文件。
my cfg.xml file.
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">jdbc/company</property>
<property name="hbm2ddl.auto">validate</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.search.default.directory_provider">filesystem</property>
<property name="hibernate.search.default.indexBase">/users/george/Documents/indexes </property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
</session-factory>
基本会话使用示例class - 请注意下面的代码不是生产代码,仅用于说明会话使用情况。
Sample of basic session usage in class - "please note below code is not production code, just used to illustrate session usage.
private final Session session;
public LineReaderParserImpl(Session session) {
this.session = session;
}
public void parse() {
exec.submit(new Runnable() {
public void run() {
for (int i = 0; i < 10000; i++) {
Object object = session.createCriteria()...
session.save(object);
session.getTransaction().commit();
if (currentRow % 250 == 0 || currentRow == totalRows) {
try {
session.getTransaction().commit();
} catch (RuntimeException ex) {
try {
session.getTransaction().rollback();
} catch (RuntimeException rbe) {
throw ex;
} finally {
session.clear();
session.beginTransaction();
}
}
}
}
}
}
推荐答案
tapestry-hibernate提供的hibernate会话是PerThread作用域。 PerThread作用域服务通过PerthreadManager.cleanupThread()进行清理。 Tapestry自动清理ParallelExecutor管理的请求线程和线程。如果您正在管理自己的线程,则必须显式调用PerthreadManager.cleanupThread()。
The hibernate session provided by tapestry-hibernate is PerThread scoped. PerThread scoped services are cleaned up via PerthreadManager.cleanupThread(). Tapestry automatically cleans up request threads and threads managed by ParallelExecutor. If you are managing your own thread, you must call PerthreadManager.cleanupThread() explicitly.
这篇关于Hibernate和mysql的连接太多了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!