JDBC數據源(DataSource)的簡單實現

JDBC數據源(DataSource)的簡單實現
 
數據源技術是Java操做數據庫的一個很關鍵技術,流行的持久化框架都離不開數據源的應用。
 
數據源提供了一種簡單獲取數據庫鏈接的方式,並能在內部經過一個池的機制來複用數據庫鏈接,這樣就大大減小建立數據庫鏈接的次數,提升了系統性能。
 
對於數據源的應用,通常都選擇實用開源的數據源或數據庫鏈接池來使用,好比,常見的有DBCP、C3P0、Proxool等等。但用起來有些笨重和麻煩。下面本身手動實現個精簡的數據源,代碼以下:
 
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;
        }
}
 
這個數據源的實現雖然很簡陋,總代碼量不到百行,卻基本上實現了數據源的全部功能,達到了提升Connection複用的目的。
 
若是你想作的更復雜些,作個配置文件,
配置數據庫鏈接信息
寫個後臺線程監控鏈接池的Connection超時、被強制關閉、池的尺寸、當前大小等等。
再完善下數據源的log相關方法的實現。
功能就很強大了。
 
歡迎參與完善!
相關文章
相關標籤/搜索