我已经使用Struts2 and Hibernate 4.3.5实现了一个web应用程序
我正面临一些问题,需要澄清一些连接池属性。
为了使用hibernate创建连接池,我使用了c3p0-0.9.1.jar
下面是hibernate.cfg.xml中c3p0的连接池属性
<!-- Connection Pooling using c3p0 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.timeout">500</property>
<property name="hibernate.c3p0.max_statements">2</property>// I heard somewhere that this property improves performance.. But dont know How it improves
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.preferredTestQuery">
select sysdate from dual;
</property>在我的应用程序中,通过在hibernte-cfg.xml中设置以下属性,我使用了*每次请求会话*模式
<!-- To open the connection when the request hits. -->
<property name="hibernate.current_session_context_class">thread</property>当我使用它时,我得到的Session如下所示
//Here MyHiberantaeUtil is my own class having singleton pattern and Static block
// To read hibernate-cfg.xml and will create SessionFactroy object.
SessionFactory sessionFactory = MyHibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();根据文档..当我使用此模式时,应该在请求时创建连接,并在请求完成时关闭连接。对于我的情况,它正在创建连接..也就是说,我可以使用这个getCurrentSession()获取会话,但它不会在请求结束时关闭。
为了测试连接池,我使用了jconsole tool in my jdk
我能看到的地方
`numBusyConection = 2(If that is two requests.)`我想知道我有没有遗漏什么。为什么thread属性不能按预期工作?
And how to say that Request has completed and request has generated ?
Is session-per-request is best pattern or better to go any other patterns ?
I did not find this class `org.hibernate.connection.C3P0ConnectionProvider` in any jar files.. But My application is connecting to DB and working fine. 请帮忙提前道谢。
发布于 2014-07-25 12:53:32
JDBC连接在第一次需要时打开,在会话关闭时关闭。当您退出服务层时,需要手动关闭会话:
session.close();否则,连接将不会关闭,并且您将耗尽连接池。
使用Spring是一个更好的主意,因为它支持自动创建/关闭会话。您的MyHibernateUtil无法与Spring提供的功能相匹敌:
运行时异常的成功或回滚时自动提交本地transactions
使用Hibernate设置Spring和使用HibernateUtil类一样简单。
https://stackoverflow.com/questions/24947534
复制相似问题