您的位置:首页 > 数据库

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

2011-10-13 20:05 417 查看
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("系统错误,请联系系统管理员!");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