Skip to content

配置示例

王祎 edited this page Aug 28, 2013 · 1 revision
<util:set id="shardSet" set-class="java.util.LinkedHashSet">  
    <ref local="shard1"></ref>
	<ref local="shard2"></ref> 
</util:set>  

<!-- 数据源1 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    
    <property name="filters" value="stat" />
 
    <property name="maxActive" value="${jdbc.maxActive}" />
    <property name="initialSize" value="1" />
    <property name="maxWait" value="${jdbc.maxIdle}" />
    <property name="minIdle" value="1" />
 
    <property name="timeBetweenEvictionRunsMillis" value="3000" />
    <property name="minEvictableIdleTimeMillis" value="300000" />
 
    <property name="validationQuery" value="SELECT 'x'" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
     
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
</bean>

<!-- 数据源2 -->
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
    <property name="url" value="${jdbc1.url}" />
    <property name="username" value="${jdbc1.username}" />
    <property name="password" value="${jdbc1.password}" />
     
    <property name="filters" value="stat" />
 
    <property name="maxActive" value="${jdbc1.maxActive}" />
    <property name="initialSize" value="5" />
    <property name="maxWait" value="${jdbc1.maxIdle}" />
    <property name="minIdle" value="5" />
 
    <property name="timeBetweenEvictionRunsMillis" value="3000" />
    <property name="minEvictableIdleTimeMillis" value="300000" />
 
    <property name="validationQuery" value="SELECT 'x'" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
     
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
</bean>

<!-- 定义数据分片 -->
<bean id="shard1" class="com.alibaba.cobarclient.Shard">
	<property name="id" value="shard1"></property>
	<property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="shard2" class="com.alibaba.cobarclient.Shard">
	<property name="id" value="shard2"></property>
	<property name="dataSource" ref="dataSource1"></property>
</bean>

<util:map id="functionsMap">
	<entry key="hash" value-ref="hashFunction">
	</entry>
</util:map>

<!-- 定义总router -->
<bean id="router" class="com.alibaba.cobarclient.config.SimpleRouterFactoryBean">
	<property name="configLocation" value="classpath:/dbrule/sharding-rules-on-namespace.xml"></property>
	<property name="functions" ref="functionsMap"></property>
	<property name="shards" ref="shardSet"></property>
</bean>

<bean id="sqlMapClient"
	class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
	<property name="configLocation">
		<value>classpath:sqlmap-config.xml</value>
	</property>
	<property name="mappingLocations">
		<value>classpath*:/ibatis/sqlmap/*.xml</value>
	</property>
</bean>

<bean id="hashFunction" class="com.raycloud.db.router.rules.support.HashFunction"></bean>

<!-- 工程里一定要使用此工程模板,不能再使用ibatis原生的api,不然有的情况会不经过cobar的过滤 -->
<bean id="sqlMapClientTemplate" class="com.alibaba.cobarclient.MysdalSqlMapClientTemplate">
	<property name="sqlMapClient" ref="sqlMapClient" />
	<property name="shards" ref="shardSet"></property>
	<property name="router" ref="router"></property>
</bean>

<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>

<aop:aspectj-autoproxy proxy-target-class="true" />

<bean id="txManager"
	class="com.alibaba.cobarclient.transaction.BestEffortMultiDataSourceTransactionManager">
	<property name="shards" ref="shardSet"></property>
</bean>
Clone this wiki locally