JPA+Hibernate的两种种连接池设置C3P0、Proxool
时间:2010-10-04 来源:ailon
JPA+Hibernate的两种种连接池设置C3P0、Proxool
一、Proxool的配置
1、配置persistence.xml文件
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="UTF-8"?>
02.<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
03. <persistence-unit name="crmPersistenceUnit" transaction-type="RESOURCE_LOCAL">
04. <provider>org.hibernate.ejb.HibernatePersistence</provider>
05. <properties>
06. <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
07. <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
08. <property name="hibernate.show_sql" value="true"/>
09. <property name="hibernate.format_sql" value="true"/>
10.
11. <!-- hibernate的proxool连接池配置(需要jar包:proxool.jar) -->
12. <property name="hibernate.connection.provider_class" value="org.hibernate.connection.ProxoolConnectionProvider"/>
13. <property name="hibernate.proxool.pool_alias" value="OraclePool"/>
14. <property name="hibernate.proxool.xml" value="proxool.xml"/>
15.
16. </properties>
17. </persistence-unit>
18.</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="crmPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- hibernate的proxool连接池配置(需要jar包:proxool.jar) -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.ProxoolConnectionProvider"/>
<property name="hibernate.proxool.pool_alias" value="OraclePool"/>
<property name="hibernate.proxool.xml" value="proxool.xml"/>
</properties>
</persistence-unit>
</persistence>
2、在src目录下新增proxool.xml文件,文件配置如下
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<something-else-entirely>
03. <proxool>
04. <alias>OraclePool</alias>
05. <!--proxool只能管理由自己产生的连接-->
06. <driver-url>jdbc:oracle:thin:@localhost:1521:crm</driver-url>
07. <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
08. <driver-properties>
09. <property name="user" value="crm" />
10. <property name="password" value="crm123" />
11. </driver-properties>
12. <!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->
13. <house-keeping-sleep-time>90000</house-keeping-sleep-time>
14. <!-- 最少保持的空闲连接数-->
15. <prototype-count>5</prototype-count>
16. <!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
17. <maximum-connection-count>100</maximum-connection-count>
18. <!-- 最小连接数-->
19. <minimum-connection-count>10</minimum-connection-count>
20. </proxool>
21.</something-else-entirely>
<?xml version="1.0" encoding="utf-8"?>
<something-else-entirely>
<proxool>
<alias>OraclePool</alias>
<!--proxool只能管理由自己产生的连接-->
<driver-url>jdbc:oracle:thin:@localhost:1521:crm</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="crm" />
<property name="password" value="crm123" />
</driver-properties>
<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 最少保持的空闲连接数-->
<prototype-count>5</prototype-count>
<!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>
3、注意proxool.xml中不要加注释,否则会报错
二、C3P0连接池配置
1、配置persistence.xml文件
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="tssPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jiaoyou"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="ganbin"/>
<property name="hibernate.connection.password" value="tarena#2010"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- hibernate的c3p0连接池配置(需要jar包:c3p0-0.9.0.4.jar) -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<!-- 最小连接数 -->
<property name="c3p0.min_size" value="5"/>
<!-- 最大连接数 -->
<property name="c3p0.max_size" value="30"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="c3p0.maxIdleTime" value="60"/>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="c3p0.timeout" value="1800"/>
<!-- 最大的PreparedStatement的数量 -->
<property name="c3p0.max_statements" value="50"/>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="c3p0.idle_test_period" value="120"/>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="c3p0.acquire_increment" value="1"/>
<!-- 是否每次都验证连接是否可用 -->
<property name="c3p0.validate" value="false"/>
</properties>
</persistence-unit>
</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="tssPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jiaoyou"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="ganbin"/>
<property name="hibernate.connection.password" value="tarena#2010"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- hibernate的c3p0连接池配置(需要jar包:c3p0-0.9.0.4.jar) -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<!-- 最小连接数 -->
<property name="c3p0.min_size" value="5"/>
<!-- 最大连接数 -->
<property name="c3p0.max_size" value="30"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="c3p0.maxIdleTime" value="60"/>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="c3p0.timeout" value="1800"/>
<!-- 最大的PreparedStatement的数量 -->
<property name="c3p0.max_statements" value="50"/>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="c3p0.idle_test_period" value="120"/>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="c3p0.acquire_increment" value="1"/>
<!-- 是否每次都验证连接是否可用 -->
<property name="c3p0.validate" value="false"/>
</properties>
</persistence-unit>
</persistence>
两种连接池都需要对应的jar包:proxool.jar和c3p0.jar可以在网上找到 转自:http://07er.com/newsview.asp?id=428&treeid=1
一、Proxool的配置
1、配置persistence.xml文件
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="UTF-8"?>
02.<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
03. <persistence-unit name="crmPersistenceUnit" transaction-type="RESOURCE_LOCAL">
04. <provider>org.hibernate.ejb.HibernatePersistence</provider>
05. <properties>
06. <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
07. <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
08. <property name="hibernate.show_sql" value="true"/>
09. <property name="hibernate.format_sql" value="true"/>
10.
11. <!-- hibernate的proxool连接池配置(需要jar包:proxool.jar) -->
12. <property name="hibernate.connection.provider_class" value="org.hibernate.connection.ProxoolConnectionProvider"/>
13. <property name="hibernate.proxool.pool_alias" value="OraclePool"/>
14. <property name="hibernate.proxool.xml" value="proxool.xml"/>
15.
16. </properties>
17. </persistence-unit>
18.</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="crmPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- hibernate的proxool连接池配置(需要jar包:proxool.jar) -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.ProxoolConnectionProvider"/>
<property name="hibernate.proxool.pool_alias" value="OraclePool"/>
<property name="hibernate.proxool.xml" value="proxool.xml"/>
</properties>
</persistence-unit>
</persistence>
2、在src目录下新增proxool.xml文件,文件配置如下
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<something-else-entirely>
03. <proxool>
04. <alias>OraclePool</alias>
05. <!--proxool只能管理由自己产生的连接-->
06. <driver-url>jdbc:oracle:thin:@localhost:1521:crm</driver-url>
07. <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
08. <driver-properties>
09. <property name="user" value="crm" />
10. <property name="password" value="crm123" />
11. </driver-properties>
12. <!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->
13. <house-keeping-sleep-time>90000</house-keeping-sleep-time>
14. <!-- 最少保持的空闲连接数-->
15. <prototype-count>5</prototype-count>
16. <!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
17. <maximum-connection-count>100</maximum-connection-count>
18. <!-- 最小连接数-->
19. <minimum-connection-count>10</minimum-connection-count>
20. </proxool>
21.</something-else-entirely>
<?xml version="1.0" encoding="utf-8"?>
<something-else-entirely>
<proxool>
<alias>OraclePool</alias>
<!--proxool只能管理由自己产生的连接-->
<driver-url>jdbc:oracle:thin:@localhost:1521:crm</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="crm" />
<property name="password" value="crm123" />
</driver-properties>
<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 最少保持的空闲连接数-->
<prototype-count>5</prototype-count>
<!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>
3、注意proxool.xml中不要加注释,否则会报错
二、C3P0连接池配置
1、配置persistence.xml文件
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="tssPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jiaoyou"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="ganbin"/>
<property name="hibernate.connection.password" value="tarena#2010"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- hibernate的c3p0连接池配置(需要jar包:c3p0-0.9.0.4.jar) -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<!-- 最小连接数 -->
<property name="c3p0.min_size" value="5"/>
<!-- 最大连接数 -->
<property name="c3p0.max_size" value="30"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="c3p0.maxIdleTime" value="60"/>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="c3p0.timeout" value="1800"/>
<!-- 最大的PreparedStatement的数量 -->
<property name="c3p0.max_statements" value="50"/>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="c3p0.idle_test_period" value="120"/>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="c3p0.acquire_increment" value="1"/>
<!-- 是否每次都验证连接是否可用 -->
<property name="c3p0.validate" value="false"/>
</properties>
</persistence-unit>
</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="tssPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jiaoyou"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="ganbin"/>
<property name="hibernate.connection.password" value="tarena#2010"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- hibernate的c3p0连接池配置(需要jar包:c3p0-0.9.0.4.jar) -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<!-- 最小连接数 -->
<property name="c3p0.min_size" value="5"/>
<!-- 最大连接数 -->
<property name="c3p0.max_size" value="30"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="c3p0.maxIdleTime" value="60"/>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="c3p0.timeout" value="1800"/>
<!-- 最大的PreparedStatement的数量 -->
<property name="c3p0.max_statements" value="50"/>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="c3p0.idle_test_period" value="120"/>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="c3p0.acquire_increment" value="1"/>
<!-- 是否每次都验证连接是否可用 -->
<property name="c3p0.validate" value="false"/>
</properties>
</persistence-unit>
</persistence>
两种连接池都需要对应的jar包:proxool.jar和c3p0.jar可以在网上找到 转自:http://07er.com/newsview.asp?id=428&treeid=1
相关阅读 更多 +