设为首页 加入收藏

TOP

在spring中配置多数据库读取(一)
2014-11-24 07:25:23 来源: 作者: 【 】 浏览:8
Tags:spring 配置 数据库 读取
在应对大量用户读取的系统中,对 数据库的操作通常采用读写分离方式,往一个数据库写入,然后通过复制将数据同步到另外的多个数据库中,读操作都从这些数据库中操作,在采用spring来配置多数据库时,并不能直接支持从多个DataSource中获得数据库连接,为此需要开发一个DataSource的代理,代理实现javax.sql.DataSource接口。该代理根据一定的策略从已有的多个DataSource中选择一个,提供给SessionFactory,供数据访问层使用。原理如下图所示:

\



DataSource Proxy选择DataSource的方式可以根据实际进行设计,这里为简单,就采用随机方式选择一个。源代码如下:


[java] view plaincopy
package code;


import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;


import java.util.List;
import java.util.Random;


import javax.sql.DataSource;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class RandomDataSource implements DataSource {


private List dataSourcePool ;
protected Log log = LogFactory.getLog(getClass());

private static ThreadLocal dsHolder = new ThreadLocal();


/**
* 从已有DataSource中随机选择一个
*
*
*/
private DataSource randomDs(){

int size = dataSourcePool.size();

Random r= new Random();
int t = r.nextInt(size);
dsHolder.set(dataSourcePool.get(t));
return dsHolder.get();


}


@Override
public Connection getConnection() throws SQLException {

Connection conn = randomDs().getConnection();
return conn;
}


@Override
public Connection getConnection(String username, String password)
throws SQLException {
Connection conn = randomDs().getConnection(username, password);
log.info("conn URL---->"+conn.getMetaData().getURL());

return conn;
}


@Override
public PrintWriter getLogWriter() throws SQLException {

return dsHolder.get().getLogWriter();
}


@Override
public int getLoginTimeout() throws SQLException {

return dsHolder.get().getLoginTimeout();
}


@Override
public void setLogWriter(PrintWriter out) throws SQLException {

dsHolder.get().setLogWriter(out);
}


@Override
public void setLoginTimeout(int seconds) throws SQLException {

dsHolder.get().setLoginTimeout(seconds);
}


@Override
public boolean isWrapperFor(Class< > iface) throws SQLException {
// TODO Auto-generated method stub
return dsHolder.get().isWrapperFor(iface);
}


@Override
public T unwrap(Class iface) throws SQLException {

return dsHolder.get().unwrap(iface);
}


public List getDataSourcePool() {
return dataSourcePool;
}


public void setDataSourcePool(List dataSourcePool) {

this.dataSourcePool = dataSourcePool;
}


}

为了可用性,实际生产环境中需要判断取到的DataSource对应的数据库是否可访问,若不能访问,则要取其他可用的DataSource。


在springframework中的配置如下:





xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
h
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇获取当前日期对应是星期几 下一篇数据库归档模式修改

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Libevent C++ 高并发 (2025-12-26 00:49:30)
·C++ dll 设计接口时 (2025-12-26 00:49:28)
·透彻理解 C 语言指针 (2025-12-26 00:22:52)
·C语言指针详解 (经典 (2025-12-26 00:22:49)
·C 指针 | 菜鸟教程 (2025-12-26 00:22:46)