有问题请联系管理员elsie@fanruan.com

Page tree
Skip to end of metadata
Go to start of metadata

 

 

package com.fr.data.impl;

import com.fr.base.TemplateUtils;
import com.fr.data.pool.DBCPConnectionPoolAttr;
import com.fr.data.pool.MemoryConnection;
import com.fr.general.ComparatorUtils;
import com.fr.general.FRLogger;
import com.fr.general.Inter;
import com.fr.json.JSONException;
import com.fr.json.JSONObject;
import com.fr.stable.CodeUtils;
import com.fr.stable.StringUtils;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLableReader;
import holder.ConfigHolder;
import holder.NameSpaceWrapper;
import holder.factory.ConfigHolderFactory;
import holder.impl.ObjectHolder;

import java.sql.Connection;

/**
 * JDBC Database Connection
 */
public class JDBCDatabaseConnection extends AbstractDatabaseConnection {
   private ConfigHolder<String> driver = HolderFactory.simpleHolder("driver", "sun.jdbc.odbc.JdbcOdbcDriver", getNameSpace());
	private ConfigHolder<String> url = HolderFactory.simpleHolder("url", StringUtils.EMPTY, getNameSpace());
	private ConfigHolder<String> user = HolderFactory.simpleHolder("user", StringUtils.EMPTY, getNameSpace());
	private ConfigHolder<String> password = HolderFactory.simpleHolder("password", StringUtils.EMPTY, getNameSpace());
	private ConfigHolder<Boolean> encryptPassword = HolderFactory.simpleHolder("encryptPassword", true, getNameSpace());
	private ConfigHolder<DBCPConnectionPoolAttr> dbcpAttr = HolderFactory.objectHolder("dbcpAttr", null, DBCPConnectionPoolAttr.class, getNameSpace());

    /**
     * Constructor.
     */
    public JDBCDatabaseConnection() {
        this(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY);
    }

    /**
     * Constructor.
     */
    public JDBCDatabaseConnection(String driver, String url,
                                  String user, String password) {
        this.setDriver(driver);
        this.setURL(url);
        this.setUser(user);
        this.setPassword(password);
    }

}

改写部分DataSourcemanger

public class DataSourceRefactor extends Configuration{
    private ConfigHolder<ConnectionN> nameConnectionMap = HolderFactory.objectHolder("nameConnectionMap", new ConnectionN(), ConnectionN.class, getNameSpace());
	public abstract static class N extends UniqueKey {
    MapHolder<Map> map = HolderFactory.mapHolder("map", new ListMap(), null, getNameSpace()); // <String, <T extends XMLable>>

    public N() {
    }

	public String getNameSpace(){
	return "datasource";
}
    /**
     * 获取map中保存的键值对的个数
     *
     * @return 数量
     */
    public int size() {
        return map.get().size();
    }

    /**
     * 返回名字的迭代器
     *
     * @return
     */
    public java.util.Iterator getNameIterator() {
        return this.map.get().keySet().iterator();
    }

    /**
     * 根据名称返回保存的内容
     *
     * @param name 名称
     * @return
     */
    public XMLable getValueByName(String name) {
        return (XMLable) this.map.get(name);
    }

    /**
     * 添加值
     *
     * @param name  名称
     * @param value 值
     */
    public void putValueByName(String name, XMLable value) {
        if (value == null) {
            this.map.remove(name);
        } else {
            this.map.put(name, value);
        }
    }

    /**
     * 重命名
     *
     * @param oldName 旧名字
     * @param newName 新名字
     * @return 重命名成功
     */
    public boolean rename(String oldName, String newName) {
        map.rename(oldName, newName);
        return true;
    }

    /**
     * 根据名称删除对象
     *
     * @param name 对象名
     */
    public void removeValueByName(String name) {
        this.map.remove(name);
    }

