JDBC数据库连接池的实现:第一天:数据源配置解析工具

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云解析 DNS,旗舰版 1个月
云解析DNS,个人版 1个月
简介:
+关注继续查看
开源的连接池很多,实现都不错,可是就不知道怎么做的,在研读了javax.sql包中的API规范后,想手动实现一个通用的JDBC数据库连接池。这个工作量比较大,主要目的是为了深入底层学习Java处理数据库的技术。
 
第一天:数据源配置解析工具
 
这个解析工具支持xml配置和properties配置,程序不依赖任何jdk以外的任何第三方包。
 
xml和properties文件的格式对名字有限制,xml还对格式有限制,要求符合sun公司的[url]http://java.sun.com/dtd/properties.dtd[/url]规范。
 
解析工具要完成的功能,读取配置文件,并存放到Map中,对于非必须而没有配置的属性,给以默认的属性值。
 
要求:两种配置文件读取方法通用,不进行切换。有导出配置文件的功能。
 
实现代码:
package com.blog51cto.lavasoft.ds; 

import java.util.*; 
import java.util.regex.Pattern; 
import java.util.regex.Matcher; 
import java.io.InputStream; 

/** 
* Created by IntelliJ IDEA. 
* User: leizhimin 
* Date: 2008-3-19 23:22:38 
* Company: LavaSoft([url]http://lavasoft.blog.51cto.com[/url]) 
* 数据源配置工具 
*/
 
public class DataSourceConfig { 
    private static Map<String, String> defcfgmap; 

    //初始化默认配置参数 
    static { 
        defcfgmap = new HashMap<String, String>(); 
        defcfgmap.put("driverClassName", ""); 
        defcfgmap.put("url", ""); 
        defcfgmap.put("user", ""); 
        defcfgmap.put("password", ""); 
        defcfgmap.put("type""javax.sql.DataSource"); 
        defcfgmap.put("initCreate""12"); 
        defcfgmap.put("maxIdle""8"); 
        defcfgmap.put("maxWait""10"); 
        defcfgmap.put("maxActive""5"); 
        defcfgmap.put("validationQuery", ""); 
    } 

    /** 
     * 获取数据源配置信息,本方法接受两种配置文件类型,xml和properties类型. 
     * 
     * @param cfgFilePath 配置文件的名 
     * @return 配置参数Map 
     * @throws Exception 
     */
 
    public static Map<String, String> getConfigParameter(String cfgFilePath) throws Exception { 
        Map<String, String> mcfg = new HashMap<String, String>(); 
        Pattern pp = Pattern.compile(".+properties"); 
        Pattern px = Pattern.compile(".+xml"); 
        Matcher mp = pp.matcher(cfgFilePath); 
        Matcher mx = px.matcher(cfgFilePath); 
        InputStream in = DataSourceConfig.class.getResourceAsStream(cfgFilePath); 
        Properties prop = new Properties(); 
        if (mp.matches()) { 
            prop.load(in); 
            Enumeration<String> enu = (Enumeration<String>) prop.propertyNames(); 
            while (enu.hasMoreElements()) { 
                String key = enu.nextElement(); 
                String value = prop.getProperty(key); 
                mcfg.put(key, value); 
            } 
            in.close(); 
        } else if (mx.matches()) { 
            prop.loadFromXML(in); 
            Enumeration<String> enu = (Enumeration<String>) prop.propertyNames(); 
            while (enu.hasMoreElements()) { 
                String key = enu.nextElement(); 
                String value = prop.getProperty(key); 
                mcfg.put(key, value); 
            } 
            in.close(); 
        } else { 
            throw new Exception("数据源配置文件必须为xml或者properties文件,请检查配置!"); 
        } 
        //检查没有配置的属性,并通过默认的属性值进行设置 
        Set<Map.Entry<String, String>> def = defcfgmap.entrySet(); 
        for (Map.Entry<String, String> d : def) { 
            if (!mcfg.containsKey(d.getKey())) { 
                mcfg.put(d.getKey(), d.getValue()); 
            } 
        } 
        return mcfg; 
    } 

    /** 
     * 获取默认的数据源配置信息 
     * 
     * @return 
     */
 
