c3p0数据库连接池+mysql数据库基本使用方法
? ? ? ?一般我們在項目中操作數(shù)據(jù)庫時,都是每次需要操作數(shù)據(jù)庫就建立一個連接,操作完成后釋放連接。因為jdbc沒有保持連接的能力,一旦超過一定時間沒有使用(大約幾百毫秒),
連接就會被自動釋放掉。而每次新建連接都需要140毫秒左右的時間,所以耗費(fèi)時間比較多。若使用C3P0連接池來池化連接,隨時取用,則平均每次取用只需要10-20毫秒。
這在高并發(fā)隨機(jī)訪問數(shù)據(jù)庫的時候?qū)π实奶嵘泻艽髱椭?/span>
? ? ? C3P0連接池會根據(jù)你的配置來初始化N個數(shù)據(jù)庫連接,空閑T時間后連接過期又會自動新建K個連接使得連接池總有空閑的數(shù)據(jù)庫連接等待被取用。我們只需通過dataSourse.getConnection()
即可從線程池中取用一個已經(jīng)連接好的空閑連接,執(zhí)行數(shù)據(jù)庫操作。然后“斷開”(放回)這個連接,把這個連接的使用權(quán)放回連接池。真正的數(shù)據(jù)庫連接的創(chuàng)建與釋放是由C3P0在后臺自動完成的
,我們花的只是取用與釋放占用權(quán)的時間。全程耗時10+毫秒,比原來提高了幾十倍。
?
下面我使用idea舉例
首先需要三個jar包:
?
整體的項目結(jié)構(gòu)如下;
?
這里的xml文件默認(rèn)只能放在src文件下才能識別,且名字不能改
c3p0-config.xml代碼如下:
<?xml version="1.0" encoding="UTF-8" ?> <c3p0-config><!-- This is default config! --><default-config><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></default-config><!-- This is my config for mysql--><named-config name="mysql"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/customer</property><property name="user">root</property><property name="password">1234</property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></named-config> </c3p0-config>?
在Test.java中測試
package main;import com.mchange.v2.c3p0.ComboPooledDataSource;import java.sql.*; import java.sql.PreparedStatement; import java.sql.SQLException;/*** Created by Administrator on 2017/8/4.*/ public class Test {static ComboPooledDataSource cpds=null;static{//這里有個優(yōu)點,寫好配置文件,想換數(shù)據(jù)庫,簡單//cpds = new ComboPooledDataSource("oracle");//這是oracle數(shù)據(jù)庫cpds = new ComboPooledDataSource("mysql");//這是mysql數(shù)據(jù)庫 }/*** 獲得數(shù)據(jù)庫連接* @return Connection*/public static Connection getConnection(){try {return cpds.getConnection();} catch (SQLException e) {e.printStackTrace();return null;}}/*** 數(shù)據(jù)庫關(guān)閉操作* @param conn* @param pst* @param rs*/public static void close(Connection conn, PreparedStatement pst, ResultSet rs){if(rs!=null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if(pst!=null){try {pst.close();} catch (SQLException e) {e.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}/*** 測試DBUtil類* @param args*/public static void main(String[] args) {Connection conn=getConnection();try {PreparedStatement pst=conn.prepareStatement("SELECT * FROM t_customer");ResultSet rs=pst.executeQuery();while(rs.next()){System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));}} catch (SQLException e) {e.printStackTrace();}close(conn,null,null);}}?
轉(zhuǎn)載于:https://www.cnblogs.com/liushuncheng/p/7283671.html
總結(jié)
以上是生活随笔為你收集整理的c3p0数据库连接池+mysql数据库基本使用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 1853 MCMF
- 下一篇: 利用Java上手微服务架构