将c3p0与Tomcat 8 DataSource一起使用

时间:2016-07-14 01:06:12

标签: hibernate c3p0

我有一个加载了DataSource的tomcat 8服务器。我想知道是否可以将此DataSource与c3p0连接池管理结合使用。到目前为止,这是我尝试过的。

context.xml中

 <Context>
 ...
 <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" 
  maxIdle="30" maxTotal="100" maxWaitMillis="10000"
name="jdbc/store" password="text" type="javax.sql.DataSource" 
url="jdbc:mysql://localhost:3306/db" username="user"/>
 </Context>

hibernate.cfg.xml中

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.connection.datasource">
        java:comp/env/jdbc/store
    </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>


    <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>

...more stuff

问题是mysql在服务器启动后只显示一个进程

1 个答案:

答案 0 :(得分:4)

我终于找到了解决方案:context.xml资源应该像这样

<Resource auth="Container" 
 name="jdbc/store" 
 type="com.mchange.v2.c3p0.ComboPooledDataSource" 
 driverClass="com.mysql.jdbc.Driver" 
 minPoolSize="5" maxPoolSize="10" 
 factory="org.apache.naming.factory.BeanFactory"
 jdbcUrl="jdbc:mysql://localhost:3306/database" 
 user="user" password="text" />

请注意,一些常见的资源属性是不同的,例如jdbcUrl而不是url,用户而不是用户名等

相关问题