如何为数据库连接池找到合理的大小以及如何验证它?

Tim*_*man 11 java performance hibernate connection-pooling java-ee

我想知道我的connection.pool_size是多少合理的数字?它与哪些方面有关?一旦为其定义了大小,还需要知道如何测试应用程序.

我的应用程序将由AT LEAST 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.
Run Code Online (Sandbox Code Playgroud)

我的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();
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

Ang*_*gga 9

您必须使用实际框架测试它将使用多少最小和最大连接池.根据这篇文章:

小连接池:

将在连接表上更快地访问.但可能没有足够的连接来满足请求,并且请求可能会在队列中花费更多时间.

大型连接池:

将有更多的连接来满足请求和请求将花费更少(或没有)时间在队列中,代价是连接表上的访问速度较慢.

所以你必须用一些连接池测试,做一些负载测试.还要考虑获取当前负载的性能/资源使用信息,并进行一些基于事务成本的分析.

根据分析结果,如果对连接表的访问速度太慢,则可以减少连接池,或者如果连接不够,则可以添加更多连接池.平衡这些因素以获得最佳时间流逝.