君子博学而日参省乎己 则知明而行无过矣

博客园 首页 新随笔 联系 订阅 管理
JDBCContext类主要解决数据连接的问题
public class JDBCContext {
    
    private static final Log log = LogFactory.getLog(JDBCContext.class);
    //private Database database;
    private DataSource datasource;
    protected Connection connection;
    private boolean isValid = true;
    private static ThreadLocal<JDBCContext> jdbcContext;
    /**
     * 私有构造函数
     * 
@param database
     
*/
    private JDBCContext(DataSource datasource){        
        this.datasource = datasource;
        createConnection();        
    }
    public static JDBCContext getJdbcContext(DataSource datasource)
    {        
        
        if(jdbcContext==null)jdbcContext=new JDBCContextThreadLocal(datasource);
        
        JDBCContext context = (JDBCContext) jdbcContext.get();
        if (context == null) {
            
            context = new JDBCContext(datasource);
            
        }
        return context;
    }
    
    private void createConnection(){        
        try{
            
            if(connection == null || connection.isClosed() == true){
                log.debug("Create a connection");
                
                //connection=datasource.getConnection();
                
//isValid = true;
                
                if(datasource != null){
                    
                        //Class.forName(datasource.getdBType().getDriverclass()).newInstance();
                        
//connection = DriverManager.getConnection(datasource.getUrl(), datasource.getUser(),datasource.getPassword());
                    connection=datasource.getConnection();
                    isValid = true;
                    
                }
                else{
                    log.error("DataSource is invalid");
                    isValid = false;
                    return;
                }
            }else{
                log.debug("The current connection is valid.");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
    
    /**
     * 释放当前连接;
     
*/
    public void releaseConnection(){
        try{
            if(connection != null && connection.isClosed() == false){
                connection.close();
                // 添加代码 by chenying                
                jdbcContext.remove();
                isValid = false;
                log.debug("The connection is closed.");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    
    /**
     * 
@return
     
*/
    public Connection getConnection(){
        
        log.debug("Get a Connection!");
        try{
        if(connection==null || connection.isClosed())createConnection();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return connection;
    }
    /**
     * 
@return
     
*/
    public boolean isValid(){
        return isValid;
    }
    
    
    /**
     * 内部类
     * 
@author Administrator
     *
     
*/
    private static class JDBCContextThreadLocal extends ThreadLocal<JDBCContext> {
        public DataSource datasource;
        public JDBCContextThreadLocal(DataSource datasource)
        {
            this.datasource=datasource;
        }
        protected synchronized JDBCContext initialValue() {            
            return new JDBCContext(datasource);            
        }
    }

}
posted on 2012-09-23 02:03  刺猬的温驯  阅读(441)  评论(0编辑  收藏  举报