使用c3p0和Tomcat进行Hibernate:连接太多

时间:2013-05-16 03:43:22

标签: hibernate tomcat c3p0

我有一个带有c3p0的hibernate持久层,它在Tomcat上运行。我有一个连接太多的问题。只有少数用户进入网站后,它开始增加与数据库的连接。我已经尝试将maxConnectionAge更改为0,我已经将max_pool_size更改为0.我已经搞砸了很多c3p0设置,这些设置来自许多网站的人们有相同问题的建议。然而,似乎没有任何帮助,最终数据库获得太多连接,应用程序停止运行。我希望有人可能会有其他一些可能有帮助的见解。下面是一些相关的代码。

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">ibisalibi1</property>
    <property name="hibernate.connection.url">jdbc:mysql://ikidhub-db-1.cvo0kkopzfhs.us-west-1.rds.amazonaws.com/homemoviezoo</property>
    <property name="hibernate.connection.username">rjdamore</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="log4jdbc.drivers">jdbc:log4jdbc:mysql://ikidhub-db-1.cvo0kkopzfhs.us-west-1.rds.amazonaws.com/homemoviezoo</property>
    <!-- configuration pool via c3p0-->
            <property name="c3p0.acquire_increment">1</property>
            <property name="c3p0.idle_test_period">800</property> <!-- seconds -->
            <property name="c3p0.max_size">1</property>
            <property name="c3p0.max_statements">20</property>
            <property name="c3p0.min_size">1</property>
            <property name="c3p0.timeout">1800</property> <!-- seconds -->
            <property name="c3p0.maxConnectionAge">0</property>
            <property name="connection.provider_class">
                               org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>


    <property   name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="current_session_context_class">thread</property>
    <property name="hibernate.show_sql">true</property>
    <!--  <property name="hbm2ddl.auto">update</property>-->
    <mapping class="com.local.shared.School"/>
    <mapping class="com.local.shared.SchoolVideo"/>
    <mapping class="com.local.shared.Category"/>
    <mapping class="com.local.shared.Administrator"/>
</session-factory>

</hibernate-configuration>

SessionFactoryUtil

public class SessionFactoryUtil {

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

private static SessionFactory configureSessionFactory() 
        throws HibernateException {

    Configuration configuration = new Configuration();
    configuration.configure();
    serviceRegistry = new ServiceRegistryBuilder()
            .applySettings(configuration.getProperties())
            .buildServiceRegistry();

    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    return sessionFactory;
}


public static SessionFactory getInstance() {
    return configureSessionFactory();
    //return sessionFactory;
}

public Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
}

public static void close() {
    if (sessionFactory != null) {
        sessionFactory.close();
        sessionFactory = null;
    }
}

  }

我的db_access_class

的典型方法
  @Override
public School loadSchoolInfo(School schoolInfo) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
        tx = session.beginTransaction();
        School school = new School();
        String admin = schoolInfo.getAdministrator_name();

        Query q1 = (Query) session.getNamedQuery("school.admin");
        q1.setParameter("admin", admin);
        for (Iterator<?> iter = q1.iterate(); iter.hasNext();)
            school = (School) iter.next();
        schoolInfo.setAdministrator_name((school.getAdministrator_name()));
        schoolInfo.setEmail((school.getEmail()));
        schoolInfo.setSchool_name((school.getSchool_name()));

        tx.commit();
    } catch (RuntimeException e) {
        if (tx != null && tx.isActive()) {
            try {
                tx.rollback();
            } catch (HibernateException e1) {
                e1.printStackTrace();
            }
            throw e;
        }
             } finally {
            if (session != null) {
            if (session.isOpen())
                session.close();
        }
        }
        return schoolInfo;
}

1 个答案:

答案 0 :(得分:1)

尝试将c3p0.max_size更改为100左右,将c3p0.min_size更改为10.现在设置的方式只会使用一个连接,这可能是您遇到的瓶颈。如果你故意将这些选项设置为1,我很好奇为什么,因为如果只使用一个连接,你也可以扔掉c3p0。