<!-- 分散配置 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置(主库)数据源 --> <bean id="dataSource-main" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverclass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxPoolSize" value="${c3p0.pool.size.max}" /> <property name="minPoolSize" value="${c3p0.pool.size.min}" /> <property name="initialPoolSize" value="${c3p0.pool.size.ini}" /> <property name="acquireIncrement" value="${c3p0.pool.size.increment}" /> </bean> <!-- 配置从库数据源 --> <bean id="dataSource-1" parent="dataSource-main"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/lsn_surveypark001_1" /> </bean> <!-- 数据源路由器 --> <bean id="dataSourceRouter" class="com.surveypark.datasource.SurveyparkDataSourceRouter"> <!-- 目标数据源集合 --> <property name="targetDataSources"> <map> <entry key="odd" value-ref="dataSource-main" /> <entry key="even" value-ref="dataSource-1" /> </map> </property> <!-- 默认数据源集合 --> <property name="defaultTargetDataSource" ref="dataSource-main" /> </bean>
public class SurveyparkDataSourceRouter extends AbstractRoutingDataSource { protected Object determineCurrentLookupKey() { SurveyparkToken token = SurveyparkToken.getCurrentToken(); if(token != null){ Integer id = token.getSurvey().getId(); SurveyparkToken.unbindToken(); return ((id % 2) == 0) ? "even" : "odd" ; } return null; } }
public class SurveyparkToken { private static ThreadLocal<SurveyparkToken> l = new ThreadLocal<SurveyparkToken>(); private Survey survey ; public Survey getSurvey() { return survey; } public void setSurvey(Survey survey) { this.survey = survey; } public static void bindToken(SurveyparkToken token){ l.set(token); } public static void unbindToken(){ l.remove(); } public static SurveyparkToken getCurrentToken(){ return l.get(); } }
SurveyparkToken token = new SurveyparkToken(); token.setSurvey(getCurrentSurvey()); SurveyparkToken.bindToken(token); surveyService.saveAnswers(processAnswers());
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!