數據庫連接池

轉自 http://blog.csdn.net/xinxinqiu/article/details/17531449 public class ConnUtils3

 

public class ConnUtils3 {

private static LinkedList<Connection> pool = new LinkedList<Connection>();  

    static {  

        try {  

            // 聲明資源器類 -   

            Properties prop = new Properties();  

            // 獲取這個文件的路徑  

            URL url = ConnUtils3.class.getClassLoader().getResource(  

                    "jdbc.properties");  

            // ConnUtils3.class.getResource("jdbc.properties")獲取的是同目錄下  

            String path = url.getPath();// 運行是路徑  

            // 爲了防止有中文或是空格  

            path = URLDecoder.decode(path, "UTf-8");// 反向將url編碼過的路徑轉換回來  

            File file = new File(path);  

            // 加載jdbc.properties這個文件  

            prop.load(new FileInputStream(file));  

            // 獲取信息  

            String driver = prop.getProperty("driver");  

            Class.forName(driver);  

            String jdbcurl = prop.getProperty("url");  

            String nm = prop.getProperty("name");  

            String pwd = prop.getProperty("pwd");  

            // 創建三個原生的連接,都將它們代理  

            String poolSize = prop.getProperty("poolSize");  

            int size = Integer.parseInt(poolSize);  

            for (int i = 0; i < size; i++) {  

                final Connection con = DriverManager.getConnection(jdbcurl, nm,  

                        pwd);  

                // 對con進行動態代理  

                Object proxyedObj = Proxy.newProxyInstance(  

                        ConnUtils3.class.getClassLoader(),  

                        new Class[] { Connection.class },  

                        new InvocationHandler() {  

                            public Object invoke(Object proxy, Method method,  

                                    Object[] args) throws Throwable {  

                                // 是否是close方法  

                                if (method.getName().equals("close")) {  

                                    synchronized (pool) {  

                                        pool.addLast((Connection) proxy);//鏈接往最後放速度最快  

                                        pool.notify();  

                                    }  

                                    return null;// 如果調用的是close則不會調用被代理類的方法。  

                                }  

                                return method.invoke(con, args);  

                            }  

                        });  

                // 將代理對象放到pool中  

                pool.add((Connection) proxyedObj);  

            }  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

    }  

  

    public static Connection getConn() {  

        synchronized (pool) {  

            if (pool.size() == 0) {  

                try {  

                    pool.wait();  

                } catch (InterruptedException e) {  

                    e.printStackTrace();  

                }  

                return getConn();//線程喚醒之後遞歸繼續取鏈接  

            } else {  

                Connection con = pool.removeFirst();//取出第一個速度快  

                System.err.println("還有幾個:" + pool.size());  

                return con;  

            }  

        }  

    }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章