<?xml version="1.0" encoding="utf-8" ?>
<!-- generator="crazycoder cms 1.0(beta)" -->
<rss version="2.0" xmlns:sns="http://www.crazycoder.cn/rss/news">
	<channel>
		<title>tomcat连接池的配置</title>
		<description></description>
		<link>http://CrazyCoder.cn/Tag/67458/Index.html</link>
		<lastBuildDate>2010-07-31</lastBuildDate>
		<generator>CrazyCoder 1.0(beta)</generator>
		<language>zh-cn</language>
		<copyright>Copyright 1996 - 2008 疯狂代码  All Rights Reserved.</copyright>
		<pubDate>2010-07-31</pubDate>
		<item>
			<title><![CDATA[c3p0连接池:C3P0连接池详细配置和实现]]></title>
			<link>http://CrazyCoder.cn/Java/Article42954.html</link>
			<description>
				<![CDATA[

		
          
              
          <br/>
        <br/>
          &lt;c3p0-config&gt; <br />&lt;default-config&gt; <br />&lt;!--当连接池中<img src='/icons/16684de.gif' />连接耗尽<img src='/icons/16684de.gif' />时候c3p0<img src='/icons/16684yi.gif' />次同时获取<img src='/icons/16684de.gif' />连接数<img src='/icons/16684dou2.gif' />Default: 3 --&gt; <br />&lt;property name=&quot;acquireIncrement&quot;&gt;3&lt;/property&gt; <br /><br />&lt;!--定义在从数据库获取新连接失败后重复尝试<img src='/icons/16684de.gif' />次数<img src='/icons/16684dou2.gif' />Default: 30 --&gt; <br />&lt;property name=&quot;acquireRetryAttempts&quot;&gt;30&lt;/property&gt; <br /><br />&lt;!--两次连接中间隔时间<img src='/icons/16684dou.gif' />单位毫秒<img src='/icons/16684dou2.gif' />Default: 1000 --&gt; <br />&lt;property name=&quot;acquireRetryDelay&quot;&gt;1000&lt;/property&gt; <br /><br />&lt;!--连接关闭时默认将所有未提交<img src='/icons/16684de.gif' />操作回滚<img src='/icons/16684dou2.gif' />Default: false --&gt; <br />&lt;property name=&quot;autoCommitOnClose&quot;&gt;false&lt;/property&gt; <br /><br />&lt;!--c3p0将建<img src='/icons/16684yi.gif' />张名为Test<img src='/icons/16684de.gif' />空表<img src='/icons/16684dou.gif' />并使用其自带<img src='/icons/16684de.gif' />查询语句进行测试<img src='/icons/16684dou2.gif' />如果定义了这个参数那么 <br />属性preferredTestQuery将被忽略<img src='/icons/16684dou2.gif' />你不能在这张Test表上进行任何操作<img src='/icons/16684dou.gif' />它将只供c3p0测试 <br />使用<img src='/icons/16684dou2.gif' />Default: null--&gt; <br />&lt;property name=&quot;automaticTestTable&quot;&gt;Test&lt;/property&gt; <br /><br />&lt;!--获取连接失败将会引起所有等待连接池来获取连接<img src='/icons/16684de.gif' />线程抛出异常<img src='/icons/16684dou2.gif' />但是数据源仍有效 <br />保留<img src='/icons/16684dou.gif' />并在下次<img src='/icons/16684diaoyong.gif' />getConnection<img src='/icons/16684kh.gif' /><img src='/icons/16684de.gif' />时候继续尝试获取连接<img src='/icons/16684dou2.gif' />如果设为true<img src='/icons/16684dou.gif' />那么在尝试 <br />获取连接失败后该数据源将申明已断开并永久关闭<img src='/icons/16684dou2.gif' />Default: false--&gt; <br />&lt;property name=&quot;<img src='/icons/16684break.gif' />AfterAcquireFailure&quot;&gt;false&lt;/property&gt; <br /><br />&lt;!--当连接池用完时客户端<img src='/icons/16684diaoyong.gif' />getConnection<img src='/icons/16684kh.gif' />后等待获取新连接<img src='/icons/16684de.gif' />时间<img src='/icons/16684dou.gif' />超时后将抛出 <br />SQLException,如设为0则无限期等待<img src='/icons/16684dou2.gif' />单位毫秒<img src='/icons/16684dou2.gif' />Default: 0 --&gt; <br />&lt;property name=&quot;checkoutTimeout&quot;&gt;100&lt;/property&gt; <br /><br />&lt;!--通过实现ConnectionTester或QueryConnectionTester<img src='/icons/16684de.gif' />类来测试连接<img src='/icons/16684dou2.gif' />类名需制定全路径<img src='/icons/16684dou2.gif' /> <br />Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester--&gt; <br />&lt;property name=&quot;connectionTesterClassName&quot;&gt;&lt;/property&gt; <br /><br />&lt;!--指定c3p0 libraries<img src='/icons/16684de.gif' />路径<img src='/icons/16684dou.gif' />如果(通常都是这样)在本地即可获得那么无需设置<img src='/icons/16684dou.gif' />默认null即可 <br />Default: null--&gt; <br />&lt;property name=&quot;factoryClassLocation&quot;&gt;null&lt;/property&gt; <br /><br />&lt;!--Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs. <br />(文档原文)作者强烈建议不使用<img src='/icons/16684de.gif' /><img src='/icons/16684yi.gif' />个属性--&gt; <br />&lt;property name=&quot;forceIgnoreUnresolvedTransactions&quot;&gt;false&lt;/property&gt; <br /><br />&lt;!--每60秒检查所有连接池中<img src='/icons/16684de.gif' />空闲连接<img src='/icons/16684dou2.gif' />Default: 0 --&gt; <br />&lt;property name=&quot;idleConnectionTestPeriod&quot;&gt;60&lt;/property&gt; <br /><br />&lt;!--<img src='/icons/16684chushi.gif' />化时获取 3个连接<img src='/icons/16684dou.gif' />取值应在minPoolSize和maxPoolSize的间<img src='/icons/16684dou2.gif' />Default: 3 --&gt; <br />&lt;property name=&quot;initialPoolSize&quot;&gt;3&lt;/property&gt; <br /><br />&lt;!--最大空闲时间,60秒内未使用则连接被丢弃<img src='/icons/16684dou2.gif' />若为0则永不丢弃<img src='/icons/16684dou2.gif' />Default: 0 --&gt; <br />&lt;property name=&quot;maxIdleTime&quot;&gt;60&lt;/property&gt; <br /><br />&lt;!--连接池中保留<img src='/icons/16684de.gif' />最大连接数<img src='/icons/16684dou2.gif' />Default: 15 --&gt; <br />&lt;property name=&quot;maxPoolSize&quot;&gt;15&lt;/property&gt; <br /><br />&lt;!--JDBC<img src='/icons/16684de.gif' />标准参数<img src='/icons/16684dou.gif' />用以控制数据源内加载<img src='/icons/16684de.gif' />PreparedStatements数量<img src='/icons/16684dou2.gif' />但由于预缓存Cache<img src='/icons/16684de.gif' />statements <br />属于单个connection而不是整个连接池<img src='/icons/16684dou2.gif' />所以设置这个参数需要考虑到多方面<img src='/icons/16684de.gif' />原因<img src='/icons/16684dou2.gif' /> <br />如果maxStatements和maxStatementsPerConnection均为0<img src='/icons/16684dou.gif' />则缓存Cache被关闭<img src='/icons/16684dou2.gif' />Default: 0--&gt; <br />&lt;property name=&quot;maxStatements&quot;&gt;100&lt;/property&gt; <br /><br />&lt;!--maxStatementsPerConnection定义了连接池内单个连接所拥有<img src='/icons/16684de.gif' />最大缓存Cachestatements数<img src='/icons/16684dou2.gif' />Default: 0 --&gt; <br />&lt;property name=&quot;maxStatementsPerConnection&quot;&gt;&lt;/property&gt; <br /><br />&lt;!--c3p0是异步操作<img src='/icons/16684de.gif' /><img src='/icons/16684dou.gif' />缓慢<img src='/icons/16684de.gif' />JDBC操作通过帮助进程完成<img src='/icons/16684dou2.gif' />扩展这些操作可以有效<img src='/icons/16684de.gif' />提升性能 <br />通过多线程实现多个操作同时被执行<img src='/icons/16684dou2.gif' />Default: 3--&gt; <br />&lt;property name=&quot;numHelperThreads&quot;&gt;3&lt;/property&gt; <br /><br />&lt;!--当用户<img src='/icons/16684diaoyong.gif' />getConnection<img src='/icons/16684kh.gif' />时使root用户成为去获取连接<img src='/icons/16684de.gif' />用户<img src='/icons/16684dou2.gif' />主要用于连接池连接非c3p0 <br /><img src='/icons/16684de.gif' />数据源时<img src='/icons/16684dou2.gif' />Default: null--&gt; <br />&lt;property name=&quot;overrideDefaultUser&quot;&gt;root&lt;/property&gt; <br /><br />&lt;!--和overrideDefaultUser参数对应使用<img src='/icons/16684de.gif' /><img src='/icons/16684yi.gif' />个参数<img src='/icons/16684dou2.gif' />Default: null--&gt; <br />&lt;property name=&quot;overrideDefaultPassword&quot;&gt;password&lt;/property&gt; <br /><br />&lt;!--密码<img src='/icons/16684dou2.gif' />Default: null--&gt; <br />&lt;property name=&quot;password&quot;&gt;&lt;/property&gt; <br /><br />&lt;!--定义所有连接测试都执行<img src='/icons/16684de.gif' />测试语句<img src='/icons/16684dou2.gif' />在使用连接测试<img src='/icons/16684de.gif' />情况下这个<img src='/icons/16684yi.gif' />显著提高测试速度<img src='/icons/16684dou2.gif' />注意: <br />测试<img src='/icons/16684de.gif' />表必须在<img src='/icons/16684chushi.gif' />数据源<img src='/icons/16684de.gif' />时候就存在<img src='/icons/16684dou2.gif' />Default: null--&gt; <br />&lt;property name=&quot;preferredTestQuery&quot;&gt;select id from test where id=1&lt;/property&gt; <br /><br />&lt;!--用户修改系统配置参数执行前最多等待300秒<img src='/icons/16684dou2.gif' />Default: 300 --&gt; <br />&lt;property name=&quot;propertyCycle&quot;&gt;300&lt;/property&gt; <br /><br />&lt;!--因性能消耗大请只在需要<img src='/icons/16684de.gif' />时候使用它<img src='/icons/16684dou2.gif' />如果设为true那么在每个connection提交<img src='/icons/16684de.gif' /> <br />时候都将校验其有效性<img src='/icons/16684dou2.gif' />建议使用idleConnectionTestPeriod或automaticTestTable <br />等思路方法来提升连接测试<img src='/icons/16684de.gif' />性能<img src='/icons/16684dou2.gif' />Default: false --&gt; <br />&lt;property name=&quot;testConnectionOnCheckout&quot;&gt;false&lt;/property&gt; <br /><br />&lt;!--如果设为true那么在取得连接<img src='/icons/16684de.gif' />同时将校验连接<img src='/icons/16684de.gif' />有效性<img src='/icons/16684dou2.gif' />Default: false --&gt; <br />&lt;property name=&quot;testConnectionOnCheckin&quot;&gt;true&lt;/property&gt;  
		
          
              
          <br/>
        <br/>
          <br /><br />&lt;!--用户名<img src='/icons/16684dou2.gif' />Default: null--&gt; <br />&lt;property name=&quot;user&quot;&gt;root&lt;/property&gt; <br /><br />&lt;!--早期<img src='/icons/16684de.gif' />c3p0版本对JDBC接口采用动态反射代理<img src='/icons/16684dou2.gif' />在早期版本用途广泛<img src='/icons/16684de.gif' />情况下这个参数 <br />允许用户恢复到动态反射代理以解决不稳定<img src='/icons/16684de.gif' />故障<img src='/icons/16684dou2.gif' />最新<img src='/icons/16684de.gif' />非反射代理更快并且已经开始 <br />广泛<img src='/icons/16684de.gif' />被使用<img src='/icons/16684dou.gif' />所以这个参数未必有用<img src='/icons/16684dou2.gif' />现在原先<img src='/icons/16684de.gif' />动态反射和新<img src='/icons/16684de.gif' />非反射代理同时受到 <br />支持<img src='/icons/16684dou.gif' />但今后可能<img src='/icons/16684de.gif' />版本可能不支持动态反射代理<img src='/icons/16684dou2.gif' />Default: false--&gt; <br />&lt;property name=&quot;usesTraditionalReflectiveProxies&quot;&gt;false&lt;/property&gt; <br /><br />&lt;property name=&quot;automaticTestTable&quot;&gt;con_test&lt;/property&gt; <br />&lt;property name=&quot;checkoutTimeout&quot;&gt;30000&lt;/property&gt; <br />&lt;property name=&quot;idleConnectionTestPeriod&quot;&gt;30&lt;/property&gt; <br />&lt;property name=&quot;initialPoolSize&quot;&gt;10&lt;/property&gt; <br />&lt;property name=&quot;maxIdleTime&quot;&gt;30&lt;/property&gt; <br />&lt;property name=&quot;maxPoolSize&quot;&gt;25&lt;/property&gt; <br />&lt;property name=&quot;minPoolSize&quot;&gt;10&lt;/property&gt; <br />&lt;property name=&quot;maxStatements&quot;&gt;0&lt;/property&gt; <br />&lt;user-overrides user=&quot;swaldman&quot;&gt; <br />&lt;/user-overrides&gt; <br />&lt;/default-config&gt; <br />&lt;named-config name=&quot;dumbTestConfig&quot;&gt; <br />&lt;property name=&quot;maxStatements&quot;&gt;200&lt;/property&gt; <br />&lt;user-overrides user=&quot;poop&quot;&gt; <br />&lt;property name=&quot;maxStatements&quot;&gt;300&lt;/property&gt; <br />&lt;/user-overrides&gt; <br />&lt;/named-config&gt; <br />&lt;/c3p0-config&gt; <br /><br />c3p0<img src='/icons/16684de.gif' />实现 <br /><br />&lt;!-- <br />全局使用c3p0 <br />1. server.xml中GlobalNamingResources <br />2. context.xml中ResourceLink <br />3. web.xml <br />--&gt; <br />全局使用c3p0 <br />1. server.xml中GlobalNamingResources <br />&lt;Resource auth=&quot;Container&quot; <br />description=&quot;DB Connection&quot; <br />driverClass=&quot;org.gjt.mm.mysql.Driver&quot; <br />maxPoolSize=&quot;10&quot; <br />minPoolSize=&quot;2&quot; <br />acquireIncrement=&quot;2&quot; <br />name=&quot;jdbc/mysql1&quot; <br />user=&quot;root&quot; <br />password=&quot;xxxxxx&quot; <br />factory=&quot;org.apache.naming.factory.BeanFactory&quot; <br />type=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot; <br />jdbcUrl=&quot;jdbc:mysql://localhost:3306/noticemanager?characterEncoding=UTF-8&amp;amp;useUnicode=TRUE&amp;amp;autoReconnect=true&quot; /&gt; <br /><br />2. context.xml中ResourceLink <br />&lt;ResourceLink name=&quot;jdbc/mysql1&quot; global=&quot;jdbc/mysql1&quot; type=&quot;javax.sql.DataSource&quot;/&gt; <br /><br />3. web.xml <br />&lt;resource-ref&gt; <br />&lt;description&gt;Tomcat Datasource&lt;/description&gt; <br />&lt;res-ref-name&gt;jdbc/mysql1&lt;/res-ref-name&gt; <br />&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt; <br />&lt;res-auth&gt;Container&lt;/res-auth&gt; <br />&lt;/resource-ref&gt; <br /><br /><br /><br />4.Hibernate中<img src='/icons/16684de.gif' />配置 <br /><br />&lt;?xml version='1.0' encoding='UTF-8'?&gt; <br />&lt;!DOCTYPE hibernate-configuration PUBLIC <br />&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot; <br />&quot;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&quot;&gt; <br /><br />&lt;!-- Generated by MyEclipse Hibernate Tools. --&gt; <br />&lt;hibernate-configuration&gt; <br /><br />&lt;session-factory&gt; <br />&lt;property name=&quot;c3p0.min_size&quot;&gt;5&lt;/property&gt; <br />&lt;property name=&quot;c3p0.max_size&quot;&gt;100&lt;/property&gt; <br />&lt;property name=&quot;c3p0.time_out&quot;&gt;1800&lt;/property&gt; <br />&lt;property name=&quot;c3p0.max_statement&quot;&gt;100&lt;/property&gt; <br /><br />&lt;property name=&quot;dialect&quot;&gt; <br />org.hibernate.dialect.MySQLDialect <br />&lt;/property&gt; <br />&lt;property name=&quot;jndi.url&quot;&gt; <br />jdbc:mysql://localhost:3306/noticemanager <br />&lt;/property&gt; <br />&lt;!-- &lt;property name=&quot;jndi.<img src='/icons/16684class.gif' />&quot;&gt; <br />org.gjt.mm.mysql.Driver <br />&lt;/property&gt; --&gt; <br />&lt;property name=&quot;connection.datasource&quot;&gt; <br />java:comp/env/jdbc/mysql1 <br />&lt;/property&gt; <br />&lt;property name=&quot;connection.username&quot;&gt;root&lt;/property&gt; <br />&lt;property name=&quot;connection.password&quot;&gt;xxxxxxxxx&lt;/property&gt; <br />&lt;property name=&quot;hibernate.show_sql&quot;&gt;true&lt;/property&gt; <br />&lt;!-- &lt;property name=&quot;hibernate.use_outer_join&quot;&gt;true&lt;/property&gt; --&gt; <br />&lt;mapping resource=&quot;com/dpoo/pojo/Notice.hbm.xml&quot; /&gt; <br /><br />&lt;/session-factory&gt; <br /><br />&lt;/hibernate-configuration&gt; <br /><br /><br />JDBC实现:和DateSource相同 <br /><br />try { <br />Context ctx = <img src='/icons/16684new.gif' /> InitialContext<img src='/icons/16684kh.gif' />; <br />ds = (DataSource) ctx.lookup(&quot;java:comp/env/jdbc/mysql1&quot;); <br />conn = ds.getConnection<img src='/icons/16684kh.gif' />; <br />} catch (NamingException e) { <br />e.pr<img src='/icons/16684int.gif' />StackTrace<img src='/icons/16684kh.gif' />; <br /><img src='/icons/16684return.gif' /> null; <br /><br />} catch (SQLException e) { <br />e.pr<img src='/icons/16684int.gif' />StackTrace<img src='/icons/16684kh.gif' />; <br /><img src='/icons/16684return.gif' /> null; <br />}  <a href="/mypdf/42954.pdf" title="查看 c3p0连接池:C3P0连接池详细配置和实现 的PDF电子文档">[查看PDF电子文档]</a>				]]>
			</description>
			<author></author>
			<category>Java教程</category>
			<pubDate>2009-02-12</pubDate>
			<guid>http://CrazyCoder.cn/Java/Article42954.html</guid>
		</item>
	</channel>
</rss>
