数据库访问 threadlocal模式[参考easydbo]

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);            
        }
    }

}

你可能感兴趣的:(ThreadLocal模式)