1.引言
笔者最近在作一个互联网的“类SNS”应用,应用中用户数量巨大(约4000万)左右,所以,简单的使用传统单一数据库存储确定是不行的。
参考了业内普遍使用的分库分表,以及使用DAL数据访问层等的作法,笔者决定使用一种最简单的数据源路由选择方式来解决问题。
严格的说,目前的实现不能算是一个解决方案,只能是一种思路的简易实现,笔者也仅花了2天时间来完成(其中1.5天是在看资料和Spring/ibatis的源码)。这里也只是为各位看官提供一个思路参考,顺便给本身留个笔记
2.系统的设计前提
咱们的系统使用了16个数据库实例(目前分布在2台物理机器上,后期将根据系统负荷的增长,逐步移库到16台物理机器上)。16个库是根据用户的UserID进行简单的hash分配。这里值得一说的是,咱们既然作了这样的横向切分设计,就已经考虑了系统需求的特性,html
1.不会发生常常性的跨库访问。java
2.主要的业务逻辑都是围绕UserID为核心的,在一个单库事务内便可完成。mysql
在系统中,咱们使用Spring和iBatis。Spring负责数据库的事务管理AOP,以及Bean间的IOC。选择iBatis的最大缘由是对Sql的性能优化,以及后期若是有分表要求的时,能够很容易实现对sql表名替换。
3.设计思路
首先,要说明一下笔者的思路,其实很简单,即“在每次数据库操做前,肯定当前要选择的数据库对象”然后就如同访问单库同样的访问当前选中的数据库便可。
其次,要在每次DB访问前选择数据库,须要明确几个问题,1.iBatis在何时从DataSource中取得具体的数据库Connection 的,2.对取得的Connection,iBatis是否进行缓存,由于在多库状况下Connection被缓存就意味着没法及时改变数据库连接选择。 3.因为咱们使用了Spring来管理DB事务,所以必须搞清Spring对DB Connction的开关拦截过程是否会影响多DataSource的状况。
幸运的是,研究源码的结果发现,iBatis和Spring都是经过标准的DataSource接口来控制
Connection的,这就为咱们省去了不少的麻烦,只须要实现一个可以支持多个数据库的DataSource,就能达到咱们的目标。
4.代码与实现
多数据库的DataSource实现:MultiDataSource.classweb
[java] view plaincopyspring
import java.io.PrintWriter; sql
import java.sql.Connection; 数据库
import java.sql.SQLException; apache
import java.util.ArrayList; 缓存
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import com.xxx.sql.DataSourceRouter.RouterStrategy;
/**
* 复合多数据源(Alpha)
* @author linliangyi2005@gmail.com
* Jul 15, 2010
*/
public class MultiDataSource implements DataSource {
static Logger logger = Logger.getLogger(MultiDataSource.class);
//当前线程对应的实际DataSource
private ThreadLocal<DataSource> currentDataSourceHolder = new ThreadLocal<DataSource>();
//使用Key-Value映射的DataSource
private Map<String , DataSource> mappedDataSources;
//使用横向切分的分布式DataSource
private ArrayList<DataSource> clusterDataSources;
public MultiDataSource(){
mappedDataSources = new HashMap<String , DataSource>(4);
clusterDataSources = new ArrayList<DataSource>(4);
}
/**
* 数据库链接池初始化
* 该方法一般在web 应用启动时调用
*/
public void initialMultiDataSource(){
for(DataSource ds : clusterDataSources){
if(ds != null){
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
} finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
}
Collection<DataSource> dsCollection = mappedDataSources.values();
for(DataSource ds : dsCollection){
if(ds != null){
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
} finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
}
}
/**
* 获取当前线程绑定的DataSource
* @return
*/
public DataSource getCurrentDataSource() {
//若是路由策略存在,且更新过,则根据路由算法选择新的DataSource
RouterStrategy strategy = DataSourceRouter.currentRouterStrategy.get();
if(strategy == null){
throw new IllegalArgumentException("DataSource RouterStrategy No found.");
}
if(strategy != null && strategy.isRefresh()){
if(RouterStrategy.SRATEGY_TYPE_MAP.equals(strategy.getType())){
this.choiceMappedDataSources(strategy.getKey());
}else if(RouterStrategy.SRATEGY_TYPE_CLUSTER.equals(strategy.getType())){
this.routeClusterDataSources(strategy.getRouteFactor());
}
strategy.setRefresh(false);
}
return currentDataSourceHolder.get();
}
public Map<String, DataSource> getMappedDataSources() {
return mappedDataSources;
}
public void setMappedDataSources(Map<String, DataSource> mappedDataSources) {
this.mappedDataSources = mappedDataSources;
}
public ArrayList<DataSource> getClusterDataSources() {
return clusterDataSources;
}
public void setClusterDataSources(ArrayList<DataSource> clusterDataSources) {
this.clusterDataSources = clusterDataSources;
}
/**
* 使用Key选择当前的数据源
* @param key
*/
public void choiceMappedDataSources(String key){
DataSource ds = this.mappedDataSources.get(key);
if(ds == null){
throw new IllegalStateException("No Mapped DataSources Exist!");
}
this.currentDataSourceHolder.set(ds);
}
/**
* 使用取模算法,在群集数据源中作路由选择
* @param routeFactor
*/
public void routeClusterDataSources(int routeFactor){
int size = this.clusterDataSources.size();
if(size == 0){
throw new IllegalStateException("No Cluster DataSources Exist!");
}
int choosen = routeFactor % size;
DataSource ds = this.clusterDataSources.get(choosen);
if(ds == null){
throw new IllegalStateException("Choosen DataSources is null!");
}
logger.debug("Choosen DataSource No." + choosen+ " : " + ds.toString());
this.currentDataSourceHolder.set(ds);
}
/* (non-Javadoc)
* @see javax.sql.DataSource#getConnection()
*/
public Connection getConnection() throws SQLException {
if(getCurrentDataSource() != null){
return getCurrentDataSource().getConnection();
}
return null;
}
/* (non-Javadoc)
* @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
*/
public Connection getConnection(String username, String password)
throws SQLException {
if(getCurrentDataSource() != null){
return getCurrentDataSource().getConnection(username , password);
}
return null;
}
/* (non-Javadoc)
* @see javax.sql.CommonDataSource#getLogWriter()
*/
public PrintWriter getLogWriter() throws SQLException {
if(getCurrentDataSource() != null){
return getCurrentDataSource().getLogWriter();
}
return null;
}
/* (non-Javadoc)
* @see javax.sql.CommonDataSource#getLoginTimeout()
*/
public int getLoginTimeout() throws SQLException {
if(getCurrentDataSource() != null){
return getCurrentDataSource().getLoginTimeout();
}
return 0;
}
/* (non-Javadoc)
* @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter)
*/
public void setLogWriter(PrintWriter out) throws SQLException {
if(getCurrentDataSource() != null){
getCurrentDataSource().setLogWriter(out);
}
}
/* (non-Javadoc)
* @see javax.sql.CommonDataSource#setLoginTimeout(int)
*/
public void setLoginTimeout(int seconds) throws SQLException {
if(getCurrentDataSource() != null){
getCurrentDataSource().setLoginTimeout(seconds);
}
}
/* (non-Javadoc)
* 该接口方法since 1.6
* 不是全部的DataSource都实现有这个方法
* @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
*/
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// if(getCurrentDataSource() != null){
// return getCurrentDataSource().isWrapperFor(iface);
// }
return false;
}
/* (non-Javadoc)
* 该接口方法since 1.6
* 不是全部的DataSource都实现有这个方法
* @see java.sql.Wrapper#unwrap(java.lang.Class)
*/
public <T> T unwrap(Class<T> iface) throws SQLException {
// if(getCurrentDataSource() != null){
// return getCurrentDataSource().unwrap(iface);
// }
return null;
}
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import com.xxx.sql.DataSourceRouter.RouterStrategy;
/**
* 复合多数据源(Alpha)
* @author linliangyi2005@gmail.com
* Jul 15, 2010
*/
public class MultiDataSource implements DataSource {
static Logger logger = Logger.getLogger(MultiDataSource.class);
//当前线程对应的实际DataSource
private ThreadLocal<DataSource> currentDataSourceHolder = new ThreadLocal<DataSource>();
//使用Key-Value映射的DataSource
private Map<String , DataSource> mappedDataSources;
//使用横向切分的分布式DataSource
private ArrayList<DataSource> clusterDataSources;
public MultiDataSource(){
mappedDataSources = new HashMap<String , DataSource>(4);
clusterDataSources = new ArrayList<DataSource>(4);
}
/**
* 数据库链接池初始化
* 该方法一般在web 应用启动时调用
*/
public void initialMultiDataSource(){
for(DataSource ds : clusterDataSources){
if(ds != null){
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
} finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
}
Collection<DataSource> dsCollection = mappedDataSources.values();
for(DataSource ds : dsCollection){
if(ds != null){
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
} finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
}
}
/**
* 获取当前线程绑定的DataSource
* @return
*/
public DataSource getCurrentDataSource() {
//若是路由策略存在,且更新过,则根据路由算法选择新的DataSource
RouterStrategy strategy = DataSourceRouter.currentRouterStrategy.get();
if(strategy == null){
throw new IllegalArgumentException("DataSource RouterStrategy No found.");
}
if(strategy != null && strategy.isRefresh()){
if(RouterStrategy.SRATEGY_TYPE_MAP.equals(strategy.getType())){
this.choiceMappedDataSources(strategy.getKey());
}else if(RouterStrategy.SRATEGY_TYPE_CLUSTER.equals(strategy.getType())){
this.routeClusterDataSources(strategy.getRouteFactor());
}
strategy.setRefresh(false);
}
return currentDataSourceHolder.get();
}
public Map<String, DataSource> getMappedDataSources() {
return mappedDataSources;
}
public void setMappedDataSources(Map<String, DataSource> mappedDataSources) {
this.mappedDataSources = mappedDataSources;
}
public ArrayList<DataSource> getClusterDataSources() {
return clusterDataSources;
}
public void setClusterDataSources(ArrayList<DataSource> clusterDataSources) {
this.clusterDataSources = clusterDataSources;
}
/**
* 使用Key选择当前的数据源
* @param key
*/
public void choiceMappedDataSources(String key){
DataSource ds = this.mappedDataSources.get(key);
if(ds == null){
throw new IllegalStateException("No Mapped DataSources Exist!");
}
this.currentDataSourceHolder.set(ds);
}
/**
* 使用取模算法,在群集数据源中作路由选择
* @param routeFactor
*/
public void routeClusterDataSources(int routeFactor){
int size = this.clusterDataSources.size();
if(size == 0){
throw new IllegalStateException("No Cluster DataSources Exist!");
}
int choosen = routeFactor % size;
DataSource ds = this.clusterDataSources.get(choosen);
if(ds == null){
throw new IllegalStateException("Choosen DataSources is null!");
}
logger.debug("Choosen DataSource No." + choosen+ " : " + ds.toString());
this.currentDataSourceHolder.set(ds);
}
/* (non-Javadoc)
* @see javax.sql.DataSource#getConnection()
*/
public Connection getConnection() throws SQLException {
if(getCurrentDataSource() != null){
return getCurrentDataSource().getConnection();
}
return null;
}
/* (non-Javadoc)
* @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
*/
public Connection getConnection(String username, String password)
throws SQLException {
if(getCurrentDataSource() != null){
return getCurrentDataSource().getConnection(username , password);
}
return null;
}
/* (non-Javadoc)
* @see javax.sql.CommonDataSource#getLogWriter()
*/
public PrintWriter getLogWriter() throws SQLException {
if(getCurrentDataSource() != null){
return getCurrentDataSource().getLogWriter();
}
return null;
}
/* (non-Javadoc)
* @see javax.sql.CommonDataSource#getLoginTimeout()
*/
public int getLoginTimeout() throws SQLException {
if(getCurrentDataSource() != null){
return getCurrentDataSource().getLoginTimeout();
}
return 0;
}
/* (non-Javadoc)
* @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter)
*/
public void setLogWriter(PrintWriter out) throws SQLException {
if(getCurrentDataSource() != null){
getCurrentDataSource().setLogWriter(out);
}
}
/* (non-Javadoc)
* @see javax.sql.CommonDataSource#setLoginTimeout(int)
*/
public void setLoginTimeout(int seconds) throws SQLException {
if(getCurrentDataSource() != null){
getCurrentDataSource().setLoginTimeout(seconds);
}
}
/* (non-Javadoc)
* 该接口方法since 1.6
* 不是全部的DataSource都实现有这个方法
* @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
*/
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// if(getCurrentDataSource() != null){
// return getCurrentDataSource().isWrapperFor(iface);
// }
return false;
}
/* (non-Javadoc)
* 该接口方法since 1.6
* 不是全部的DataSource都实现有这个方法
* @see java.sql.Wrapper#unwrap(java.lang.Class)
*/
public <T> T unwrap(Class<T> iface) throws SQLException {
// if(getCurrentDataSource() != null){
// return getCurrentDataSource().unwrap(iface);
// }
return null;
}
1.实例变量 clusterDataSources 是一个DataSource 的 ArrayList它存储了多个数据库的DataSource实例,咱们使用Spring的IOC功能,将多个DataSource注入到这个list中。
2.实例变量 mappedDataSources 是一个DataSource 的Map,它与clusterDataSources 同样用来存储多个数据库的DataSource实例,不一样的是,它可使用key直接获取DataSource。咱们同样会使用Spring的IOC功 能,将多个DataSource注入到这个Map中。
3.实例变量currentDataSourceHolder ,他是一个ThreadLocal变量,保存与当前线程相关的且已经取得的DataSource实例。这是为了在同一线程中,屡次访问同一数据库时,不须要再从新作路由选择。
4.当外部类调用getConnection()方法时,方法将根据上下文的路由规则,从clusterDataSources 或者 mappedDataSources 选择对应DataSource,并返回其中的Connection。
(PS:关于DataSource的路由选择规则,能够根据应用场景的不一样,自行设计。笔者这里提供两种简单的思路,1.根据 HashCode,在上述例子中能够是UserId,进行取模运算,来定位数据库。2.根据上下文设置的关键字key,从map中选择映射的 DataSource)
5.将MultiDataSource与Spring,iBatis结合
在完成了上述的编码过程后,就是将这个MultiDataSource与现有Spring和iBatis结合起来配置。
STEP 1。配置多个数据源
笔者这里使用了C3P0做为数据库链接池,这一步和标准的Spring配置同样,惟一不一样的是,之前只配置一个,如今要配置多个
[xml] view plaincopy
<textarea cols="84" rows="15" name="code" class="xhtml"><!-- jdbc链接池-1-->
<bean id="c3p0_dataSource_1" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driverClass}</value>
</property>
<property name="jdbcUrl">
<value>${mysql.url_1}</value>
</property>
<property name="user">
value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<!--链接池中保留的最小链接数。-->
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<!--链接池中保留的最大链接数。Default: 15 -->
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<!--初始化时获取的链接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<!--每60秒检查全部链接池中的空闲链接。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
</bean>
<!------------- jdbc链接池-2------------------->
<bean id="c3p0_dataSource_2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driverClass}</value>
</property>
<property name="jdbcUrl">
<value>${mysql.url_2}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<!--链接池中保留的最小链接数。-->
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<!--链接池中保留的最大链接数。Default: 15 -->
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<!--初始化时获取的链接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<!--每60秒检查全部链接池中的空闲链接。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
</bean>
<!------------- 更多的连接池配置------------------->
......
<!-- jdbc链接池-1-->
<bean id="c3p0_dataSource_1" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driverClass}</value>
</property>
<property name="jdbcUrl">
<value>${mysql.url_1}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<!--链接池中保留的最小链接数。-->
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<!--链接池中保留的最大链接数。Default: 15 -->
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<!--初始化时获取的链接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<!--每60秒检查全部链接池中的空闲链接。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
</bean>
<!------------- jdbc链接池-2------------------->
<bean id="c3p0_dataSource_2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driverClass}</value>
</property>
<property name="jdbcUrl">
<value>${mysql.url_2}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<!--链接池中保留的最小链接数。-->
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<!--链接池中保留的最大链接数。Default: 15 -->
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<!--初始化时获取的链接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<!--每60秒检查全部链接池中的空闲链接。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
</bean>
<!------------- 更多的连接池配置------------------->
......
</textarea>
STEP 2。将多个数据源都注入到MultiDataSource中
[xhtml] view plaincopy
<bean id="multiDataSource" class="com.xxx.sql.MultiDataSource">
<property name="clusterDataSources">
<list>
<ref bean="c3p0_dataSource_1" />
<ref bean="c3p0_dataSource_2" />
<ref bean="c3p0_dataSource_3" />
<ref bean="c3p0_dataSource_4" />
<ref bean="c3p0_dataSource_5" />
<ref bean="c3p0_dataSource_6" />
<ref bean="c3p0_dataSource_7" />
<ref bean="c3p0_dataSource_8" />
</list>
</property>
<property name="mappedDataSources">
<map>
<entry key="system" value-ref="c3p0_dataSource_system" />
</map>
</property>
</bean>
<bean id="multiDataSource" class="com.xxx.sql.MultiDataSource">
<property name="clusterDataSources">
<list>
<ref bean="c3p0_dataSource_1" />
<ref bean="c3p0_dataSource_2" />
<ref bean="c3p0_dataSource_3" />
<ref bean="c3p0_dataSource_4" />
<ref bean="c3p0_dataSource_5" />
<ref bean="c3p0_dataSource_6" />
<ref bean="c3p0_dataSource_7" />
<ref bean="c3p0_dataSource_8" />
</list>
</property>
<property name="mappedDataSources">
<map>
<entry key="system" value-ref="c3p0_dataSource_system" />
</map>
</property>
</bean>
[xml] view plaincopy
STEP 3。像使用标准的DataSource同样,使用MultiDataSource
[xml] view plaincopy
<textarea cols="84" rows="15" name="code" class="xhtml"><!-- iBatis Client配置 将 MultiDataSource 与iBatis Client 绑定-->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<property name="dataSource" ref="multiDataSource"></property>
</bean>
<!-- jdbc事务管理配置 将 MultiDataSource 与事务管理器绑定-->
<bean id="jdbc_TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="multiDataSource"></property>
</bean>
<!-- iBatis Client配置 将 MultiDataSource 与iBatis Client 绑定-->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<property name="dataSource" ref="multiDataSource"></property>
</bean>
<!-- jdbc事务管理配置 将 MultiDataSource 与事务管理器绑定-->
<bean id="jdbc_TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="multiDataSource"></property>
</bean>
</textarea>
至此,咱们的程序就可让Spring来管理多库访问了,但请注意,数据库事务仍然限于单库范围(以前已经说过,这里的应用场景不存在跨库的事务)。
6.Java代码使用例子
首先要说明的是,这里咱们只是提供了一个简单的使用范例,在范例中,咱们还必须手动的调用API,以肯定DataSource的路由规则,在实际的应用中,您能够针对本身的业务特色,对此进行封装,以实现相对透明的路由选择
[java] view plaincopy
<textarea cols="81" rows="15" name="code" class="java">public boolean addUserGameInfo(UserGameInfo userGameInfo){
//1.根据UserGameInfo.uid 进行数据源路由选择 DataSourceRouter.setRouterStrategy(
RouterStrategy.SRATEGY_TYPE_CLUSTER ,
null,
userGameInfo.getUid());
//2.数据库存储
try {
userGameInfoDAO.insert(userGameInfo);
return true;
} catch (SQLException e) {
e.printStackTrace();
logger.debug("Insert UserGameInfo failed. " + userGameInfo.toString());
}
return false;
}
public boolean addUserGameInfo(UserGameInfo userGameInfo){
//1.根据UserGameInfo.uid 进行数据源路由选择
DataSourceRouter.setRouterStrategy(
RouterStrategy.SRATEGY_TYPE_CLUSTER ,
null,
userGameInfo.getUid());
//2.数据库存储
try {
userGameInfoDAO.insert(userGameInfo);
return true;
} catch (SQLException e) {
e.printStackTrace();
logger.debug("Insert UserGameInfo failed. " + userGameInfo.toString());
}
return false;
}
</textarea>
OK,咱们的多库横向切分的实验能够暂告一个段落。实际上,要实现一个完整的DAL是很是庞大的工程,而对咱们推进巨大的,可能只是很小的一个部分,处处都存在着8-2法则,要如何选择,就看各位看官了!!