4.JDBC ConnectionManager类,根据sys-config.xml,类JdbcConfig,类XmlConfigReader,连接数据库,释放资源等功能

package com.bjpowernode.drp.util;

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

/**
 * 这是一个管理JDBC connection的类 .
 * 浏览器的每一次访问服务器,服务器都会开启一个线程,即每一个request,对应一个线程.
 * 每一个线程即是一个ThreadLocal,放在ThreadLocal中的东西是属于同一线程共享的,所以在ThreadLocal放Connection,
 * 同一个request取出来的Connection是属于同一个的.
 * 
 * @author Kevin
 *
 */
public class ConnectionManager {
	
	private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>();
	
	/**
	 *  getConnection和connectionHolder.get()的区别
	 *  connectionHolder.get()是尝试从ThreadLocal中获取Connection,如果没有,返回null,如果有,直接返回.
	 *  getConnection也是尝试从ThreadLocal中获取Connection,如果没有,则创建一个,然后返回,如果有,直接返回.
	 */
	public static Connection getConnection() {
		
		Connection connection = connectionHolder.get();
		
		if (connection == null) {
			JdbcConfig jdbcConfig = XmlConfigReader.getInstance().getJdbcConfig();
			try {
				Class.forName(jdbcConfig.getDriverName());
				connection = DriverManager.getConnection(jdbcConfig.getUrl(), jdbcConfig.getUserName(), jdbcConfig.getPassword());
				connectionHolder.set(connection);
				
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				throw new ApplicationException("系统错误,请联系系统管理员!");
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new ApplicationException("系统错误,请联系系统管理员!");
			}
		}
		
		return connection;
	}
	
	/**
	 * Connection使用完毕,关闭
	 */
	public static void closeConnection() {
		// 尝试从ThreadLocal获取Connection,如果没有,关闭Connection失去意义.
		Connection connection = connectionHolder.get();
		
		if (connection != null) {
			try {
				connection.close();
				connectionHolder.remove();
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new ApplicationException("系统错误,请联系系统管理员!"); 
			}
		}
	}
	
	/**
	 * Statement使用完毕,关闭
	 */
	public static void closeStatement(Statement statement) {
		
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new ApplicationException("系统错误,请联系系统管理员!");
			}
		}
	}
	
	/**
	 * ResultSet使用完毕,关闭
	 */
	public static void closeResultSet(ResultSet resultSet) {
		
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new ApplicationException("系统错误,请联系系统管理员!");
			}
		}
	}
	
	/**
	 *  获取connection,把事务设置为手动提交,自己控制事务
	 */
	public static void manualCommitTransaction() {
		
		// 看看ThreadLocal中是否有Connection,如果没有(因为这是第一次跟Connection打交道,所以没有Connection),必须创建一个.
		// 如果此处不创建Connection,将无法保证事务.
		Connection connection = getConnection();
		
		if (connection != null) {
			try {
				if (connection.getAutoCommit()) {
					connection.setAutoCommit(false);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new ApplicationException("系统错误,请联系系统管理员!");
			}
		}
	}
	
	/**
	 * 一切操作正常,提交事务
	 */
	public static void commitTransaction() {
		// 尝试从ThreadLocal获取Connection,如果没有,提交事务失去意义.
		Connection connection = connectionHolder.get();
		
		if (connection != null) {
			try {
				if (!connection.getAutoCommit()) {
					connection.commit();
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new ApplicationException("系统错误,请联系系统管理员!");
			}
		}
	}
	
	/**
	 * 操作发生异常,回滚事务
	 */
	public static void rollbackTransaction() {
		// 尝试从ThreadLocal获取Connection,如果没有,回滚事务失去意义.
		Connection connection = connectionHolder.get();
		
		if (connection != null) {
			try {
				if (!connection.getAutoCommit()) {
					connection.rollback();
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new ApplicationException("系统错误,请联系系统管理员!");
			}
		}
	}
	
	/**
	 * 重置事务,如果事务是手动提交的,重置为自动提交,如果是自动提交的,重置为手动提交.
	 */
	public static void resetConnection() {
		// 尝试从ThreadLocal获取Connection,如果没有,重置事务失去意义.
		Connection connection = connectionHolder.get();
		if (connection != null) {
			try {
				if (connection.getAutoCommit()) {
					connection.setAutoCommit(false);
					
				} else {
					connection.setAutoCommit(true);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new ApplicationException("系统错误,请联系系统管理员!");
			}
		}
	}
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值