(16)Hibernate对连接池的支持-创新互联

十年的赤峰林西网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整赤峰林西建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“赤峰林西网站设计”,“赤峰林西网站推广”以来,每个客户项目都认真落实执行。

除非我们用爱去对待一个人,否则我们无法了解他。

We never can have a true view of man unless we have a love for him.

1、连接池知识

连接池的作用: 管理连接;提升连接的利用效率!

常用的连接池: C3P0连接池

Hibernate 自带的也有一个连接池,且对C3P0连接池也有支持!

Hibernate自带连接池:只维护一个连接,比较简陋。

可以查看hibernate.properties文件(%hibernate%/project/etc/hibernate.properties)

Hibernate对C3P0连接池支持的核心类是org.hibernate.connection.C3P0ConnectionProvider

告诉Hibernate使用的是哪一个连接池技术。

#hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider

hibernate.properties中连接池详细配置:

################################# ### Hibernate Connection Pool ### ################################# hibernate.connection.pool_size 1            #Hibernate自带连接池:只有一个连接 ########################### ### C3P0 Connection Pool###                 #Hibernate对C3P0连接池支持 ########################### #hibernate.c3p0.max_size 2                  #大连接数 #hibernate.c3p0.min_size 2                  #最小连接数 #hibernate.c3p0.timeout 5000                #超时时间 #hibernate.c3p0.max_statements 100          #大执行的命令的个数 #hibernate.c3p0.idle_test_period 3000       #空闲测试时间 #hibernate.c3p0.acquire_increment 2         #连接不够用的时候, 每次增加的连接数 #hibernate.c3p0.validate false              # ################################# ### Plugin ConnectionProvider ### ################################# ## use a custom ConnectionProvider (if not set, Hibernate will choose a built-in ConnectionProvider using hueristics) #hibernate.connection.provider_class org.hibernate.connection.DriverManagerConnectionProvider #hibernate.connection.provider_class org.hibernate.connection.DatasourceConnectionProvider #hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider #hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider

2、使用连接池的步骤

(1)添加C3P0的jar包

    %hibernate%/lib/optional/c3p0/c3p0-0.9.1.jar

(2)在hibernate.cfg.xml文件中添加数据库连接池配置

    核心配置

org.hibernate.connection.C3P0ConnectionProvider 4 8 5000 10 10000 2

    完整配置

                           com.mysql.jdbc.Driver         jdbc:mysql:///test         root         root         org.hibernate.dialect.MySQL5Dialect                   true false update org.hibernate.connection.C3P0ConnectionProvider 4 8 5000 10 10000 2       

(3)使用SHOW PROCESSLIST;进行测试

    测试代码

@Test public void testPool() { Session session = sf.openSession(); session.beginTransaction(); Department dept = (Department)session.get(Department.class, 3); System.out.println(dept); //在这里打断点,并使用  SHOW PROCESSLIST;  查看活跃连接 session.getTransaction().commit(); session.close(); }

    测试方法

    a)在执行testPool()方法之前,使用SHOW PROCESSLIST;查看活跃的连接数量

    b)调试执行testPool()方法,停在断点时,查看活跃的连接数量

    c)testPool()方法执行完之后,查看活跃连接数量

    d)修改配置中hibernate.c3p0.min_size数目,再次执行a,b,c查看活跃连接数量

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:(16)Hibernate对连接池的支持-创新互联
分享URL:http://ybzwz.com/article/dsjhsg.html