Spring+Ibatis数据库水平分库

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代码

  

[java] view plaincopy

  1. import java.io.PrintWriter;     

  2.   

  3. import java.sql.Connection;     

  4.   

  5. import java.sql.SQLException;     

  6.   

  7. import java.util.ArrayList;     

  8.   

  9. import java.util.Collection;     

  10.   

  11. import java.util.HashMap;     

  12.   

  13. import java.util.Map;     

  14.   

  15.     

  16.   

  17. import javax.sql.DataSource;     

  18.   

  19.     

  20.   

  21. import org.apache.log4j.Logger;     

  22.   

  23.     

  24.   

  25. import com.xxx.sql.DataSourceRouter.RouterStrategy;     

  26.   

  27.     

  28.   

  29. /**   

  30.  

  31.  * 复合多数据源(Alpha)   

  32.  

  33.  * @author linliangyi2005@gmail.com   

  34.  

  35.  * Jul 15, 2010   

  36.  

  37.  */    

  38.   

  39. public class MultiDataSource implements DataSource {     

  40.   

  41.          

  42.   

  43.     static Logger logger = Logger.getLogger(MultiDataSource.class);     

  44.   

  45.          

  46.   

  47.     //当前线程对应的实际DataSource     

  48.   

  49.     private ThreadLocal<DataSource> currentDataSourceHolder = new ThreadLocal<DataSource>();     

  50.   

  51.     //使用Key-Value映射的DataSource     

  52.   

  53.     private Map<String , DataSource> mappedDataSources;     

  54.   

  55.     //使用横向切分的分布式DataSource     

  56.   

  57.     private ArrayList<DataSource> clusterDataSources;     

  58.   

  59.          

  60.   

  61.     public MultiDataSource(){     

  62.   

  63.         mappedDataSources = new HashMap<String , DataSource>(4);     

  64.   

  65.         clusterDataSources = new ArrayList<DataSource>(4);     

  66.   

  67.     }     

  68.   

  69.          

  70.   

  71.     /**   

  72.  

  73.      * 数据库连接池初始化   

  74.  

  75.      * 该方法通常在web 应用启动时调用   

  76.  

  77.      */    

  78.   

  79.     public void initialMultiDataSource(){     

  80.   

  81.         for(DataSource ds : clusterDataSources){     

  82.   

  83.             if(ds != null){     

  84.   

  85.                 Connection conn = null;     

  86.   

  87.                 try {     

  88.   

  89.                     conn = ds.getConnection();                       

  90.   

  91.                 } catch (SQLException e) {     

  92.   

  93.                     e.printStackTrace();     

  94.   

  95.                 } finally{     

  96.   

  97.                     if(conn != null){     

  98.   

  99.                         try {     

  100.   

  101.                             conn.close();     

  102.   

  103.                         } catch (SQLException e) {     

  104.   

  105.                             e.printStackTrace();     

  106.   

  107.                         }     

  108.   

  109.                         conn = null;     

  110.   

  111.                     }     

  112.   

  113.                 }     

  114.   

  115.             }     

  116.   

  117.         }     

  118.   

  119.         Collection<DataSource> dsCollection = mappedDataSources.values();     

  120.   

  121.        for(DataSource ds : dsCollection){     

  122.   

  123.             if(ds != null){     

  124.   

  125.                 Connection conn = null;     

  126.   

  127.                 try {     

  128.   

  129.                     conn = ds.getConnection();     

  130.   

  131.                 } catch (SQLException e) {     

  132.   

  133.                     e.printStackTrace();     

  134.   

  135.                 } finally{     

  136.   

  137.                     if(conn != null){     

  138.   

  139.                         try {     

  140.   

  141.                             conn.close();     

  142.   

  143.                         } catch (SQLException e) {     

  144.   

  145.                             e.printStackTrace();     

  146.   

  147.                         }     

  148.   

  149.                         conn = null;     

  150.   

  151.                     }     

  152.   

  153.                 }     

  154.   

  155.             }     

  156.   

  157.         }     

  158.   

  159.     }     

  160.   

  161.     /**   

  162.  

  163.      * 获取当前线程绑定的DataSource   

  164.  

  165.      * @return   

  166.  

  167.      */    

  168.   

  169.     public DataSource getCurrentDataSource() {     

  170.   

  171.         //如果路由策略存在,且更新过,则根据路由算法选择新的DataSource     

  172.   

  173.         RouterStrategy strategy = DataSourceRouter.currentRouterStrategy.get();     

  174.   

  175.         if(strategy == null){     

  176.   

  177.             throw new IllegalArgumentException("DataSource RouterStrategy No found.");     

  178.   

  179.         }            

  180.   

  181.         if(strategy != null && strategy.isRefresh()){                

  182.   

  183.            if(RouterStrategy.SRATEGY_TYPE_MAP.equals(strategy.getType())){     

  184.   

  185.                 this.choiceMappedDataSources(strategy.getKey());     

  186.   

  187.                     

  188.   

  189.             }else if(RouterStrategy.SRATEGY_TYPE_CLUSTER.equals(strategy.getType())){     

  190.   

  191.                 this.routeClusterDataSources(strategy.getRouteFactor());     

  192.   

  193.             }                

  194.   

  195.             strategy.setRefresh(false);     

  196.   

  197.         }     

  198.   

  199.         return currentDataSourceHolder.get();     

  200.   

  201.     }     

  202.   

  203.     

  204.   

  205.     public Map<String, DataSource> getMappedDataSources() {     

  206.   

  207.         return mappedDataSources;     

  208.   

  209.     }     

  210.   

  211.    

  212.   

  213.     public void setMappedDataSources(Map<String, DataSource> mappedDataSources) {     

  214.   

  215.         this.mappedDataSources = mappedDataSources;     

  216.   

  217.     }     

  218.   

  219.     

  220.   

  221.     public ArrayList<DataSource> getClusterDataSources() {     

  222.   

  223.         return clusterDataSources;     

  224.   

  225.     }     

  226.   

  227.     

  228.   

  229.     public void setClusterDataSources(ArrayList<DataSource> clusterDataSources) {     

  230.   

  231.         this.clusterDataSources = clusterDataSources;     

  232.   

  233.     }     

  234.   

  235.          

  236.   

  237.     /**   

  238.  

  239.      * 使用Key选择当前的数据源   

  240.  

  241.      * @param key   

  242.  

  243.      */    

  244.   

  245.     public void choiceMappedDataSources(String key){     

  246.   

  247.         DataSource ds = this.mappedDataSources.get(key);     

  248.   

  249.         if(ds == null){     

  250.   

  251.             throw new IllegalStateException("No Mapped DataSources Exist!");     

  252.   

  253.         }     

  254.   

  255.         this.currentDataSourceHolder.set(ds);     

  256.   

  257.     }     

  258.   

  259.          

  260.   

  261.     /**   

  262.  

  263.      * 使用取模算法,在群集数据源中做路由选择   

  264.  

  265.      * @param routeFactor   

  266.  

  267.      */    

  268.   

  269.     public void routeClusterDataSources(int routeFactor){     

  270.   

  271.         int size = this.clusterDataSources.size();     

  272.   

  273.         if(size == 0){     

  274.   

  275.             throw new IllegalStateException("No Cluster DataSources Exist!");     

  276.   

  277.         }     

  278.   

  279.         int choosen = routeFactor % size;     

  280.   

  281.        DataSource ds = this.clusterDataSources.get(choosen);     

  282.   

  283.         if(ds == null){     

  284.   

  285.             throw new IllegalStateException("Choosen DataSources is null!");     

  286.   

  287.         }     

  288.   

  289.         logger.debug("Choosen DataSource No." + choosen+ " : " + ds.toString());     

  290.   

  291.         this.currentDataSourceHolder.set(ds);     

  292.   

  293.     }     

  294.   

  295.     

  296.   

  297.     /* (non-Javadoc)   

  298.  

  299.      * @see javax.sql.DataSource#getConnection()   

  300.  

  301.      */    

  302.   

  303.     public Connection getConnection() throws SQLException {     

  304.   

  305.        if(getCurrentDataSource() != null){     

  306.   

  307.            return getCurrentDataSource().getConnection();     

  308.   

  309.         }     

  310.   

  311.         return null;     

  312.   

  313.     }       

  314.   

  315.     /* (non-Javadoc)   

  316.  

  317.      * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)   

  318.  

  319.      */    

  320.   

  321.     public Connection getConnection(String username, String password)     

  322.   

  323.             throws SQLException {     

  324.   

  325.         if(getCurrentDataSource() != null){     

  326.   

  327.           return getCurrentDataSource().getConnection(username , password);     

  328.   

  329.        }     

  330.   

  331.         return null;     

  332.   

  333.     }     

  334.   

  335.     

  336.   

  337.     /* (non-Javadoc)   

  338.  

  339.      * @see javax.sql.CommonDataSource#getLogWriter()   

  340.  

  341.      */    

  342.   

  343.    public PrintWriter getLogWriter() throws SQLException {     

  344.   

  345.         if(getCurrentDataSource() != null){     

  346.   

  347.            return getCurrentDataSource().getLogWriter();     

  348.   

  349.         }     

  350.   

  351.        return null;     

  352.   

  353.     }     

  354.   

  355.     

  356.   

  357.     /* (non-Javadoc)   

  358.  

  359.      * @see javax.sql.CommonDataSource#getLoginTimeout()   

  360.  

  361.      */    

  362.   

  363.     public int getLoginTimeout() throws SQLException {     

  364.   

  365.         if(getCurrentDataSource() != null){     

  366.   

  367.             return getCurrentDataSource().getLoginTimeout();     

  368.   

  369.         }     

  370.   

  371.         return 0;     

  372.   

  373.     }     

  374.   

  375.     

  376.   

  377.     /* (non-Javadoc)   

  378.  

  379.      * @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter)   

  380.  

  381.      */    

  382.   

  383.     public void setLogWriter(PrintWriter out) throws SQLException {     

  384.   

  385.         if(getCurrentDataSource() != null){     

  386.   

  387.             getCurrentDataSource().setLogWriter(out);     

  388.   

  389.         }     

  390.   

  391.     }     

  392.   

  393.     

  394.   

  395.     /* (non-Javadoc)   

  396.  

  397.      * @see javax.sql.CommonDataSource#setLoginTimeout(int)   

  398.  

  399.      */    

  400.   

  401.     public void setLoginTimeout(int seconds) throws SQLException {     

  402.   

  403.         if(getCurrentDataSource() != null){     

  404.   

  405.             getCurrentDataSource().setLoginTimeout(seconds);     

  406.   

  407.         }     

  408.   

  409.     }     

  410.   

  411.     

  412.   

  413.     /* (non-Javadoc)   

  414.  

  415.      * 该接口方法since 1.6   

  416.  

  417.      * 不是所有的DataSource都实现有这个方法   

  418.  

  419.     * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)   

  420.  

  421.      */    

  422.   

  423.     public boolean isWrapperFor(Class<?> iface) throws SQLException {     

  424.   

  425.              

  426.   

  427. //      if(getCurrentDataSource() != null){     

  428.   

  429. //          return getCurrentDataSource().isWrapperFor(iface);     

  430.   

  431. //      }     

  432.   

  433.         return false;     

  434.   

  435.    }     

  436.   

  437.     

  438.   

  439.     /* (non-Javadoc)   

  440.  

  441.      * 该接口方法since 1.6   

  442.  

  443.      * 不是所有的DataSource都实现有这个方法   

  444.  

  445.      * @see java.sql.Wrapper#unwrap(java.lang.Class)   

  446.  

  447.      */    

  448.   

  449.     public <T> T unwrap(Class<T> iface) throws SQLException {     

  450.   

  451. //      if(getCurrentDataSource() != null){     

  452.   

  453. //          return getCurrentDataSource().unwrap(iface);     

  454.   

  455. //      }     

  456.   

  457.        return null;     

  458.   

  459.     }    

  460.   

  461. import java.io.PrintWriter;  

  462. import java.sql.Connection;  

  463. import java.sql.SQLException;  

  464. import java.util.ArrayList;  

  465. import java.util.Collection;  

  466. import java.util.HashMap;  

  467. import java.util.Map;  

  468.   

  469. import javax.sql.DataSource;  

  470.   

  471. import org.apache.log4j.Logger;  

  472.   

  473. import com.xxx.sql.DataSourceRouter.RouterStrategy;  

  474.   

  475. /** 

  476.  * 复合多数据源(Alpha) 

  477.  * @author linliangyi2005@gmail.com 

  478.  * Jul 15, 2010 

  479.  */  

  480. public class MultiDataSource implements DataSource {  

  481.       

  482.     static Logger logger = Logger.getLogger(MultiDataSource.class);  

  483.       

  484.     //当前线程对应的实际DataSource  

  485.     private ThreadLocal<DataSource> currentDataSourceHolder = new ThreadLocal<DataSource>();  

  486.     //使用Key-Value映射的DataSource  

  487.     private Map<String , DataSource> mappedDataSources;  

  488.     //使用横向切分的分布式DataSource  

  489.     private ArrayList<DataSource> clusterDataSources;  

  490.       

  491.     public MultiDataSource(){  

  492.         mappedDataSources = new HashMap<String , DataSource>(4);  

  493.         clusterDataSources = new ArrayList<DataSource>(4);  

  494.     }  

  495.       

  496.     /** 

  497.      * 数据库连接池初始化 

  498.      * 该方法通常在web 应用启动时调用 

  499.      */  

  500.     public void initialMultiDataSource(){  

  501.         for(DataSource ds : clusterDataSources){  

  502.             if(ds != null){  

  503.                 Connection conn = null;  

  504.                 try {  

  505.                     conn = ds.getConnection();                    

  506.                 } catch (SQLException e) {  

  507.                     e.printStackTrace();  

  508.                 } finally{  

  509.                     if(conn != null){  

  510.                         try {  

  511.                             conn.close();  

  512.                         } catch (SQLException e) {  

  513.                             e.printStackTrace();  

  514.                         }  

  515.                         conn = null;  

  516.                     }  

  517.                 }  

  518.             }  

  519.         }  

  520.         Collection<DataSource> dsCollection = mappedDataSources.values();  

  521.         for(DataSource ds : dsCollection){  

  522.             if(ds != null){  

  523.                 Connection conn = null;  

  524.                 try {  

  525.                     conn = ds.getConnection();  

  526.                 } catch (SQLException e) {  

  527.                     e.printStackTrace();  

  528.                 } finally{  

  529.                     if(conn != null){  

  530.                         try {  

  531.                             conn.close();  

  532.                         } catch (SQLException e) {  

  533.                             e.printStackTrace();  

  534.                         }  

  535.                         conn = null;  

  536.                     }  

  537.                 }  

  538.             }  

  539.         }  

  540.     }  

  541.     /** 

  542.      * 获取当前线程绑定的DataSource 

  543.      * @return 

  544.      */  

  545.     public DataSource getCurrentDataSource() {  

  546.         //如果路由策略存在,且更新过,则根据路由算法选择新的DataSource  

  547.         RouterStrategy strategy = DataSourceRouter.currentRouterStrategy.get();  

  548.         if(strategy == null){  

  549.             throw new IllegalArgumentException("DataSource RouterStrategy No found.");  

  550.         }         

  551.         if(strategy != null && strategy.isRefresh()){             

  552.             if(RouterStrategy.SRATEGY_TYPE_MAP.equals(strategy.getType())){  

  553.                 this.choiceMappedDataSources(strategy.getKey());  

  554.                   

  555.             }else if(RouterStrategy.SRATEGY_TYPE_CLUSTER.equals(strategy.getType())){  

  556.                 this.routeClusterDataSources(strategy.getRouteFactor());  

  557.             }             

  558.             strategy.setRefresh(false);  

  559.         }  

  560.         return currentDataSourceHolder.get();  

  561.     }  

  562.   

  563.     public Map<String, DataSource> getMappedDataSources() {  

  564.         return mappedDataSources;  

  565.     }  

  566.   

  567.     public void setMappedDataSources(Map<String, DataSource> mappedDataSources) {  

  568.         this.mappedDataSources = mappedDataSources;  

  569.     }  

  570.   

  571.     public ArrayList<DataSource> getClusterDataSources() {  

  572.         return clusterDataSources;  

  573.     }  

  574.   

  575.     public void setClusterDataSources(ArrayList<DataSource> clusterDataSources) {  

  576.         this.clusterDataSources = clusterDataSources;  

  577.     }  

  578.       

  579.     /** 

  580.      * 使用Key选择当前的数据源 

  581.      * @param key 

  582.      */  

  583.     public void choiceMappedDataSources(String key){  

  584.         DataSource ds = this.mappedDataSources.get(key);  

  585.         if(ds == null){  

  586.             throw new IllegalStateException("No Mapped DataSources Exist!");  

  587.         }  

  588.         this.currentDataSourceHolder.set(ds);  

  589.     }  

  590.       

  591.     /** 

  592.      * 使用取模算法,在群集数据源中做路由选择 

  593.      * @param routeFactor 

  594.      */  

  595.     public void routeClusterDataSources(int routeFactor){  

  596.         int size = this.clusterDataSources.size();  

  597.         if(size == 0){  

  598.             throw new IllegalStateException("No Cluster DataSources Exist!");  

  599.         }  

  600.         int choosen = routeFactor % size;  

  601.         DataSource ds = this.clusterDataSources.get(choosen);  

  602.         if(ds == null){  

  603.             throw new IllegalStateException("Choosen DataSources is null!");  

  604.         }  

  605.         logger.debug("Choosen DataSource No." + choosen+ " : " + ds.toString());  

  606.         this.currentDataSourceHolder.set(ds);  

  607.     }  

  608.   

  609.     /* (non-Javadoc) 

  610.      * @see javax.sql.DataSource#getConnection() 

  611.      */  

  612.     public Connection getConnection() throws SQLException {  

  613.         if(getCurrentDataSource() != null){  

  614.             return getCurrentDataSource().getConnection();  

  615.         }  

  616.         return null;  

  617.     }  

  618.   

  619.     /* (non-Javadoc) 

  620.      * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String) 

  621.      */  

  622.     public Connection getConnection(String username, String password)  

  623.             throws SQLException {  

  624.         if(getCurrentDataSource() != null){  

  625.             return getCurrentDataSource().getConnection(username , password);  

  626.         }  

  627.         return null;  

  628.     }  

  629.   

  630.     /* (non-Javadoc) 

  631.      * @see javax.sql.CommonDataSource#getLogWriter() 

  632.      */  

  633.     public PrintWriter getLogWriter() throws SQLException {  

  634.         if(getCurrentDataSource() != null){  

  635.             return getCurrentDataSource().getLogWriter();  

  636.         }  

  637.         return null;  

  638.     }  

  639.   

  640.     /* (non-Javadoc) 

  641.      * @see javax.sql.CommonDataSource#getLoginTimeout() 

  642.      */  

  643.     public int getLoginTimeout() throws SQLException {  

  644.         if(getCurrentDataSource() != null){  

  645.             return getCurrentDataSource().getLoginTimeout();  

  646.         }  

  647.         return 0;  

  648.     }  

  649.   

  650.     /* (non-Javadoc) 

  651.      * @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter) 

  652.      */  

  653.     public void setLogWriter(PrintWriter out) throws SQLException {  

  654.         if(getCurrentDataSource() != null){  

  655.             getCurrentDataSource().setLogWriter(out);  

  656.         }  

  657.     }  

  658.   

  659.     /* (non-Javadoc) 

  660.      * @see javax.sql.CommonDataSource#setLoginTimeout(int) 

  661.      */  

  662.     public void setLoginTimeout(int seconds) throws SQLException {  

  663.         if(getCurrentDataSource() != null){  

  664.             getCurrentDataSource().setLoginTimeout(seconds);  

  665.         }  

  666.     }  

  667.   

  668.     /* (non-Javadoc) 

  669.      * 该接口方法since 1.6 

  670.      * 不是所有的DataSource都实现有这个方法 

  671.      * @see java.sql.Wrapper#isWrapperFor(java.lang.Class) 

  672.      */  

  673.     public boolean isWrapperFor(Class<?> iface) throws SQLException {  

  674.           

  675. //      if(getCurrentDataSource() != null){  

  676. //          return getCurrentDataSource().isWrapperFor(iface);  

  677. //      }  

  678.         return false;  

  679.     }  

  680.   

  681.     /* (non-Javadoc) 

  682.      * 该接口方法since 1.6 

  683.      * 不是所有的DataSource都实现有这个方法 

  684.      * @see java.sql.Wrapper#unwrap(java.lang.Class) 

  685.      */  

  686.     public <T> T unwrap(Class<T> iface) throws SQLException {  

  687. //      if(getCurrentDataSource() != null){  

  688. //          return getCurrentDataSource().unwrap(iface);  

  689. //      }  

  690.         return null;  

  691.     }  

这个类实现了DataSource的标准接口,而最核心的部分是getConnection()方法的重载。下面具体阐述:
  • 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代码

[xml] view plaincopy

  1. <textarea cols="84" rows="15" name="code" class="xhtml"><!-- jdbc连接池-1-->    

  2.   

  3. <bean    id="c3p0_dataSource_1"  class="com.mchange.v2.c3p0.ComboPooledDataSource"   destroy-method="close">        

  4.   

  5.     <property name="driverClass">        

  6.   

  7.         <value>${jdbc.driverClass}</value>        

  8.   

  9.     </property>        

  10.   

  11.    <property name="jdbcUrl">        

  12.   

  13.         <value>${mysql.url_1}</value>        

  14.   

  15.        </property>        

  16.   

  17.     <property name="user">        

  18.   

  19.        value>${jdbc.username}</value>        

  20.   

  21.     </property>        

  22.   

  23.     <property name="password">        

  24.   

  25.         <value>${jdbc.password}</value>        

  26.   

  27.     </property>         

  28.   

  29.     <!--连接池中保留的最小连接数。-->        

  30.   

  31.    <property name="minPoolSize">        

  32.   

  33.            <value>${c3p0.minPoolSize}</value>        

  34.   

  35.        </property>         

  36.   

  37.     <!--连接池中保留的最大连接数。Default: 15 -->        

  38.   

  39.        <property name="maxPoolSize">        

  40.   

  41.         <value>${c3p0.maxPoolSize}</value>        

  42.   

  43.     </property>        

  44.   

  45.     <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->        

  46.   

  47.       <property name="initialPoolSize">        

  48.   

  49.        <value>${c3p0.initialPoolSize}</value>        

  50.   

  51.     </property>      

  52.   

  53.    <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->        

  54.   

  55.        <property name="idleConnectionTestPeriod">        

  56.   

  57.           <value>${c3p0.idleConnectionTestPeriod}</value>        

  58.   

  59.       </property>        

  60.   

  61.    </bean>      

  62.   

  63.         

  64.   

  65. <!------------- jdbc连接池-2------------------->    

  66.   

  67. <bean    id="c3p0_dataSource_2"  class="com.mchange.v2.c3p0.ComboPooledDataSource"   destroy-method="close">        

  68.   

  69.     <property name="driverClass">        

  70.   

  71.       <value>${jdbc.driverClass}</value>        

  72.   

  73.     </property>        

  74.   

  75.    <property name="jdbcUrl">        

  76.   

  77.        <value>${mysql.url_2}</value>        

  78.   

  79.        </property>        

  80.   

  81.     <property name="user">        

  82.   

  83.        <value>${jdbc.username}</value>        

  84.   

  85.    </property>        

  86.   

  87.     <property name="password">        

  88.   

  89.         <value>${jdbc.password}</value>        

  90.   

  91.    </property>         

  92.   

  93.     <!--连接池中保留的最小连接数。-->        

  94.   

  95.     <property name="minPoolSize">        

  96.   

  97.            <value>${c3p0.minPoolSize}</value>        

  98.   

  99.        </property>         

  100.   

  101.     <!--连接池中保留的最大连接数。Default: 15 -->        

  102.   

  103.       <property name="maxPoolSize">        

  104.   

  105.       <value>${c3p0.maxPoolSize}</value>        

  106.   

  107.    </property>        

  108.   

  109.     <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->        

  110.   

  111.       <property name="initialPoolSize">        

  112.   

  113.      <value>${c3p0.initialPoolSize}</value>        

  114.   

  115.     </property>      

  116.   

  117.   <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->        

  118.   

  119.      <property name="idleConnectionTestPeriod">        

  120.   

  121.           <value>${c3p0.idleConnectionTestPeriod}</value>        

  122.   

  123.        </property>        

  124.   

  125.   </bean>    

  126.   

  127.     

  128.   

  129.   <!------------- 更多的链接池配置------------------->    

  130.   

  131.    ......    

  132.   

  133.     <!-- jdbc连接池-1-->  

  134.     <bean    id="c3p0_dataSource_1"  class="com.mchange.v2.c3p0.ComboPooledDataSource"   destroy-method="close">     

  135.         <property name="driverClass">     

  136.             <value>${jdbc.driverClass}</value>     

  137.         </property>     

  138.         <property name="jdbcUrl">     

  139.             <value>${mysql.url_1}</value>     

  140.         </property>     

  141.         <property name="user">     

  142.             <value>${jdbc.username}</value>     

  143.         </property>     

  144.         <property name="password">     

  145.             <value>${jdbc.password}</value>     

  146.         </property>      

  147.         <!--连接池中保留的最小连接数。-->     

  148.         <property name="minPoolSize">     

  149.             <value>${c3p0.minPoolSize}</value>     

  150.         </property>      

  151.         <!--连接池中保留的最大连接数。Default: 15 -->     

  152.         <property name="maxPoolSize">     

  153.             <value>${c3p0.maxPoolSize}</value>     

  154.         </property>     

  155.         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->     

  156.         <property name="initialPoolSize">     

  157.             <value>${c3p0.initialPoolSize}</value>     

  158.         </property>   

  159.         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->     

  160.         <property name="idleConnectionTestPeriod">     

  161.             <value>${c3p0.idleConnectionTestPeriod}</value>     

  162.         </property>     

  163.     </bean>   

  164.       

  165.     <!------------- jdbc连接池-2------------------->  

  166.     <bean    id="c3p0_dataSource_2"  class="com.mchange.v2.c3p0.ComboPooledDataSource"   destroy-method="close">     

  167.         <property name="driverClass">     

  168.             <value>${jdbc.driverClass}</value>     

  169.         </property>     

  170.         <property name="jdbcUrl">     

  171.             <value>${mysql.url_2}</value>     

  172.         </property>     

  173.         <property name="user">     

  174.             <value>${jdbc.username}</value>     

  175.         </property>     

  176.         <property name="password">     

  177.             <value>${jdbc.password}</value>     

  178.         </property>      

  179.         <!--连接池中保留的最小连接数。-->     

  180.         <property name="minPoolSize">     

  181.             <value>${c3p0.minPoolSize}</value>     

  182.         </property>      

  183.         <!--连接池中保留的最大连接数。Default: 15 -->     

  184.         <property name="maxPoolSize">     

  185.             <value>${c3p0.maxPoolSize}</value>     

  186.         </property>     

  187.         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->     

  188.         <property name="initialPoolSize">     

  189.             <value>${c3p0.initialPoolSize}</value>     

  190.         </property>   

  191.         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->     

  192.         <property name="idleConnectionTestPeriod">     

  193.             <value>${c3p0.idleConnectionTestPeriod}</value>     

  194.         </property>     

  195.     </bean>  

  196.   

  197.     <!------------- 更多的链接池配置------------------->  

  198.     ......  

  199.   

  200. </textarea>   



STEP 2。将多个数据源都注入到MultiDataSource中

Xml代码

 

 

[xhtml] view plaincopy

  1.   <bean id="multiDataSource"    class="com.xxx.sql.MultiDataSource">    

  2.   

  3.     <property name="clusterDataSources">    

  4.   

  5.       <list>    

  6.   

  7.           <ref bean="c3p0_dataSource_1" />    

  8.   

  9.         <ref bean="c3p0_dataSource_2" />    

  10.   

  11.           <ref bean="c3p0_dataSource_3" />    

  12.   

  13.           <ref bean="c3p0_dataSource_4" />    

  14.   

  15.         <ref bean="c3p0_dataSource_5" />    

  16.   

  17.           <ref bean="c3p0_dataSource_6" />    

  18.   

  19.            <ref bean="c3p0_dataSource_7" />    

  20.   

  21.             <ref bean="c3p0_dataSource_8" />    

  22.   

  23.         </list>    

  24.   

  25.     </property>    

  26.   

  27.     <property name="mappedDataSources">    

  28.   

  29.        <map>    

  30.   

  31.            <entry key="system" value-ref="c3p0_dataSource_system" />    

  32.   

  33.         </map>    

  34.   

  35.    </property>    

  36.   

  37. </bean>    

  38.   

  39.     <bean id="multiDataSource"   class="com.xxx.sql.MultiDataSource">  

  40.         <property name="clusterDataSources">  

  41.             <list>  

  42.                 <ref bean="c3p0_dataSource_1" />  

  43.                 <ref bean="c3p0_dataSource_2" />  

  44.                 <ref bean="c3p0_dataSource_3" />  

  45.                 <ref bean="c3p0_dataSource_4" />  

  46.                 <ref bean="c3p0_dataSource_5" />  

  47.                 <ref bean="c3p0_dataSource_6" />  

  48.                 <ref bean="c3p0_dataSource_7" />  

  49.                 <ref bean="c3p0_dataSource_8" />  

  50.             </list>  

  51.         </property>  

  52.         <property name="mappedDataSources">  

  53.             <map>  

  54.                 <entry key="system" value-ref="c3p0_dataSource_system" />  

  55.             </map>  

  56.         </property>  

  57.     </bean>  

[xml] view plaincopy

  1.    

 



STEP 3。像使用标准的DataSource一样,使用MultiDataSource

Xml代码

[xml] view plaincopy

  1. <textarea cols="84" rows="15" name="code" class="xhtml"><!--  iBatis Client配置 将 MultiDataSource 与iBatis Client 绑定-->    

  2.   

  3. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">    

  4.   

  5.         <property name="configLocation" value="classpath:SqlMapConfig.xml"/>    

  6.   

  7.     <property name="dataSource" ref="multiDataSource"></property>    

  8.   

  9. </bean>    

  10.   

  11.     

  12.   

  13. <!-- jdbc事务管理配置 将 MultiDataSource 与事务管理器绑定-->    

  14.   

  15. <bean id="jdbc_TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    

  16.   

  17.   <property name="dataSource" ref="multiDataSource"></property>    

  18.   

  19. </bean>    

  20.   

  21.     <!--  iBatis Client配置 将 MultiDataSource 与iBatis Client 绑定-->  

  22.     <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  

  23.         <property name="configLocation" value="classpath:SqlMapConfig.xml"/>  

  24.         <property name="dataSource" ref="multiDataSource"></property>  

  25.     </bean>  

  26.       

  27.     <!-- jdbc事务管理配置 将 MultiDataSource 与事务管理器绑定-->  

  28.     <bean id="jdbc_TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  

  29.         <property name="dataSource" ref="multiDataSource"></property>  

  30.     </bean>  

  31.   

  32. </textarea>   



至此,我们的程序就可以让Spring来管理多库访问了,但请注意,数据库事务仍然限于单库范围(之前已经说过,这里的应用场景不存在跨库的事务)。


6.Java代码使用例子
首先要说明的是,这里我们只是提供了一个简单的使用范例,在范例中,我们还必须手动的调用API,以确定DataSource的路由规则,在实际的应用中,您可以针对自己的业务特点,对此进行封装,以实现相对透明的路由选择

Java代码

[java] view plaincopy

  1. <textarea cols="81" rows="15" name="code" class="java">public boolean addUserGameInfo(UserGameInfo userGameInfo){     

  2.   

  3.     //1.根据UserGameInfo.uid 进行数据源路由选择       DataSourceRouter.setRouterStrategy(     

  4.   

  5.            RouterStrategy.SRATEGY_TYPE_CLUSTER ,     

  6.   

  7.            null,     

  8.   

  9.            userGameInfo.getUid());     

  10.   

  11.          

  12.   

  13.     //2.数据库存储     

  14.   

  15.     try {     

  16.   

  17.        userGameInfoDAO.insert(userGameInfo);     

  18.   

  19.        return true;     

  20.   

  21.     } catch (SQLException e) {     

  22.   

  23.         e.printStackTrace();     

  24.   

  25.         logger.debug("Insert UserGameInfo failed. " + userGameInfo.toString());     

  26.   

  27.     }     

  28.   

  29.     return false;     

  30.   

  31. }    

  32.   

  33.     public boolean addUserGameInfo(UserGameInfo userGameInfo){  

  34.         //1.根据UserGameInfo.uid 进行数据源路由选择  

  35.         DataSourceRouter.setRouterStrategy(  

  36.                 RouterStrategy.SRATEGY_TYPE_CLUSTER ,  

  37.                 null,  

  38.                 userGameInfo.getUid());  

  39.           

  40.         //2.数据库存储  

  41.         try {  

  42.             userGameInfoDAO.insert(userGameInfo);  

  43.             return true;  

  44.         } catch (SQLException e) {  

  45.             e.printStackTrace();  

  46.             logger.debug("Insert UserGameInfo failed. " + userGameInfo.toString());  

  47.         }  

  48.         return false;  

  49.     }  

  50.   

  51. </textarea>  



OK,我们的多库横向切分的实验可以暂告一个段落。实际上,要实现一个完整的DAL是非常庞大的工程,而对我们推动巨大的,可能只是很小的一个部分,到处都存在着8-2法则,要如何选择,就看各位看官了!!

转载于:https://my.oschina.net/sniperLi/blog/504113

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值