小工具之使用c3p0获取Jdbc连接

简单的介绍关于利用c3p0连接池进行jdbc连接操作:


第一步:建立JdbcUtils类

package com.victorzhang.tools.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 使用本类方法,必须提供c3p0-config.xml配置文件
 * @author victor
 *
 */
public class JdbcUtils {
	//饿汉式
	public static DataSource ds = new ComboPooledDataSource();
	
	/**
	 * 它为null的时候表示没有事务
	 * 它不为null的时候表示有事务
	 * 当开始事务时,需要给它赋值
	 * 当事务结束时,需要给它赋值为null
	 * 并且在开始事务时,让dao的多个方法共享这个Connection
	 */
	
	private static ThreadLocal<Connection> thread = new ThreadLocal<Connection>();
	
	public static DataSource getDataSource(){
		return ds;
	}
	
	/**
	 * dao使用本方法获取conn
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		/*
		 * 如果有事务返回当前的conn
		 * 如果没有事务,通过连接池返回性的conn
		 */
		Connection conn = thread.get();//获取当前线程的事务连接
		if(conn != null) return conn;
		return ds.getConnection();
	}
	
	/**
	 * 开启事务
	 * @throws SQLException 
	 */
	public static void beginTransaction() throws SQLException {
		Connection conn = thread.get();//获取当前线程的事务连接
		if(conn != null) throw new SQLException("已经开始了事务不能重复开启");
		conn = ds.getConnection();//给conn赋值表示开启了事务
		conn.setAutoCommit(false);//设置为手动提交
		thread.set(conn);//将当前连接放到当前线程中
		
	}
	
	/**
	 * 提交事务
	 * @throws SQLException 
	 */
	public static void commitTranction() throws SQLException {
		Connection conn = thread.get();//获取当前线程的事务连接
		if(conn == null) throw new SQLException("没有事务,不能提交");
		conn.commit();//提交事务
		conn.close();//关闭连接
		conn = null;//表示事务结束
		thread.remove();
		
	}
	
	/**
	 * 回滚事务
	 * @throws SQLException 
	 */
	public static void rollbackTranscantion() throws SQLException {
		Connection conn = thread.get();//获取当前线程的事务连接
		if(conn == null) throw new SQLException("没有事务,不用回滚");
		conn.rollback();//事务回滚
		conn.close();//关闭连接
		conn = null;//表示事务结束
		thread.remove();
	}
	
	/**
	 * 释放Connection
	 * @throws SQLException 
	 */
	public static void  releaseConnection(Connection connection) throws SQLException {
		Connection conn = thread.get();//获取当前线程的事务连接
		if(connection != conn){//如果参数连接connection与当前事务连接conn不同的话,可以进行关闭
			if(connection != null && !connection.isClosed()){//如果参数连接没有关闭,关闭
				connection.close();
			}
		}
	}
}


第二步:关于对jdbc连接的测试类JdbcUtilsTest:

package com.victorzhang.tools.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

/**
 * JdbcUtils获取Connection
 * 底层使用了c3p0连接池
 * 还需要mysql驱动
 * @author victor
 *
 */
public class JdbcUtilsTest {

	/**
	 * 底层使用了c3p0连接池,还需要提供c3p0的配置文件
	 * @throws SQLException 
	 */
	@Test
	public void getConnection() throws SQLException {
		Connection conn = JdbcUtils.getConnection();
		System.out.println(conn);
		JdbcUtils.releaseConnection(conn);
		System.out.println(conn.isClosed());
	}
	
	/**
	 * JdbcUtils还提供了与事务相关的功能
	 */
	@Test
	public void testTranscation() {
		try{
			JdbcUtils.beginTransaction();//开启事务
			
			//具体操作
			
			JdbcUtils.commitTranction();//提交事务
			
			JdbcUtils.rollbackTranscantion();//回滚事务
		}catch(SQLException e){
			
		}
	}
	
}

第三步:需要注意的是c3p0连接池必须提供c3p0的配置文件(这里是我个人的配置文件,读者根据自己的数据库配置自行配置)

<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
	<default-config> 
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/goods</property>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="user">root</property>
		<property name="password">root</property>
		
		<property name="acquireIncrement">3</property>
		<property name="initialPoolSize">10</property>
		<property name="minPoolSize">2</property>
		<property name="maxPoolSize">10</property>
	</default-config>
</c3p0-config>

还有需要提供的一些jar文件,可以在我的资源中进行查找,也可以自己从网络中查找资源。分别有c3p0-0.9.2-pre1.jar,mchange-commons-0.2.jar,mysql-connector-java-5.1.28-bin.jar

猜你喜欢

转载自blog.csdn.net/zhangwei408089826/article/details/39958249
今日推荐