    private static String getDefaultConfig() { 
        return getConfig(defcfgmap); 
    } 

    /** 
     * 返回数据源配置信息的文本表述 
     * 
     * @param map 配置信息Map 
     * @return 数据源配置信息的文本表述 
     */
 

    private static String getConfig(Map<String, String> map) { 
        StringBuffer sb = new StringBuffer("数据源配置参数:\n"); 
        for (Map.Entry<String, String> ent : map.entrySet()) { 
            sb.append(ent.getKey()).append("=").append(ent.getValue()).append("\n"); 
        } 
        return sb.toString(); 
    } 

    /** 
     * 测试配置参数获取工具 
     * 
     * @param args 
     * @throws Exception 
     */
 
    public static void main(String args[]) throws Exception { 
        Map<String, String> mcfg = getConfigParameter("dscfg.xml"); 
        String cfg = getConfig(mcfg); 
        System.out.println(cfg); 
    } 
}
 
 
配置文件
dscfg.properties
driverClassName=com.mysql.jdbc.Driver 
url=jdbc:mysql://127.0.0.1:3306/mysql 
user=root 
password=leizhimin 
 
dscfg.xml
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 
<properties> 
<comment>JDBC数据库连接池实现</comment> 
<entry key="user">root</entry> 
<entry key="password">leizhimin</entry> 
<entry key="url">jdbc:mysql://127.0.0.1:3306/mysql</entry> 
<entry key="driverClassName">com.mysql.jdbc.Driver</entry> 
</properties>
 
 
现有的方法都测试过了,还没有写导出配置的方法,接下来我会补上。


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/66960,如需转载请自行联系原作者
相关文章
|
1月前
|
JavaScript IDE 前端开发
TypeScript 提供了丰富的工具和框架来提高开发效率。具体应用案例解析
TypeScript 提供了丰富的工具和框架来提高开发效率。具体应用案例解析
|
2月前
|
C++ 计算机视觉
实用分享-Dependencies(DLL解析工具)
实用分享-Dependencies(DLL解析工具)
|
2月前
|
存储 网络协议 开发工具
[笔记]深入解析Windows操作系统《一》概念和工具(二)
[笔记]深入解析Windows操作系统《一》概念和工具(二)
|
2月前
|
存储 安全 API
[笔记]深入解析Windows操作系统《一》概念和工具(一)
[笔记]深入解析Windows操作系统《一》概念和工具
|
3月前
|
JavaScript
如何使用 multiparty 工具库在 Node.js 应用里解析 multipart form-data 格式的请求
如何使用 multiparty 工具库在 Node.js 应用里解析 multipart form-data 格式的请求
28 1
|
3月前
|
监控 Oracle 数据可视化
深度解析JVM性能监控工具:推荐与详细用法
深度解析JVM性能监控工具:推荐与详细用法
146 0
|
4月前
|
Web App开发 Java 开发工具
systrace: 系统级跟踪工具的解析
systrace是Android4.1版本之后推出的,对系统Performance分析的工具,该工具结合Android 内核的数据,最终会生产html文件。 systrace的功能包括跟踪系统的I/O操作、内核工作队列、CPU负载以及Android各个子系统的运行状况等
|
4月前
|
人工智能 自然语言处理
精细解析中文公司名称:智能分词工具助力地名、品牌名、行业词和后缀提取
精细解析中文公司名称:智能分词工具助力地名、品牌名、行业词和后缀提取
 精细解析中文公司名称:智能分词工具助力地名、品牌名、行业词和后缀提取
|
4月前
在线JWT Token解析解码工具
在线JWT Token解析解码工具
468 0
|
5月前
|
Prometheus 运维 Kubernetes
Kubernetes 故障排查工具- Robusta 解析
Hello folks,我是 Luga,今天我们来分享一款用于 Kubernetes Cluster 故障排查的开源工具 - Robusta (罗布斯塔)。作为一个用于多集群 Kubernetes 监控、故障排除和自动化的开源平台,就像 Docker 用于部署应用程序的基础设施即代码一样,Robusta 用于维护 Kubernetes Cluster 应用程序和处理其警报的基础设施即代码。
107 0
推荐文章
更多
推荐镜像
更多