2013-07-22 19 views
11

我想知道什麼是我的connection.pool_size的合理數字?它涉及哪些方面?一旦爲其定義大小,還需要知道如何測試應用程序。如何查找數據庫連接池的合理大小以及如何驗證它?

我的應用程序將由ATLAST 100用戶同時使用,它的數據庫中有超過20個表。我的數據庫是MySQL,至少有12個系統同時使用我的應用程序。如果您需要了解更多信息,請告訴我。

我也發現了以下內容,它有助於定義連接池大小,但仍然不確定合理的數字是多少。

Hibernate's own connection pooling algorithm is, however, quite rudimentary. 
    It is intended to help you get started and is not intended for use in a production 
    system, or even for performance testing. You should use a third party pool for 
    best performance and stability. Just replace the hibernate.connection.pool_size 
    property with connection pool specific settings. This will turn off Hibernate's 
    internal pool. For example, you might like to use c3p0. 

    connection.pool_size indicates the maximum number of pooled connections. So it is 
    better to keep it at a logical count. It depends on your application and DB how 
    much it can handle. 10 is a reasonable count that will typically used as it is 
    sufficient for most cases. 

我HibernateUtil的是如下

import org.hibernate.HibernateException; 
    import org.hibernate.Session; 
    import org.hibernate.SessionFactory; 
    import org.hibernate.cfg.Configuration; 
    import org.hibernate.service.ServiceRegistry; 
    import org.hibernate.service.ServiceRegistryBuilder; 

    public class HibernateUtil { 

     private static ServiceRegistry serviceRegistry; 
     private static final ThreadLocal<Session> threadLocal = new ThreadLocal(); 
     private static SessionFactory sessionFactory; 
     private static SessionFactory configureSessionFactory() { 
      try { 
       Configuration configuration = new Configuration(); 
       configuration.configure(); 
       serviceRegistry = new 
ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 
       sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
       return sessionFactory; 
      } catch (HibernateException e) { 
       System.out.append("** Exception in SessionFactory **"); 
       e.printStackTrace(); 
      } 
      return sessionFactory; 
     } 

     static { 
     try { 
      sessionFactory = configureSessionFactory(); 
     } catch (Exception e) { 
      System.err.println("%%%% Error Creating SessionFactory %%%%"); 
      e.printStackTrace(); 
     } 
     } 

     private HibernateUtil() { 
     } 

     public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
     } 

     public static Session getSession() throws HibernateException { 
     Session session = threadLocal.get(); 

     if (session == null || !session.isOpen()) { 
      if (sessionFactory == null) { 
      rebuildSessionFactory(); 
      } 
      session = (sessionFactory != null) ? sessionFactory.openSession() : null; 
      threadLocal.set(session); 
     } 

     return session; 
     } 

     public static void rebuildSessionFactory() { 
     try { 
      sessionFactory = configureSessionFactory(); 
     } catch (Exception e) { 
      System.err.println("%%%% Error Creating SessionFactory %%%%"); 
      e.printStackTrace(); 
     } 
     } 

     public static void closeSession() throws HibernateException { 
     Session session = (Session) threadLocal.get(); 
     threadLocal.set(null); 

     if (session != null) { 
      session.close(); 
     } 
     } 
    } 
+1

您正在使用哪個數據庫,以及每次連接到數據庫的系統數量? – Karthikeyan

+2

@Karthikeyan問題被更新,12個系統同時使用它並且數據庫是mysql –

回答

8

您必須使用實際的框架,你會多少最小和最大連接池的使用測試。根據this article

小的連接池:

會對連接表更快的訪問。 但是可能沒有足夠的連接來滿足請求,並且請求 可能會在隊列中花費更多時間。

大型連接池:

將有更多的連接,以滿足要求 和要求將在對連接表 訪問速度較慢的成本花費較少(或沒有)的時間在排隊。

所以你必須測試一些連接池,做一些負載測試。還要考慮獲取當前負載的性能/資源使用信息,並進行一些基於交易成本的分析。

並且通過分析的結果,如果對連接表的訪問速度太慢,那麼可以減少連接池,或者如果連接不夠,則可以添加更多連接池。平衡這些因素以獲得最佳時間流逝。

+0

對於上面的考試,現實會是什麼? – maxammann

2

如果你正在使用一些應用程序服務器(Jboss,Weblogic,Glassfish等等),這個人可以向你展示一些關於池使用情況的統計信息。分析一些這些數據(最大隊列時間,最大連接使用等),並運行一些測試,以找到最適合您的案例的數字。

0

您必須使用第三方連接池,如c3p0。 100個併發用戶需要20到30個連接。您必須使用某種工具(如jmeter)來執行性能測試。使用perfomance工具,您可以發送n個併發請求。根據該報告,您可以增加或減少連接大小。

0

通過進行監控和調整,瞭解您需要多少連接的唯一合理方法就是通過進行監控和調整。這是因爲連接獲取時間,池大小和傳入請求吞吐量之間的關係由Little's Law給出,因此池大小取決於請求的數量以及您在獲得連接之前願意等待的時間。

FlexyPool是一個開放源代碼框架,允許您監視連接使用情況,甚至可以將池大小增加到初始容量以外。

FlexyPool collects the following metrics

  • 併發連接直方圖
  • 併發連接請求直方圖
  • 數據源連接獲取時間直方圖
  • 連接租約時間直方圖
  • 最大池尺寸直方圖
  • 總連接獲取時間直方圖
  • 溢出池大小直方圖
  • 試嘗試直方圖

它支持幾乎所有主要的連接池解決方案:

  • 的Apache DBCP
  • 阿帕奇DBCP2
  • C3P0
  • BoneCP
  • 嗨kariCP
  • Tomcat的CP
  • Vibur DBCP
  • Bitronix事務管理
  • Atomikos公司TransactionsEssentials
  • 的Java EE數據源

它採用Codahale/Dropwizard指標,讓你可以用Graphana或石墨整合。

所以,回到你的問題。您可以從一個較小的池大小(5個連接)開始,並配置5個額外連接的溢出緩衝區。您可以根據應用程序SLA(100 ms)設置超時間隔。然後,您可以監視連接池使用情況as explained in this article

相關問題