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相关方法的实现。
功能就很强大了。
 
欢迎参与完善!