简体   繁体   English

Hibernate和mysql连接过多

[英]Too many connections Hibernate and mysql

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. 我完全不知所措,我正在使用hibernate和mysql运行批处理作业,几个小时后,我收到一个异常消息,说我正在使用许多连接。 I've read all the articles on SO, but none seem to relate to me. 我已经阅读了所有关于SO的文章,但似乎都与我无关。 I'm using Tapestry-hibernate with a very simple configuration, http://tapestry.apache.org/using-tapestry-with-hibernate.html . 我正在使用一个非常简单的配置Tapestry-hibernate 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. 不,我在哪里创建新的SessionFactory,一旦应用程序启动,我就将休眠Session注入到我的类中。

This is my current connection view with mysql. 这是我当前与mysql的连接视图。 在此处输入图片说明

My batch job is threaded and everytime a new thread fires off, the threads_connected seems to increment. 我的批处理作业已线程化,每次启动新线程时,threads_connected似乎都会增加。

my cfg.xml file. 我的cfg.xml文件。

<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>

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

The hibernate session provided by tapestry-hibernate is PerThread scoped. tapestry-hibernate提供的休眠会话是PerThread范围的。 PerThread scoped services are cleaned up via PerthreadManager.cleanupThread(). 通过PerthreadManager.cleanupThread()清理PerThread范围内的服务。 Tapestry automatically cleans up request threads and threads managed by ParallelExecutor. Tapestry自动清除请求线程和由ParallelExecutor管理的线程。 If you are managing your own thread, you must call PerthreadManager.cleanupThread() explicitly. 如果要管理自己的线程,则必须显式调用PerthreadManager.cleanupThread()。

Considering that Session Object is not thread-Safe maybe there's something wrong with the way you are defining the session object which is getting injected. 考虑到会话对象不是线程安全的,也许您定义要注入的会话对象的方式有问题。

It is not intended that implementors be threadsafe. 并不意味着实现者是线程安全的。 Instead each thread/transaction should obtain its own instance from a SessionFactory. 相反,每个线程/事务都应从SessionFactory获取其自己的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM