1. 程式人生 > >JDBC資料來源(DataSource)的簡單實現

JDBC資料來源(DataSource)的簡單實現

package com.lavasoft.simpledatesource; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

import javax.sql.DataSource; 
import java.util.Collections; 
import java.util.LinkedList; 
import java.sql.Connection; 
import java.sql.SQLException; 
import java.sql.DriverManager; 
import
 java.io.PrintWriter; 

/** 
* 一個簡單的DataSource實現 

* @author leizhimin 2010-1-14 0:03:17 
*/
 
public class SimpleDateSource implements DataSource { 
        private static Log log = LogFactory.getLog(SimpleDateSource.class); 
        private static final String dirverClassName = "com.mysql.jdbc.Driver"
        private
 static final String url = "jdbc:mysql://127.0.0.1:3306/testdb"; 
        private static final String user = "root"
        private static final String pswd = "leizhimin"
        //連線池 
        private static LinkedList<Connection> pool = (LinkedList<Connection>) Collections.synchronizedList(new
 LinkedList<Connection>()); 
        private static SimpleDateSource instance = new SimpleDateSource(); 

        static { 
                try { 
                        Class.forName(dirverClassName); 
                } catch (ClassNotFoundException e) { 
                        log.error("找不到驅動類!", e); 
                } 
        } 

        private SimpleDateSource() { 
        } 

        /** 
         * 獲取資料來源單例 
         * 
         * @return 資料來源單例 
         */
 
        public SimpleDateSource instance() { 
                if (instance == null) instance = new SimpleDateSource(); 
                return instance; 
        } 

        /** 
         * 獲取一個數據庫連線 
         * 
         * @return 一個數據庫連線 
         * @throws SQLException 
         */
 
        public Connection getConnection() throws SQLException { 
                synchronized (pool) { 
                        if (pool.size() > 0) return pool.removeFirst(); 
                        else return makeConnection(); 
                } 
        } 

        /** 
         * 連線歸池 
         * 
         * @param conn 
         */
 
        public static void freeConnection(Connection conn) { 
                pool.addLast(conn); 
        } 

        private Connection makeConnection() throws SQLException { 
                return DriverManager.getConnection(url, user, pswd); 
        } 

        public Connection getConnection(String username, String password) throws SQLException { 
                return DriverManager.getConnection(url, username, password); 
        } 

        public PrintWriter getLogWriter() throws SQLException { 
                return null
        } 

        public void setLogWriter(PrintWriter out) throws SQLException { 

        } 

        public void setLoginTimeout(int seconds) throws SQLException { 

        } 

        public int getLoginTimeout() throws SQLException { 
                return 0; 
        } 

        public <T> T unwrap(Class<T> iface) throws SQLException { 
                return null
        } 

        public boolean isWrapperFor(Class<?> iface) throws SQLException { 
                return false
        } 
}