1.引言 
   笔者最近在做一个互联网的“类SNS”应用,应用中用户数量巨大(约4000万)左右,因此,简单的使用传统单一数据库存储肯定是不行的。 

   参考了业内广泛使用的分库分表,以及使用DAL数据访问层等的做法,笔者决定使用一种最简单的数据源路由选择方式来解决问题。 

   严格的说,目前的实现不能算是一个解决方案,只能是一种思路的简易实现,笔者也仅花了2天时间来完成(其中1.5天是在看资料和Spring/ibatis的源码)。这里也只是为各位看官提供一个思路参考,顺便给自己留个笔记 

2.系统的设计前提 
   我们的系统使用了16个数据库实例(目前分布在2台物理机器上,后期将根据系统负荷的增加,逐步移库到16台物理机器上)。16个库是根据用户的UserID进行简单的hash分配。这里值得一说的是,我们既然做了这样的横向切分设计,就已经考虑了系统需求的特性, 
  • 1.不会发生经常性的跨库访问。
  • 2.主要的业务逻辑都是围绕UserID为核心的,在一个单库事务内即可完成。


   在系统中,我们使用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.class 
Java代码  收藏代码
  1. import java.io.PrintWriter;  
  2. import java.sql.Connection;  
  3. import java.sql.SQLException;  
  4. import java.util.ArrayList;  
  5. import java.util.Collection;  
  6. import java.util.HashMap;  
  7. import java.util.Map;  
  8.   
  9. import javax.sql.DataSource;  
  10.   
  11. import org.apache.log4j.Logger;  
  12.   
  13. import com.xxx.sql.DataSourceRouter.RouterStrategy;  
  14.   
  15. /** 
  16.  * 复合多数据源(Alpha) 
  17.  * @author linliangyi2005@gmail.com 
  18.  * Jul 15, 2010 
  19.  */  
  20. public class MultiDataSource implements DataSource {  
  21.       
  22.     static Logger logger = Logger.getLogger(MultiDataSource.class);  
  23.       
  24.     //当前线程对应的实际DataSource  
  25.     private ThreadLocal<DataSource> currentDataSourceHolder = new ThreadLocal<DataSource>();  
  26.     //使用Key-Value映射的DataSource  
  27.     private Map<String , DataSource> mappedDataSources;  
  28.     //使用横向切分的分布式DataSource  
  29.     private ArrayList<DataSource> clusterDataSources;  
  30.       
  31.     public MultiDataSource(){  
  32.         mappedDataSources = new HashMap<String , DataSource>(4);  
  33.         clusterDataSources = new ArrayList<DataSource>(4);  
  34.     }  
  35.       
  36.     /** 
  37.      * 数据库连接池初始化 
  38.      * 该方法通常在web 应用启动时调用 
  39.      */  
  40.     public void initialMultiDataSource(){  
  41.         for(DataSource ds : clusterDataSources){  
  42.             if(ds != null){  
  43.                 Connection conn = null;  
  44.                 try {  
  45.                     conn = ds.getConnection();                    
  46.                 } catch (SQLException e) {  
  47.                     e.printStackTrace();  
  48.                 } finally{  
  49.                     if(conn != null){  
  50.                         try {  
  51.                             conn.close();  
  52.                         } catch (SQLException e) {  
  53.                             e.printStackTrace();  
  54.                         }  
  55.                         conn = null;  
  56.                     }  
  57.                 }  
  58.             }  
  59.         }  
  60.         Collection<DataSource> dsCollection = mappedDataSources.values();  
  61.         for(DataSource ds : dsCollection){  
  62.             if(ds != null){  
  63.                 Connection conn = null;  
  64.                 try {  
  65.                     conn = ds.getConnection();  
  66.                 } catch (SQLException e) {  
  67.                     e.printStackTrace();  
  68.                 } finally{  
  69.                     if(conn != null){  
  70.                         try {  
  71.                             conn.close();  
  72.                         } catch (SQLException e) {  
  73.                             e.printStackTrace();  
  74.                         }  
  75.                         conn = null;  
  76.                     }  
  77.                 }  
  78.             }  
  79.         }  
  80.     }  
  81.     /** 
  82.      * 获取当前线程绑定的DataSource 
  83.      * @return 
  84.      */  
  85.     public DataSource getCurrentDataSource() {  
  86.         //如果路由策略存在,且更新过,则根据路由算法选择新的DataSource  
  87.         RouterStrategy strategy = DataSourceRouter.currentRouterStrategy.get();  
  88.         if(strategy == null){  
  89.             throw new IllegalArgumentException("DataSource RouterStrategy No found.");  
  90.         }         
  91.         if(strategy != null && strategy.isRefresh()){             
  92.             if(RouterStrategy.SRATEGY_TYPE_MAP.equals(strategy.getType())){  
  93.                 this.choiceMappedDataSources(strategy.getKey());  
  94.                   
  95.             }else if(RouterStrategy.SRATEGY_TYPE_CLUSTER.equals(strategy.getType())){  
  96.                 this.routeClusterDataSources(strategy.getRouteFactor());  
  97.             }             
  98.             strategy.setRefresh(false);  
  99.         }  
  100.         return currentDataSourceHolder.get();  
  101.     }  
  102.   
  103.     public Map<String, DataSource> getMappedDataSources() {  
  104.         return mappedDataSources;  
  105.     }  
  106.   
  107.     public void setMappedDataSources(Map<String, DataSource> mappedDataSources) {  
  108.         this.mappedDataSources = mappedDataSources;  
  109.     }  
  110.   
  111.     public ArrayList<DataSource> getClusterDataSources() {  
  112.         return clusterDataSources;  
  113.     }  
  114.   
  115.     public void setClusterDataSources(ArrayList<DataSource> clusterDataSources) {  
  116.         this.clusterDataSources = clusterDataSources;  
  117.     }  
  118.       
  119.     /** 
  120.      * 使用Key选择当前的数据源 
  121.      * @param key 
  122.      */  
  123.     public void choiceMappedDataSources(String key){  
  124.         DataSource ds = this.mappedDataSources.get(key);  
  125.         if(ds == null){  
  126.             throw new IllegalStateException("No Mapped DataSources Exist!");  
  127.         }  
  128.         this.currentDataSourceHolder.set(ds);  
  129.     }  
  130.       
  131.     /** 
  132.      * 使用取模算法,在群集数据源中做路由选择 
  133.      * @param routeFactor 
  134.      */  
  135.     public void routeClusterDataSources(int routeFactor){  
  136.         int size = this.clusterDataSources.size();  
  137.         if(size == 0){  
  138.             throw new IllegalStateException("No Cluster DataSources Exist!");  
  139.         }  
  140.         int choosen = routeFactor % size;  
  141.         DataSource ds = this.clusterDataSources.get(choosen);  
  142.         if(ds == null){  
  143.             throw new IllegalStateException("Choosen DataSources is null!");  
  144.         }  
  145.         logger.debug("Choosen DataSource No." + choosen+ " : " + ds.toString());  
  146.         this.currentDataSourceHolder.set(ds);  
  147.     }  
  148.   
  149.     /* (non-Javadoc) 
  150.      * @see javax.sql.DataSource#getConnection() 
  151.      */  
  152.     public Connection getConnection() throws SQLException {  
  153.         if(getCurrentDataSource() != null){  
  154.             return getCurrentDataSource().getConnection();  
  155.         }  
  156.         return null;  
  157.     }  
  158.   
  159.     /* (non-Javadoc) 
  160.      * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String) 
  161.      */  
  162.     public Connection getConnection(String username, String password)  
  163.             throws SQLException {  
  164.         if(getCurrentDataSource() != null){  
  165.             return getCurrentDataSource().getConnection(username , password);  
  166.         }  
  167.         return null;  
  168.     }  
  169.   
  170.     /* (non-Javadoc) 
  171.      * @see javax.sql.CommonDataSource#getLogWriter() 
  172.      */  
  173.     public PrintWriter getLogWriter() throws SQLException {  
  174.         if(getCurrentDataSource() != null){  
  175.             return getCurrentDataSource().getLogWriter();  
  176.         }  
  177.         return null;  
  178.     }  
  179.   
  180.     /* (non-Javadoc) 
  181.      * @see javax.sql.CommonDataSource#getLoginTimeout() 
  182.      */  
  183.     public int getLoginTimeout() throws SQLException {  
  184.         if(getCurrentDataSource() != null){  
  185.             return getCurrentDataSource().getLoginTimeout();  
  186.         }  
  187.         return 0;  
  188.     }  
  189.   
  190.     /* (non-Javadoc) 
  191.      * @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter) 
  192.      */  
  193.     <span class="keyword" style="color: rgb(127%2