1. 程式人生 > >C3P0連線池和Druid連線池

C3P0連線池和Druid連線池

C3P0資料連線池:

所需要匯入jar包:

package utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
	//建立資料來源物件
	private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
	
	//獲取資料連線物件
	public static Connection getConnection() throws SQLException{
		return dataSource.getConnection();
	} 
	
	//關閉資料連線
	public static void closeAll(Connection conn,Statement stmt,ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	//獲取資料來源
	public static DataSource getDateSource(){
		return dataSource;
	}
}

C3P0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<!-- 如果程式碼中的config名稱不是下面的某一個,就會根據預設配置進行查詢,如果也沒有預設配置,則按照預設值配置進行查詢 -->
	<default-config>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="user">root</property>
		<property name="password">root</property>
		<property name="checkoutTimeout">30000</property>
		<property name="initialPoolSize">10</property>
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">100</property>
		<property name="minPoolSize">10</property>
	</default-config>
	<!-- 配置生產環境的資料庫 -->
	<named-config name="mysql_pro">
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="user">root</property>
		<property name="password">root</property>
		<property name="checkoutTimeout">30000</property>
		<property name="initialPoolSize">10</property>
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">100</property>
		<property name="minPoolSize">10</property>
	</named-config>
	<!-- 配置開發環境的資料庫 -->
	<named-config name="mysql_dev">
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="user">root</property>
		<property name="password">root</property>
		<property name="checkoutTimeout">30000</property>
		<property name="initialPoolSize">10</property>
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">100</property>
		<property name="minPoolSize">10</property>
	</named-config>
</c3p0-config>

Druid資料連線池:

所需jar包:

package com.igeekhome.igeekshop.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;

public class DBUtil {
	private static DruidDataSource ds;

	private static ThreadLocal<Connection> connection = new ThreadLocal<>();
	static {
		Properties prop = PropertiesUtil.loadProperties("jdbc.properties");
		ds = new DruidDataSource();
		ds.setDriverClassName(prop.getProperty("druid.driverClass"));
		ds.setUrl(prop.getProperty("druid.jdbcUrl"));
		ds.setUsername(prop.getProperty("druid.user"));
		ds.setPassword(prop.getProperty("druid.password"));
	}

	private DBUtil() {

	}

	public static DataSource getDs() {
		return ds;
	}

	public static Connection getConnection() throws SQLException {
		// 先從ThreadLocal中獲取連線物件
		Connection con = connection.get();
		// 1.如果有,則直接返回
		if (con != null) {
			return con;
		}
		// 2.如果沒有,則從連線池中獲取一個新的連線物件,並將該連線物件存入ThreadLocal中
		else {
			con = ds.getConnection();
			connection.set(con);
			return con;
		}
	}

	public static void close(Connection con) {
		if (con != null) {
			try {
				con.close();
				connection.set(null);
			} catch (SQLException e) {
			}
		}
	}
}

讀取properties檔案的工具類:

package com.igeekhome.igeekshop.util;

import java.io.IOException;
import java.util.Properties;

public class PropertiesUtil {
	private PropertiesUtil() {

	}

	public static Properties loadProperties(String fileName) {
		Properties prop = new Properties();
		try {
			prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return prop;
	}

}

jdbc.properties

druid.driverClass=com.mysql.jdbc.Driver
druid.jdbcUrl=jdbc:mysql://localhost:3306/igeekshop
druid.user=root
druid.password=root

總結:由於在Spring專案中,C3P0會有機率出現死鎖的現象,所以我還是比較傾向於阿里巴巴開發的Druid來連線資料庫。至於死鎖現象,是存線上程同時佔用兩個連線物件的問題。