<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>涂0实验室 &#187; DB</title>
	<atom:link href="http://www.sulong.info/archives/tag/db/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sulong.info</link>
	<description>一个程序员的成长之路</description>
	<lastBuildDate>Fri, 27 Aug 2010 01:54:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>DBCP和hibernate有兼容性问题</title>
		<link>http://www.sulong.info/archives/67</link>
		<comments>http://www.sulong.info/archives/67#comments</comments>
		<pubDate>Wed, 30 Jan 2008 05:24:53 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[程序]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[hibernate]]></category>

		<guid isPermaLink="false">http://www.sulong.info/archives/67</guid>
		<description><![CDATA[从spring 2.5的文档里直接抄下的来配置文件，来配置数据库连接池和hibernate，结果发现运行一段时间后程序就会在查询数据库的地方卡死，而且日志里并没有打印现hibenrate的sql，问题到底发生在什么地方呢？我打开jconsole，连接到程序上一看，发现好多线程被阻塞了，而且都阻塞于pool.getConnection()，看来是DBCP出了问题。后来在网上查了一会，找到hibernate邮件列表上的一段讨论，原来hibernate开发人员认为DBCP设计得不好，BUG多，文档少，所以就不再提供对DBCP的支持了，要用数据库连接池就得用c3p0。看来hibernate每次用完连接都不还给连接池的。我也不想去找如何配置C3P0了，反正生产环境用的肯定是jboss的通过JNDI取到的数据源。干脆让tomcat来管理数据库连接池吧，让spring通过jndi来找到它。在tomcat里配置数据源很简单，官方网站上有详细说明。简单说把下面这段xml复制到tomcat安装目录下的conf/context.xml里就可以了： &#60;Resource name=&#34;myDataSourceName&#34; &#160; &#160; auth=&#34;Container&#34; &#160; &#160; type=&#34;javax.sql.DataSource&#34; &#160; &#160; maxActive=&#34;100&#34; &#160; &#160; maxIdle=&#34;30&#34; &#160; &#160; maxWait=&#34;10000&#34; &#160; &#160; username=&#34;root&#34; &#160; &#160; password=&#34;&#34; &#160; &#160; driverClassName=&#34;com.mysql.jdbc.Driver&#34; &#160; &#160; url=&#34;jdbc:mysql://localhost:3309/mydatabase&#34; &#160; &#160; removeAbandoned=&#34;true&#34; &#160; &#160; removeAbandonedTimeout=&#34;10&#34; &#160; &#160; logAbandoned=&#34;false&#34; /&#62; 上面这段Resource元素很容易理解，关键就是removeAbandoned和removeAbandonedTimeout这两个属性，它们是告诉DBCP把被遗弃的连接删掉，以及被遗弃多久才删除。hibernate就是那个常常遗弃连接的家伙。]]></description>
			<content:encoded><![CDATA[<p>从spring 2.5的文档里直接抄下的来配置文件，来配置数据库连接池和hibernate，结果发现运行一段时间后程序就会在查询数据库的地方卡死，而且日志里并没有打印现hibenrate的sql，问题到底发生在什么地方呢？我打开jconsole，连接到程序上一看，发现好多线程被阻塞了，而且都阻塞于pool.getConnection()，看来是DBCP出了问题。后来在网上查了一会，找到hibernate邮件列表上的一段讨论，原来hibernate开发人员认为DBCP设计得不好，BUG多，文档少，所以就不再提供对DBCP的支持了，要用数据库连接池就得用c3p0。看来hibernate每次用完连接都不还给连接池的。我也不想去找如何配置C3P0了，反正生产环境用的肯定是jboss的通过JNDI取到的数据源。干脆让tomcat来管理数据库连接池吧，让spring通过jndi来找到它。在tomcat里配置数据源很简单，<a href="http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html" title="tomcat6下配置数据源说明">官方网站</a>上有详细说明。简单说把下面这段xml复制到tomcat安装目录下的conf/context.xml里就可以了：</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Resource</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myDataSourceName&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">auth</span>=<span style="color: #ff0000;">&quot;Container&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;javax.sql.DataSource&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">maxActive</span>=<span style="color: #ff0000;">&quot;100&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">maxIdle</span>=<span style="color: #ff0000;">&quot;30&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">maxWait</span>=<span style="color: #ff0000;">&quot;10000&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">username</span>=<span style="color: #ff0000;">&quot;root&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">driverClassName</span>=<span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;jdbc:mysql://localhost:3309/mydatabase&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">removeAbandoned</span>=<span style="color: #ff0000;">&quot;true&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">removeAbandonedTimeout</span>=<span style="color: #ff0000;">&quot;10&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">logAbandoned</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></div>
<p>上面这段Resource元素很容易理解，关键就是removeAbandoned和removeAbandonedTimeout这两个属性，它们是告诉DBCP把被遗弃的连接删掉，以及被遗弃多久才删除。hibernate就是那个常常遗弃连接的家伙。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/67/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
