zoukankan      html  css  js  c++  java
  • JAVA 数据库切换 -1 (MYSQL 连接)后续加锁

    public class SqlMyDataSource implements DataSource{
        
        private static SqlMyDataSource instance = new SqlMyDataSource();
        private SqlMyDataSource() {
            
        }
        public static SqlMyDataSource getInstance() {
           
            return instance;
        }
        
        private static LinkedList<Connection> pool = new LinkedList<Connection>();
        private static  String DriverName = "com.mysql.jdbc.Driver";
        private static  String url = "";
        private static  String username = "";
        private static  String password = "";
        private static Connection conn = null;
        private static Logger logger = Logger.getLogger(SqlMyDataSource.class);
        
        static{
            try {
                             
                    ResourceBundle res = ResourceBundle.getBundle("uh");
                    url = res.getString("mysql.url"); 
                    username = res.getString("mysql.username");  
                    password = res.getString("mysql.password"); 
                    DriverName = res.getString("mysql.driver"); 
                    Class.forName(DriverName);              
                    conn = DriverManager.getConnection(url, username, password);                                
                logger.info("mysql connect successfully");      
            } catch (Exception e) {
                logger.info("mysql connect fail");       
                e.printStackTrace();
                throw new RuntimeException("mysql connect fail");
            }
        }
        
        @Override
        public Connection getConnection() throws SQLException {        
            if(conn == null || conn.isClosed()) {            
                conn = DriverManager.getConnection(url, username, password);
            }
            return conn;
        }
        
    
        @Override
        public PrintWriter getLogWriter() throws SQLException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void setLogWriter(PrintWriter out) throws SQLException {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void setLoginTimeout(int seconds) throws SQLException {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public int getLoginTimeout() throws SQLException {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public <T> T unwrap(Class<T> iface) throws SQLException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public boolean isWrapperFor(Class<?> iface) throws SQLException {
            // TODO Auto-generated method stub
            return false;
        }
    
    
        @Override
        public Connection getConnection(String username, String password) throws SQLException {
            // TODO Auto-generated method stub
            return null;
        }
    
      
    
    }
  • 相关阅读:
    IDEA常用快捷键
    mybatis动态sql总结
    端口被占用的问题解决 Web server failed to start. Port ×× was already in use
    java常见的面试题(二)
    java常见的面试题(一)
    Zookeeper学习总结
    Oracle 创建表空间及用户授权、dmp数据导入、表空间、用户删除
    Navicat远程连接服务器mysql
    HashMap的实现原理?如何保证HashMap线程安全?
    ArrayList和LinkedList内部是怎么实现的?他们之间的区别和优缺点?
  • 原文地址:https://www.cnblogs.com/chuangjie1988/p/14899629.html
Copyright © 2011-2022 走看看