0

我有一个带有 c3p0 的休眠持久层,它在 Tomcat 上运行。我有太多连接的问题。在只有少数用户访问该站点后,它开始增加与数据库的连接。我尝试将 maxConnectionAge 更改为 0,我已将 max_pool_size 更改为 0。根据许多站点的建议,我已经弄乱了很多 c3p0 设置,而人们也遇到了同样的问题。但是,似乎没有任何帮助,最终数据库获得了太多连接并且应用程序停止运行。我希望有人可能有一些其他的见解可能会有所帮助。下面是一些相关的代码。

休眠.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;
}
4

1 回答 1

1

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

于 2013-08-01T14:43:25.960 回答