package com.qdsx.drp.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
//ThreadLocal
private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>();
/**
* 得到Connection
* @return
*/
public static Connection getConnection(){
//ThreadLocal
Connection conn = connectionHolder.get();
//ThreadLocal,如果在当前线程中没有绑定相应的Connection
if(conn == null){
//取得jdbc配置信息
try {
JdbcInfo jdbcInfo = ConfigReader.getInstance().getJdbcInfo();
Class.forName(jdbcInfo.getDriverName()).newInstance();
conn = DriverManager.getConnection(jdbcInfo.getUrl(), jdbcInfo.getUsername(), jdbcInfo.getPassword());
//ThreadLocal,将Connection设置(set)到线程变量(connectionHolder)中
connectionHolder.set(conn);
} catch (InstantiationException e) {
e.printStackTrace();
throw new ApplicationException("系统错误,请联系系统管理员,qq:472989050");
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new ApplicationException("系统错误,请联系系统管理员,qq:472989050");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ApplicationException("系统错误,请联系系统管理员,qq:472989050");
} catch (SQLException e) {
e.printStackTrace();
throw new ApplicationException("系统错误,请联系系统管理员,qq:472989050");
}
}
return conn;
}
public static void closeConnection(){
Connection conn = connectionHolder.get();
if(conn != null){
try {
conn.close();
//从ThreadLocal中清除Connection (如果不清楚,下次拿到的还是已经close的Connection,用不了)
connectionHolder.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//以下是事务处理的封装
public static void beginTransaction(Connection conn) {
try {
if(conn != null){
if(conn.getAutoCommit()){
conn.setAutoCommit(false);//手动提交
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void commitTransaction(Connection conn) {
try {
if(conn != null){
if(!conn.getAutoCommit()){
conn.commit();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void rollbackTransaction(Connection conn) {
try {
if (conn != null) {
if(!conn.getAutoCommit()){
conn.rollback();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void resetConnection(Connection conn) {
try {
if(conn != null){
if(conn.getAutoCommit()){
conn.setAutoCommit(false);
}else{
conn.setAutoCommit(true);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void setAutoCommit(Connection conn, boolean autoCommit) {
if (conn != null) {
try {
conn.setAutoCommit(autoCommit);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public void addFlowCard(FlowCard flowCard) throws ApplicationException {
Connection conn = null;
try {
//取得
conn = ConnectionManager.getConnection();
//开启事务
ConnectionManager.beginTransaction(conn);
//生成流向单单号
String flowCardVouNo = flowCardDao.generateVouNo();
//添加流向单主信息
flowCardDao.addFlowCardMaster(flowCardVouNo, flowCard);
//添加流向单明细信息
flowCardDao.addFlowCardDetail(flowCardVouNo, flowCard.getFlowCardDetailList());
//提交事务
ConnectionManager.commitTransaction(conn);
} catch (DaoException e) {
//异常,回滚事务
ConnectionManager.rollbackTransaction(conn);
e.printStackTrace();
throw new ApplicationException("添加流向单失败!");
}finally{
//关闭Connection,并从ThreadLocal中清除Connection
ConnectionManager.closeConnection();
}
}
在dao层使用:
public String generateVouNo() throws DaoException {
Connection conn = ConnectionManager.getConnection();
return null;
}
public void addFlowCardMaster(String flowCardVouNo, FlowCard flowCard)
throws DaoException {
Connection conn = ConnectionManager.getConnection();
}
public void addFlowCardDetail(String flowCardVouNo,
List<FlowCardDetail> flowCardDetailList) throws DaoException {
Connection conn = ConnectionManager.getConnection();
}