    /**
     * 清空对象
     */
    public void clearAll() {
        this.map.clearAll();
    }

}

public static class ConnectionN extends N {
    public ConnectionN() {
    }

}
public static void main(String[] args) {
    DataSourceRefactor dataSourceRefactor = new DataSourceRefactor();
    DBCPConnectionPoolAttr dbcpConnectionPoolAttr = new DBCPConnectionPoolAttr();
    dbcpConnectionPoolAttr.setInitialSize(111);
    dbcpConnectionPoolAttr.setMaxActive(111);
    dbcpConnectionPoolAttr.setMaxIdle(111);
    dbcpConnectionPoolAttr.setMaxWait(111);
    dbcpConnectionPoolAttr.setMinIdle(111);
    dbcpConnectionPoolAttr.setMinEvictableIdleTimeMillis(111);
    dbcpConnectionPoolAttr.setNumTestsPerEvictionRun(111);
    dbcpConnectionPoolAttr.setTimeBetweenEvictionRunsMillis(111);
    dbcpConnectionPoolAttr.setValidationQuery("heihei");
    dbcpConnectionPoolAttr.setTestOnBorrow(false);
    dbcpConnectionPoolAttr.setTestOnReturn(false);
    dbcpConnectionPoolAttr.setTestWhileIdle(false);
    JDBCDatabaseConnection jndiDatabaseConnection = new JDBCDatabaseConnection();
    jndiDatabaseConnection.setDbcpAttr(dbcpConnectionPoolAttr);
    jndiDatabaseConnection.setUser("2222");
    jndiDatabaseConnection.setURL("fdsfafasfasf");
    jndiDatabaseConnection.setPassword("sfsafsafhbf");
    dataSourceRefactor.putConnection("chen", jndiDatabaseConnection);
    JNDIDatabaseConnection jndiDatabaseConnection1 = new JNDIDatabaseConnection();
    jndiDatabaseConnection1.setJNDIName("liang");
    Hashtable hashtable = new Hashtable();
    hashtable.put("1", "1");
    hashtable.put("2", "2");
    jndiDatabaseConnection1.setContextHashtable(hashtable);
    dataSourceRefactor.putConnection("liang", jndiDatabaseConnection1);
}
}
 

执行main函数,数据库中存储格式如下

 

 

public class DBCPConnectionPoolAttr extends UniqueKey implements XMLReadable, Cloneable, java.io.Serializable {

    private static final int MAX_ACTIVE = 50;
    private static final int MAX_IDLE = 10;
    private static final int MAX_WAIT = 10000;
    private static final int MIN_EVICTABLE_IDLE_TIME_MILLIS = 1000 * 60 * 30;

