总体思路:
首先从XML中读取配置,拿到每个MYSQL的链接配置,然后在Context中初始化每个Node配置。在DataBasePool中为每个Node建立连接池
一:AbstractServerNode
定义了通用配置参数
package serverNode;
/**
* define common db connection property
* @author jason
*
*/
public class AbstractServerNode {
//common
private String driver;
private int port;
private String schema;
private String user;
private String password;
public AbstractServerNode(){};
public AbstractServerNode(String driver,String user,String password,int port,String schema){
this.driver = driver;
this.port = port;
this.schema = schema;
this.user = user;
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
}
二:ServerNode
定义每一个节点的具体链接
package serverNode;
/**
* single ip_address and abstract_server_node
* @author Jason
*
*/
public class ServerNode{
private String node_name;
private String ip_address;
private AbstractServerNode abstract_server_node;
private String jdbcUrl;
public String getIp_address() {
return ip_address;
}
public void setIp_address(String ip_address) {
this.ip_address = ip_address;
}
public AbstractServerNode getSc() {
return abstract_server_node;
}
public void setSc(AbstractServerNode sc) {
this.abstract_server_node = sc;
}
public String getJdbcUrl() {
return "jdbc:mysql://"+getIp_address()+":"+this.getSc().getPort()+"/"+this.getSc().getSchema();
}
public void setJdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
}
public String getNode_name() {
return node_name;
}
public void setNode_name(String node_name) {
this.node_name = node_name;
}
}
三:ServerNodeContext
用以初始化XML中定义的ServerNode
返回一个ServerNode List
package dataSourceContext;
import java.util.List;
import java.util.Map;
import serverNode.AbstractServerNode;
import serverNode.ServerNode;
import xmlParese.XMLParese;
/**
* get Server_Nodes Pool
* @author Jason
*
*/
public class ServerNodeContext {
private List<ServerNode> server_nodes;
public void init() {
XMLParese xml_parese = new XMLParese("C:/Users/Jason/Desktop/Document/dbSource.xml");
AbstractServerNode sc = new AbstractServerNode();
Map<String,String> abstract_sc =xml_parese.getAbstract_server_config();
sc.setDriver(abstract_sc.get("mysqlDriver"));
sc.setPassword(abstract_sc.get("password"));
sc.setPort(Integer.parseInt(abstract_sc.get("port")));
sc.setSchema(abstract_sc.get("schema"));
sc.setUser(abstract_sc.get("user"));
server_nodes = xml_parese.getServer_nodes();
// System.out.println(server_nodes.size());
for(int i=0;i<server_nodes.size();i++){
server_nodes.get(i).setSc(sc);
}
}
/*public void init(){
xml_parese = new XMLParese("C:/Users/Jason/Desktop/Document/dbSource.xml");
ServerConfig sc = new ServerConfig();
Map<String,String> abstract_sc =xml_parese.getAbstract_server_config();
sc.setDriver(abstract_sc.get("mysqlDriver"));
sc.setPassword(abstract_sc.get("password"));
sc.setPort(Integer.parseInt(abstract_sc.get("port")));
sc.setSchema(abstract_sc.get("schema"));
sc.setUser(abstract_sc.get("user"));
server_nodes = xml_parese.getServer_nodes();
// System.out.println(server_nodes.size());
for(int i=0;i<server_nodes.size();i++){
server_nodes.get(i).setSc(sc);
}
}
*/
public List<ServerNode> getServer_nodes() {
return server_nodes;
}
}
四:DataBasePool
为每一个ServerNode配置数据库连接池
以Map<String,ComboPooledDataSource>返回
Key:为XML配置的node节点名称
Value:为数据库连接池对象
package dataBasePool;
import java.beans.PropertyVetoException;
import java.util.List;
import java.util.Map;
import serverNode.ServerNode;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import dataSourceContext.ServerNodeContext;
/**
* init db_pool for each server node(jdbcURL)
* @author Jason
*
*/
public class DataBasePool {
private static Map<String,ComboPooledDataSource> pools;
static{
List<ServerNode> server_nodes = new ServerNodeContext().getServer_nodes();
ComboPooledDataSource cpds = null;
for(int i=0;i<server_nodes.size();i++){
ServerNode sn= server_nodes.get(i);
cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(sn.getSc().getDriver());
cpds.setJdbcUrl(sn.getJdbcUrl());
cpds.setUser(sn.getSc().getUser());
cpds.setPassword(sn.getSc().getPassword());
String key = sn.getNode_name();
pools.put(key, cpds);
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static Map<String, ComboPooledDataSource> getPools() {
return pools;
}
}