ThreadLocal类:
用来对当前线程中的Connection进行封装,从而用事物来处理一组Connection操作,避免了对该组事物的同步,因为这样1个用户只对应他自己的ThreadLocal封装的Connection。例子如下:
//通过ThreadLocal类来封装Connection,使得在该用户下用到的都是同一Connection,而其他用户用到的又是另一个Connection。
public class ConnectionManager {
//建立ThreadLocal类
private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>();
//得到Connection的方法
public static Connection getConnection() {
Connection conn = connectionHolder.get(); //得到当前ThreadLocal中的Connection
if (conn == null) { //如果在当前线程中没有绑定相应的Connection
try {
JdbcConfig jdbcConfig = XmlConfigReader.getInstance().getJdbcConfig();
Class.forName(jdbcConfig.getDriverName());
conn = DriverManager.getConnection(jdbcConfig.getUrl(), jdbcConfig.getUserName(), jdbcConfig.getPassword());
//将Connection设置到ThreadLocal
connectionHolder.set(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ApplicationException("系统错误,请联系系统管理员");
} catch (SQLException e) {
e.printStackTrace();
throw new ApplicationException("系统错误,请联系系统管理员");
}
}
return conn;
}
//关闭Connection的方法
public static void closeConnection() {
Connection conn = connectionHolder.get();
if (conn != null) {
try {
conn.close();
//从ThreadLocal中清除Connection
connectionHolder.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public class FlowCardManagerImpl implements FlowCardManager {
private FlowCardDao flowCardDao;
public FlowCardManagerImpl() {
this.flowCardDao = (FlowCardDao)BeanFactory.getInstance().getDaoObject(FlowCardDao.class);
}
public void addFlowCard(FlowCard flowCard) throws ApplicationException {
Connection conn = null;
try {
conn = ConnectionManager.getConnection(); //取得Connection
conn.setAutoCommit(false); //开始手动提交事务
String flowCardVouNo = flowCardDao.generateVouNo(); //生成流向单单号
flowCardDao.addFlowCardMaster(flowCardVouNo, flowCard); //添加流向单主信息
flowCardDao.addFlowCardDetail(flowCardVouNo, flowCard.getFlowCardDetailList()); //添加流向单明细信息
conn.commit(); //提交事务
}catch(DaoException e) {
ConnectionManager.rollbackTransaction(conn); //回滚事务
throw new ApplicationException("添加流向单失败!");
}finally {
ConnectionManager.closeConnection(); //关闭Connection并从ThreadLocal中清除
}
}
}
用来对当前线程中的Connection进行封装,从而用事物来处理一组Connection操作,避免了对该组事物的同步,因为这样1个用户只对应他自己的ThreadLocal封装的Connection。例子如下:
//通过ThreadLocal类来封装Connection,使得在该用户下用到的都是同一Connection,而其他用户用到的又是另一个Connection。
public class ConnectionManager {
//建立ThreadLocal类
private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>();
//得到Connection的方法
public static Connection getConnection() {
Connection conn = connectionHolder.get(); //得到当前ThreadLocal中的Connection
if (conn == null) { //如果在当前线程中没有绑定相应的Connection
try {
JdbcConfig jdbcConfig = XmlConfigReader.getInstance().getJdbcConfig();
Class.forName(jdbcConfig.getDriverName());
conn = DriverManager.getConnection(jdbcConfig.getUrl(), jdbcConfig.getUserName(), jdbcConfig.getPassword());
//将Connection设置到ThreadLocal
connectionHolder.set(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ApplicationException("系统错误,请联系系统管理员");
} catch (SQLException e) {
e.printStackTrace();
throw new ApplicationException("系统错误,请联系系统管理员");
}
}
return conn;
}
//关闭Connection的方法
public static void closeConnection() {
Connection conn = connectionHolder.get();
if (conn != null) {
try {
conn.close();
//从ThreadLocal中清除Connection
connectionHolder.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public class FlowCardManagerImpl implements FlowCardManager {
private FlowCardDao flowCardDao;
public FlowCardManagerImpl() {
this.flowCardDao = (FlowCardDao)BeanFactory.getInstance().getDaoObject(FlowCardDao.class);
}
public void addFlowCard(FlowCard flowCard) throws ApplicationException {
Connection conn = null;
try {
conn = ConnectionManager.getConnection(); //取得Connection
conn.setAutoCommit(false); //开始手动提交事务
String flowCardVouNo = flowCardDao.generateVouNo(); //生成流向单单号
flowCardDao.addFlowCardMaster(flowCardVouNo, flowCard); //添加流向单主信息
flowCardDao.addFlowCardDetail(flowCardVouNo, flowCard.getFlowCardDetailList()); //添加流向单明细信息
conn.commit(); //提交事务
}catch(DaoException e) {
ConnectionManager.rollbackTransaction(conn); //回滚事务
throw new ApplicationException("添加流向单失败!");
}finally {
ConnectionManager.closeConnection(); //关闭Connection并从ThreadLocal中清除
}
}
}