简体   繁体   English

休眠-连接过多

[英]Hibernate - Too Many Connections

I have a RESTful API which I am using for an Android application. 我有一个用于Android应用程序的RESTful API。 Everything is working fine but after some time I run out of connections on the database. 一切工作正常,但是一段时间后,我的数据库连接用完了。

I am monitoring the Client Connections using MySQL Workbench and everytime I log a user in, the connection number increases. 我正在使用MySQL Workbench监视客户端连接,每次登录用户时,连接号都会增加。 (None of them are being re-used or closed). (它们都没有被重复使用或关闭)。

Here is my example Select query to get a users information based on his authentication token: 这是我的示例Select查询,以根据用户的身份验证令牌获取用户信息:

    Session session = HibernateUtil.getSessionFactory().openSession();

    String sql = "SELECT * FROM user WHERE token = :token";
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(User.class);
    query.setParameter("token", authToken);
    List results = query.list();

    if(results.isEmpty()){
        session.close();
        return null;
    }
    else {
        session.close();
        return (User) results.get(0);
    }

The HibernateUtil.java class is set up as follows: HibernateUtil.java类的设置如下:

public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static SessionFactory createSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure();
    serviceRegistry = new ServiceRegistryBuilder().applySettings(
            configuration.getProperties()). buildServiceRegistry();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    return sessionFactory;
}

public static SessionFactory getSessionFactory() {
    if(sessionFactory != null)
        return sessionFactory;
    else
        return createSessionFactory();
}

Even though I am doing session.close(); 即使我正在执行session.close(); the client connections keep increasing until the MySQL treshold is reached and it gives me the error of Too Many Connections. 客户端连接不断增加,直到达到MySQL阈值为止,这给了我太多连接的错误。

I have looked around a lot to try and find an answer but nothing seems to have helped... Any guidance is appreciated! 我到处寻找尝试找到答案的方法很多,但似乎没有任何帮助...感谢任何指导!

Thanks Mike 谢谢迈克

EDIT: 编辑:

Here is my configuration file (hibernate.cgf.xml) 这是我的配置文件(hibernate.cgf.xml)

 <property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
    <property name="hibernate.c3p0.min_size">0</property>
    <property name="hibernate.c3p0.max_size">1</property>
    <property name="hibernate.c3p0.acquire_increment">1</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.timeout">1800</property>
    <property name="hibernate.c3p0.maxConnectionAge">0</property>

Set the connection pooling properties in the hibernate configuration file. 在休眠配置文件中设置连接池属性。 For connection pooling refer this link. 有关连接池,请参考链接。

In addition to that, you can write your own connection closing logic as follows: 除此之外,您可以编写自己的连接关闭逻辑,如下所示:

private void closeSessionFactory(SessionFactory factory) { 
   if(factory instanceof SessionFactoryImpl) {
      SessionFactoryImpl sf = (SessionFactoryImpl)factory;
      ConnectionProvider conn = sf.getConnectionProvider();
      conn.close();
   }
   factory.close();
}

Let me know if that solves your issue. 让我知道是否可以解决您的问题。

put these in your configuration xml and try, 将它们放在您的配置xml中并尝试,

<prop key="hibernate.connection.release_mode">after_statement</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>

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

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