IbatisSqlMapSessionFactory.java
上传用户:liangcc
上传日期:2019-05-24
资源大小:4412k
文件大小:4k
源码类别:
WEB邮件程序
开发平台:
Java
- package com.softeem.ibatis3;
- import java.io.IOException;
- import java.io.Reader;
- import java.sql.SQLException;
- import com.ibatis.common.resources.Resources;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import com.ibatis.sqlmap.client.SqlMapClientBuilder;
- import com.ibatis.sqlmap.client.SqlMapException;
- import com.ibatis.sqlmap.client.SqlMapSession;
- /**
- * 创建SqlMapClient对象的工厂
- *
- * @author JianChen
- *
- */
- public class IbatisSqlMapSessionFactory {
- // 配置文件路径
- private static String CONFIG_FILE_LOCATION = "SqlMapConfig.xml";
- // 在线程中保护本地变量
- private final static ThreadLocal<SqlMapClient> threadLocalClient = new ThreadLocal<SqlMapClient>();
- //在线程中保护本地变量
- private final static ThreadLocal<SqlMapSession> threadLocalSession = new ThreadLocal<SqlMapSession>();
- // 备份(由于CONFIG_FILE_LOCATION这个静态变量,有可能在外面供系统显示,
- // 如:系统配置信息
- // 而其他线程又需要这个变量的值作为参数进行工作,如果在某个特定的场合一不小心修改这个静态变量的值
- // 就会导致正个应用程序崩溃
- // )
- private static String configFile = CONFIG_FILE_LOCATION;
- private static Reader reader;
- public IbatisSqlMapSessionFactory() {
- }
- // static {
- // try {
- // reader = Resources.getResourceAsReader(configFile);
- // } catch (IOException e) {
- // System.err.println("%%%% Error Geting Reader ! %%%%");
- // }
- // }
- /**
- * 获得SqlMapClient对象
- *
- * @return
- * @throws SQLException
- */
- public static SqlMapClient getSqlMapClient() throws SqlMapException{
- try {
- reader = Resources.getResourceAsReader(configFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- // 用本地变量保护的形式去拿SqlMapClient
- SqlMapClient client = threadLocalClient.get();
- if (client == null && !isExistCurrentConnectionBySqlMapClient(client)) {
- // 生成SqlMapClient
- client = SqlMapClientBuilder.buildSqlMapClient(reader);
- // 保护起来
- threadLocalClient.set(client);
- }
- return client;
- }
- /**
- * 判断当前Client是否存在连接
- *
- * @param client
- * @return
- */
- public static boolean isExistCurrentConnectionBySqlMapClient(
- SqlMapClient client) {
- try {
- return client != null ? (client.getCurrentConnection() != null ? true
- : false)
- : false;
- } catch (SQLException e) {
- System.err.println("%%%% Error Geting Current Connection ! %%%%");
- }
- return false;
- }
- /**
- * 关闭连接
- */
- public static void coloseClient() {
- SqlMapClient client = (SqlMapClient) threadLocalClient.get();
- threadLocalClient.set(null);
- if (client != null) {
- try {
- if (client.getCurrentConnection() != null
- && !client.getCurrentConnection().isClosed()) {
- client.getCurrentConnection().close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 获得SqlMapSession对象
- * @return
- * @throws SqlMapException
- */
- public static SqlMapSession getSqlMapSession() throws SqlMapException{
- SqlMapClient client = getSqlMapClient();
- SqlMapSession session = threadLocalSession.get();
- if(client!=null && session==null){
- session = client.openSession();
- threadLocalSession.set(session);
- }
- return session;
- }
- /**
- * 关闭SqlMapSession
- */
- public static void coloseSqlMapSession(){
- SqlMapSession session = threadLocalSession.get();
- threadLocalSession.set(null);
- if(session!=null){
- session.close();
- }
- }
- /**
- * 提供一个静态方法供外面拿到此Reader对象 开发人员可以定制或修改拿到的SqlMapClient 便于程序的可扩展
- *
- * @return
- */
- public static Reader getReader() {
- return reader;
- }
- }