分庫管理

配置兩個數據源 + 一個 數據路由器java

 1   <!-- 數據源(主庫) -->                             
 2      <bean id="dataSource_main" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 3          <property name="jdbcUrl" value="${jdbc.url}"></property>
 4          <property name="user" value="${jdbc.username}"></property>
 5          <property name="password" value="${jdbc.password}"></property>
 6          <property name="driverClass" value="${jdbc.driverClass}"></property>
 7          
 8          <property name="maxPoolSize" value="${c3p0.pool.size.maxsize}"></property>
 9          <property name="minPoolSize" value="${c3p0.pool.size.minsize}"></property>
10          <property name="initialPoolSize" value="${c3p0.pool.size.init}"></property>
11          <property name="acquireIncrement" value="${c3p0.pool.size.increment}"></property>
12      </bean>
13      
14      <!-- 數據源(從庫 繼承主庫) -->
15      <bean id="dataSource_1" parent="dataSource_main">
16          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/surveypark_1"></property>
17      </bean>
18      
19      <!-- 數據路由器 -->
20      <bean id="datasource_router" class="kite.datasource.SurveyparkDataSourceRouter">
21          <property name="targetDataSources">
22              <map>
23                  <entry key="odd" value-ref="dataSource_main" />
24                  <entry key="even" value-ref="dataSource_1"></entry>
25              </map>
26          </property>
27         <property name="defaultTargetDataSource" ref="dataSource_main"></property>
28      </bean>
29      
30      <!-- 
31  本地會話工廠bean,spring整合hibernate的核心入口 32       -->
33      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
34          <!-- 注入數據源 -->
35          <property name="dataSource" ref="datasource_router"></property>
36          <!-- 指定hibernate配置文件的位置 -->
37          <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
38          <!-- 指定映射文件的位置 -->
39          <property name="mappingDirectoryLocations">
40              <list>
41                  <value>classpath:kite/domain</value>
42              </list>
43          </property>
44      </bean>    

建立一個令牌類
 1 package kite.datasource;  2 
 3 import kite.domain.Survey;  4 
 5 /**
 6  * 調查令牌,綁定到當前的線程,傳播到數據源路由器.進行分庫判斷  7  */
 8 public class SurveyToken  9 { 10     private Survey survey; 11 
12     private static ThreadLocal<SurveyToken> t = new ThreadLocal<SurveyToken>(); 13     
14     /**
15  * 將令牌對象綁定到當前線程 16      */
17     public static void bindingToken(SurveyToken surveyToken) 18  { 19  t.set(surveyToken); 20  } 21     
22     /**
23  * 從當前線程得到綁定的令牌對象 24  * @return
25      */
26     public static SurveyToken getCurrentToken() 27  { 28         return t.get(); 29  } 30     
31     /**
32  * 解除令牌的綁定 33  * @return
34      */
35     public static void unbinToken() 36  { 37  t.remove(); 38  } 39     
40     
41     
42     
43     public Survey getSurvey() 44  { 45         return survey; 46  } 47 
48     public void setSurvey(Survey survey) 49  { 50         this.survey = survey; 51  } 52     
53 }

自定義數據源mysql

 1 package kite.datasource;  2 
 3 import java.sql.SQLFeatureNotSupportedException;  4 import java.util.logging.Logger;  5 
 6 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;  7 
 8 /**
 9  * 自定義數據源 10  */
11 public class SurveyparkDataSourceRouter extends AbstractRoutingDataSource 12 { 13 
14     protected Object determineCurrentLookupKey() 15  { 16         SurveyToken token = SurveyToken.getCurrentToken(); 17             
18         if(token != null) 19  { 20             int id = token.getSurvey().getSurveyid(); 21             //解除綁定
22  SurveyToken.unbinToken(); 23             return (id % 2) == 0 ? "even":"odd"; 24  } 25         return null; 26  } 27     
28     public Logger getParentLogger() throws SQLFeatureNotSupportedException 29  { 30         return null; 31  } 32 
33     public <T> T unwrap() 34  { 35         return null; 36  } 37 
38     public boolean isWrapperFor() 39  { 40         return false; 41  } 42 }

在action中進行綁定spring

//綁定到當前線程中
SurveyToken token = new SurveyToken();
token.setSurvey(getCurrentSurvey());
SurveyToken.bindingToken(token);sql

相關文章
相關標籤/搜索