<h1 id="数据连接池" style="font-family:'microsoft yahei';font-weight:300;line-height:1.1;color:rgb(63,63,63);font-size:2.6em;">
数据连接池</h1>
<p style="line-height:27.2px;color:rgb(63,63,63);font-family:'microsoft yahei';">
在spring中,常使用数据库连接池来完成对数据库的连接配置,类似于线程池的定义,数据库连接池就是维护有一定数量数据库连接的一个缓冲池,一方面,能够即取即用,免去初始化的时间,另一方面,用完的数据连接会归还到连接池中,这样就免去了不必要的连接创建、销毁工作,提升了性能。当然,使用连接池,有一下几点是连接池配置所考虑到的,也属于配置连接池的优点,而这些也会我们后面的实例配置中体现:<br>
1、 如果没有任何一个用户使用连接,那么那么应该维持一定数量的连接,等待用户使用。<br>
2、 如果连接已经满了,则必须打开新的连接,供更多用户使用。<br>
3、 如果一个服务器就只能有100个连接,那么如果有第101个人过来呢?应该等待其他用户释放连接<br>
4、 如果一个用户等待时间太长了,则应该告诉用户,操作是失败的。</p>
<p style="line-height:27.2px;color:rgb(63,63,63);font-family:'microsoft yahei';">
在spring中,常用的连接池有:jdbc,dbcp,c3p0,JNDI4种,他们有不同的优缺点和适用场景。其中,spring框架推荐使用dbcp,hibernate框架推荐使用c3p0。经测试发现,c3p0与dbcp相比较,c3p0能够更好的支持高并发,但是在稳定性方面略逊于dpcp。<br>
下面对几个连接池进行示例配置:</p>
<ol style="line-height:27.2px;font-family:'microsoft yahei';font-size:16px;"><li>jdbc连接池配置示例</li></ol><pre class="blockcode" style="margin-left:0px;line-height:23.8px;color:rgb(51,51,51);border-style:solid;border-color:rgb(136,136,136);"><code class="language-xml hljs has-numbering" style="color:inherit;font-size:12.6px;"><span class="hljs-tag"><<span class="hljs-title" style="color:rgb(0,0,136);">bean</span> <span class="hljs-attribute" style="color:rgb(102,0,102);">id</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"dataSource"</span>
<span class="hljs-attribute" style="color:rgb(102,0,102);">class</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"org.springframework.jdbc.datasource.DriverManagerDataSource"</span>></span>
<span class="hljs-tag"><<span class="hljs-title" style="color:rgb(0,0,136);">property</span> <span class="hljs-attribute" style="color:rgb(102,0,102);">name</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"driverClassName"</span> <span class="hljs-attribute" style="color:rgb(102,0,102);">value</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"com.mysql.jdbc.Driver"</span>></span>
<span class="hljs-tag"></<span class="hljs-title" style="color:rgb(0,0,136);">property</span>></span>
<span class="hljs-tag"><<span class="hljs-title" style="color:rgb(0,0,136);">property</span> <span class="hljs-attribute" style="color:rgb(102,0,102);">name</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"url"</span> <span class="hljs-attribute" style="color:rgb(102,0,102);">value</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"jdbc:mysql://localhost:3306/yc"</span> /></span>
<span class="hljs-tag"><<span class="hljs-title" style="color:rgb(0,0,136);">property</span> <span class="hljs-attribute" style="color:rgb(102,0,102);">name</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"username"</span> <span class="hljs-attribute" style="color:rgb(102,0,102);">value</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"yc"</span>></span><span class="hljs-tag"></<span class="hljs-title" style="color:rgb(0,0,136);">property</span>></span>
<span class="hljs-tag"><<span class="hljs-title" style="color:rgb(0,0,136);">property</span> <span class="hljs-attribute" style="color:rgb(102,0,102);">name</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"password"</span> <span class="hljs-attribute" style="color:rgb(102,0,102);">value</span>=<span class="hljs-value" style="color:rgb(0,136,0);">"yc"</span>></span><span class="hljs-tag"></<span class="hljs-title" style="color:rgb(0,0,136);">property</span>></span>
<span class="hljs-tag"></<span class="hljs-title" style="color:rgb(0,0,136);">bean</span>></span></code></pre><ul class="pre-numbering" style="line-height:23.8px;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul><blockquote style="margin-left:0px;line-height:27.2px;font-family:'microsoft yahei';">
<p style="line-height:1.5;font-size:1em;color:rgb(111,111,111);">
DriverManagerDataSource没有实现连接池化连接的机制,每次调用getConnection()获取新连接时,只是简单地创建一个新的连接。所以,一般这种方式常用于开发时测试,不用于生产。</p>
</blockquote>
<ol style="line-height:27.2px;font-family:'microsoft yahei';font-size:16px;"><li>dbcp连接池配置示例</li></ol><pre class="blockcode" style="margin-left:0px;line-height:23.8px;color:rgb(51,51,51);border-style:solid;border-color:rgb(136,136,136);"><code class="language-xml hljs has-numbering" style="color:inherit;font-size:12.6px;"><span class="hljs-tag"><<span class="hljs-title" style="color:rgb(0,0,136);">bean</span> <span class="hljs |
|