    private ConfigHolder<Integer> initialSize = HolderFactory.simpleHolder("initialSize", 0, getNameSpace());
// 最大活动连接:连接池在同一时间能够分配的最大活动连接的数量, 如果设置为非正数则表示不限制
private ConfigHolder<Integer> maxActive = HolderFactory.simpleHolder("maxActive", 50, getNameSpace());
// 最大空闲连接:连接池中容许保持空闲状态的最大连接数量,超过的空闲连接将被释放,如果设置为负数表示不限制
private ConfigHolder<Integer> maxIdle = HolderFactory.simpleHolder("maxIdle", 10, getNameSpace());
// 最小空闲连接:连接池中容许保持空闲状态的最小连接数量,低于这个数量将创建新的连接,如果设置为0则不创建
private ConfigHolder<Integer> minIdle = HolderFactory.simpleHolder("minIdle", 0, getNameSpace());
// 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间则抛出异常,如果设置为-1表示无限等待
private ConfigHolder<Integer> maxWait = HolderFactory.simpleHolder("maxWait", 10000, getNameSpace());
// SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.如果指定,则查询必须是一个SQL SELECT并且必须返回至少一行记录
private ConfigHolder<String> validationQuery = HolderFactory.simpleHolder("validationQuery", StringUtils.EMPTY, getNameSpace());
// 指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个.
// 注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串
private ConfigHolder<Boolean> testOnBorrow = HolderFactory.simpleHolder("testOnBorrow", true, getNameSpace());
// 指明是否在归还到池中前进行检验
// 注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串
private ConfigHolder<Boolean> testOnReturn = HolderFactory.simpleHolder("testOnReturn", true, getNameSpace());
// 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.
// 注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串
private ConfigHolder<Boolean> testWhileIdle = HolderFactory.simpleHolder("testWhileIdle", false, getNameSpace());
// 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. 如果设置为非正数,则不运行空闲连接回收器线程
private ConfigHolder<Integer> timeBetweenEvictionRunsMillis = HolderFactory.simpleHolder("timeBetweenEvictionRunsMillis", -1, getNameSpace());
// 在每次空闲连接回收器线程(如果有)运行时检查的连接数量
private ConfigHolder<Integer> numTestsPerEvictionRun = HolderFactory.simpleHolder("numTestsPerEvictionRun", 3, getNameSpace());
// 连接在池中保持空闲而不被空闲连接回收器线程(如果有)回收的最小时间值,单位毫秒
private ConfigHolder<Integer> minEvictableIdleTimeMillis = HolderFactory.simpleHolder("minEvictableIdleTimeMillis", 1000 * 60 * 30, getNameSpace());

    public int getInitialSize() {
        return initialSize.get();
    }

    public void setInitialSize(int initialSize) {
        this.initialSize.set(initialSize);
    }

    public int getMaxActive() {
        return maxActive.get();
    }

    public DBCPConnectionPoolAttr() {
    }

    /**
     * 设置
     *
     * @param maxActive
     */
    public void setMaxActive(int maxActive) {
        this.maxActive.set(maxActive);
    }

    public int getMaxIdle() {
        return maxIdle.get();
    }

    /**
     * @param maxIdle
     */
    public void setMaxIdle(int maxIdle) {
        this.maxIdle.set(maxIdle);
    }

    public int getMinIdle() {
        return minIdle.get();
    }

    public void setMinIdle(int minIdle) {
        this.minIdle.set(minIdle);
    }

    public int getMaxWait() {
        return maxWait.get();
    }

    public void setMaxWait(int maxWait) {
        this.maxWait.set(maxWait);
    }

    public String getValidationQuery() {
        return validationQuery.get();
    }

    public void setValidationQuery(String validationQuery) {
        this.validationQuery.set(validationQuery);
    }

    /**
     * 是否在从池中取出连接前进行检验
     *
     * @return 实则返回true
     */
    public boolean isTestOnBorrow() {
        return testOnBorrow.get();
    }

    public void setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow.set(testOnBorrow);
    }

    /**
     * 是否在归还到池中前进行检验
     *
     * @return 是则返回true
     */
    public boolean isTestOnReturn() {
        return testOnReturn.get();
    }

    public void setTestOnReturn(boolean testOnReturn) {
        this.testOnReturn.set(testOnReturn);
    }

    /**
     * 连接是否被空闲连接回收器(如果有)进行检验
     *
     * @return 是则返回true
     */
    public boolean isTestWhileIdle() {
        return testWhileIdle.get();
    }

    public void setTestWhileIdle(boolean testWhileIdle) {
        this.testWhileIdle.set(testWhileIdle);
    }

    public int getTimeBetweenEvictionRunsMillis() {
        return timeBetweenEvictionRunsMillis.get();
    }

    public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis.set(timeBetweenEvictionRunsMillis);
    }

    public int getNumTestsPerEvictionRun() {
        return numTestsPerEvictionRun.get();
    }

    public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
        this.numTestsPerEvictionRun.set(numTestsPerEvictionRun);
    }

    public int getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis.get();
    }

    public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis.set(minEvictableIdleTimeMillis);
    }
}

 

 

  • No labels
Write a comment…