hibernate学习 C3P0连接池

经过前面的学习,已经掌握了hibernate的基本使用。本篇讲的是hibernate使用数据库连接池的配置。

1、使用C3P0

  • 导入jar包,hibernate-release-5.2.10.Final\lib\optional\c3p0文件夹下
hibernate学习 C3P0连接池_第1张图片
  • 配置hibernate.cfg.xml文件,直接贴出完整配置文件


          

    
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3306/hiber
        root
        root
        
        org.hibernate.dialect.MySQL5InnoDBDialect
        update
        true
        true
        
        thread
        
        
        
                
        20
        
        5
        
        2
        
        50
        
        1800
        
        120
        
        
        
    

  • 测试使用C3P0

控制台输出如上信息说明C3P0使用成功。END


在源码中发现hibernate中C3P0有效的配置也就已上六个,而我百度到的还发现这样一条配置true设置为true每次都检测连接是否可用。没有测试不知道是否有用。

下面是AvailableSettings类中关于C3P0的配置部分代码

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // c3p0 connection pooling specific settings
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    /**
     * A setting prefix used to indicate settings that target the hibernate-c3p0 integration
     */
    String C3P0_CONFIG_PREFIX = "hibernate.c3p0";

    /**
     * Maximum size of C3P0 connection pool
     */
    String C3P0_MAX_SIZE = "hibernate.c3p0.max_size";

    /**
     * Minimum size of C3P0 connection pool
     */
    String C3P0_MIN_SIZE = "hibernate.c3p0.min_size";

    /**
     * Maximum idle time for C3P0 connection pool
     */
    String C3P0_TIMEOUT = "hibernate.c3p0.timeout";

    /**
     * Maximum size of C3P0 statement cache
     */
    String C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";

    /**
     * Number of connections acquired when pool is exhausted
     */
    String C3P0_ACQUIRE_INCREMENT = "hibernate.c3p0.acquire_increment";

    /**
     * Idle time beforeQuery a C3P0 pooled connection is validated
     */
    String C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";

你可能感兴趣的:(hibernate学习 C3P0连接池)