在spring中配置多数据库读取
来源:互联网 发布:unity3d wsd人物行走 编辑:程序博客网 时间:2024/05/16 18:51
在应对大量用户读取的系统中,对数据库的操作通常采用读写分离方式,往一个数据库写入,然后通过复制将数据同步到另外的多个数据库中,读操作都从这些数据库中操作,在采用spring来配置多数据库时,并不能直接支持从多个DataSource中获得数据库连接,为此需要开发一个DataSource的代理,代理实现javax.sql.DataSource接口。该代理根据一定的策略从已有的多个DataSource中选择一个,提供给SessionFactory,供数据访问层使用。原理如下图所示:
DataSource Proxy选择DataSource的方式可以根据实际进行设计,这里为简单,就采用随机方式选择一个。源代码如下:
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<DataSource> dataSourcePool ;protected Log log = LogFactory.getLog(getClass());private static ThreadLocal<DataSource> dsHolder = new ThreadLocal<DataSource>();/** * 从已有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();}@Overridepublic Connection getConnection() throws SQLException {Connection conn = randomDs().getConnection();log.info("conn URL---->"+conn.getMetaData().getURL());return conn;}@Overridepublic Connection getConnection(String username, String password)throws SQLException {// TODO Auto-generated method stubConnection conn = randomDs().getConnection(username, password);return conn;}@Overridepublic PrintWriter getLogWriter() throws SQLException {return dsHolder.get().getLogWriter();}@Overridepublic int getLoginTimeout() throws SQLException {// TODO Auto-generated method stubreturn dsHolder.get().getLoginTimeout();}@Overridepublic void setLogWriter(PrintWriter out) throws SQLException {// TODO Auto-generated method stubdsHolder.get().setLogWriter(out);}@Overridepublic void setLoginTimeout(int seconds) throws SQLException {// TODO Auto-generated method stubdsHolder.get().setLoginTimeout(seconds);}@Overridepublic boolean isWrapperFor(Class<?> iface) throws SQLException {// TODO Auto-generated method stubreturn dsHolder.get().isWrapperFor(iface);}@Overridepublic <T> T unwrap(Class<T> iface) throws SQLException {// TODO Auto-generated method stubreturn dsHolder.get().unwrap(iface);}public List<DataSource> getDataSourcePool() {return dataSourcePool;}public void setDataSourcePool(List<DataSource> dataSourcePool) {this.dataSourcePool = dataSourcePool;}}
为了可用性,实际生产环境中需要判断取到的DataSource对应的数据库是否可访问,若不能访问,则要取其他可用的DataSource。
在springframework中的配置如下:
<bean id="dataSource" class="code.RandomDataSource"> <property name="dataSourcePool"> <list> <ref bean="dataSource1" /> <ref bean="dataSource2" /> </list> </property> </bean> <bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://192.168.2.192/ebook"/><property name="user" value="test"/><property name="password" value="test"/></bean> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://192.168.2.105/ebook"/><property name="user" value="test"/><property name="password" value="test"/></bean><!-- Hibernate SessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property><property name="dataSource" ref="dataSource"/></bean>
这里连接的是MySQL数据库,如果是其他数据库,需要将DataSource1和DataSource2中的参数以及hibernate.dialect做相应修改。
- 在spring中配置多数据库读取
- 在spring中配置多数据库读取
- Spring 多数据库 配置
- 在Spring下配置多数据源
- spring 中多数据源的配置
- spring中多数据源配置
- spring中多数据配置2
- 如何在spring中配置使得mybatis3.1.1中支持vendor方式的multi-db(多数据库)
- spring 配置多数据库和数据源
- Spring + Mybatis配置多数据库
- Spring + Mybatis配置多数据库
- 基于注解的形式在Spring中实现对多数据源配置和使用
- Spring Boot中使用多数据库
- spring配置多数据源
- Spring多数据源配置
- Spring多数据源配置
- spring多数据源配置
- Spring配置多数据源
- 3D Max Mesh Smooth at render time
- String与StringBuffer的区别?
- 《service》-“linux命令五分钟系列”之二
- hdu 2424
- 每天一道笔试题-2012年3月1日
- 在spring中配置多数据库读取
- Spring Security学习总结
- iReport的style使用举例
- MFC之命令更新
- visual studio 2010 运行后 不显示按任意键退出,窗口直接消失
- asp.net 获得客户端IP地址
- MFC 对话框程序如何添加目录,工具栏,状态栏
- 不要做不擅长的事情
- 总结