简体   繁体   English

找到hibernate正在使用的连接池

[英]Find the connection pool hibernate is using

How can I find the connection pool hibernate is using at runtime without looking at the hibernate configuration? 如何在不查看hibernate配置的情况下找到hibernate在运行时使用的连接池? Is there a API like "getConnectionPoolUsed()" which returns the connection pool hibernate is using? 是否有像“getConnectionPoolUsed()”这样的API返回hibernate正在使用的连接池?

I cannot find any such method in the javadocs. 我在javadocs中找不到任何这样的方法。 And I cannot find any exposed methods that return a ConnectionProvider . 而且我找不到任何返回ConnectionProvider公开方法。 (There are some deprecated methods, and some methods on "internal" classes, but you shouldn't be using those.) (有一些不赞成的方法,以及“内部”类的一些方法,但你不应该使用它们。)

IMO, the clean solution is to look at the configuration. IMO,干净的解决方案是查看配置。

In order to use C3P0ConnectionProvider you need hibernate-c3p0.jar (available here ) 要使用C3P0ConnectionProvider,您需要hibernate-c3p0.jar此处可用)

Then configure the following properties in hibernate.cfg.xml 然后在hibernate.cfg.xml中配置以下属性

    <property name="hibernate.connection.pool_size">10</property>

    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>

There is no need to specify *hibernate.connection.provider_class*, Hibernate will use C3P0 as long as there is any hibernate.c3p0.x property. 没有必要指定* hibernate.connection.provider_class *,只要有任何hibernate.c3p0.x属性,Hibernate就会使用C3P0。

The values are just an example, you should use the configuration most appropriate for your needs. 这些值只是一个示例,您应该使用最适合您需求的配置。

No there is no such API. 没有这样的API。 You could do it using reflection (but that would go too deep for this answer). 你可以使用反射来做到这一点(但这对于这个答案来说太深了)。

Maybe you should ask yourself why you want this. 也许你应该问问自己为什么要这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM