实际开发过程中,可能会遇到需要多数据源的场景,比如项目中之前使用的是单一的Oracle数据源,由于image表的数据量过大,因此需要将该表单独迁移至Corba-Mysql上。
首先,在biz-datasource.xml上新增一项imageDataSource:
<bean id="imageDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 驱动不同 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="xxx" />
<property name="username"><value>xxx</value></property>
<property name="password"><value>xxx</value></property>
<property name="maxActive"><value>xxx</value></property>
<property name="initialSize"><value>xxx</value></property>
<property name="maxWait"><value>xxx</value></property>
<property name="maxIdle"><value>xxx</value></property> <!-- 可以和maxActive保持一致 -->
<property name="minIdle"><value>xxx</value></property> <!-- 可以和initialSize保持一致 -->
<property name="removeAbandoned"><value>xxx</value></property>
<property name="removeAbandonedTimeout"><value>xxx</value></property>
<property name="timeBetweenEvictionRunsMillis"><value>xxx</value></property>
<property name="minEvictableIdleTimeMillis"><value>xxx</value></property>
<!-- 注意和oracle设置的参数不同 -->
<property name="connectionProperties"><value>xxx</value></property>
<!-- sql检查配置,在idle空闲线程检查时,检查链接有效性,丢弃无效的链接,实现自动重连-->
<!-- 注意使用cobar时,因为走了F5进行负载,F5默认5分钟会关闭空闲链接,所以需要通过心跳SQL保证数据库链接不被异常关闭 -->
<property name="testWhileIdle"><value>xxx</value></property>
<property name="testOnBorrow"><value>xxx</value></property>
<property name="testOnReturn"><value>xxx</value></property>
<property name="validationQuery"><value>SELECT @@SQL_MODE</value></property>
<property name="numTestsPerEvictionRun"><value>xxx</value></property> <!-- 注意和maxActive保持一致 -->
</bean>
同时,新增sqlmap配置项imageSqlMapClient,指向/ibank/sqlmap-image.xml
<bean id="imageSqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>/ibank/sqlmap-image.xml</value>
</property>
</bean>
其次,新建sqlmap-image.xml,将原来sqlmap.xml中需要改走mysql的映射文件移到sqlmap-image.xml中,如下:
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="false"
maxRequests="3000" maxSessions="3000" maxTransactions="3000" useStatementNamespaces="false" />
<typeHandler javaType="com.alibaba.ibank.biz.dal.enumeration.FeatureOfferTypeEnum" callback="com.alibaba.ibank.biz.dal.enumeration.FeatureOfferTypeHandler" />
<sqlMap resource="ibank/sqlmap-mapping-ibank-image.xml" />
<sqlMap resource="ibank/sqlmap-mapping-ibank-tmp-image.xml" />
<sqlMap resource="ibank/sqlmap-mapping-ibank-non-operating-image.xml" />
</sqlMapConfig>
最后,修改biz-dao.xml,由于spring默认的SqlMapClientDaoSupport含有名为datasource的property,因此需要修改autowire属性为no,如下:
<bean id="winportImageDAO" parent="baseDAOProxyFactoryBean">
<property name="target">
<bean class="com.alibaba.ibank.biz.dal.cache.CachedWinportImageDAO" autowire="no">
<property name="sqlMapClient" ref="imageSqlMapClient" />
<property name="dataSource" ref="imageDataSource" />
<property name="imageSortCache" ref="imageSortCache" />
<property name="dazzleImageCache" ref="dazzleImageCache" />
</bean>
<!--
更新的时候也负责清imageSort和image的cache
-->
</property>
</bean>