2013-05-16 46 views
0

我有一個Hibernate持久層C3P0,它運行在Tomcat上。我有一個連接太多的問題。只有少數用戶登錄網站後,它開始瘋狂地連接到數據庫。我曾嘗試將maxConnectionAge更改爲0,我已將max_pool_size更改爲0.我對許多網站提供的c3p0設置進行了大量調查,結果發現存在相同問題的用戶。然而,似乎沒有任何幫助,最終數據庫得到太多的連接,應用程序停止運作。我希望有人可能有其他一些可能有所幫助的見解。以下是一些相關的代碼。休眠與C3P0和Tomcat:過多的連接

的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; 
} 
+0

沒有人喜歡我的問題:-( – rjdamore

+0

有你嘗試將超時設置爲較小的數字,如100或下? – Angga

回答

1

嘗試改變c3p0.max_size到大約100和c3p0.min_size〜10的方式它現在設置的只會使用一個連接,這可能是你正在經歷的瓶頸。如果你故意將這些選項設置爲1,我很好奇爲什麼,因爲如果只使用一個連接,那麼你也可以扔掉c3p0。