• 数据库访问 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);            
            }
        }

    }
  • 相关阅读:
    Nginx 配置请求响应时间
    数论筛法小结
    梅田湖种田划水摸鱼记
    好题
    奇技淫巧 (不定期更新)
    随机化算法小结(Miller Rabin,Pollard Rho, 模拟退火, 随机化贪心)
    题解 P6918 [ICPC2016 WF]Branch Assignment
    P2605 [ZJOI2010]基站选址解题思路
    题解 BZOJ 3156 防御准备
    Flutter大坑 Your Xcode project requires migration 报错大坑
  • 原文地址:https://www.cnblogs.com/chenying99/p/2698644.html
Copyright © 2020-2023  润新知