---
title: "spring动态路由实践1"
author: "xj84"
date: "2011-08-19T13:37:44Z"
tags: []
categories: ["编程语言"]
slug: "spring-1"
draft: false
lastmod: "2011-08-19T14:20:11Z"
id: 1153310
url: "https://xj84.iteye.com/blog/1153310"
---

最近对动态路由进行了学习，现总结如下：

通过集成org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource类，自定义动态数据源。

配置如下：

datasource-config.xml:

 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans  

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  

http://www.springframework.org/schema/context  

 http://www.springframework.org/schema/context/spring-context-2.5.xsd  

http://www.springframework.org/schema/aop   

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  

http://www.springframework.org/schema/tx   

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;

 

&lt;!-- 数据源配置 --&gt;

&lt;bean id="dataSourceFirst" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close"&gt;

&lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;

&lt;property name="url" value="jdbc:oracle:thin:@10.20.151.4:1521:ptdev" /&gt;

&lt;property name="username" value="pt" /&gt;

&lt;property name="password" value="pt" /&gt;

&lt;property name="maxActive" value="200" /&gt;

&lt;property name="maxIdle" value="5" /&gt;

&lt;property name="poolPreparedStatements" value="true" /&gt;

&lt;property name="removeAbandoned" value="true" /&gt;

&lt;property name="removeAbandonedTimeout" value="300" /&gt;

&lt;/bean&gt;

 

&lt;bean id="dataSourceSecond" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close"&gt;

&lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;

&lt;property name="url" value="jdbc:oracle:thin:@10.20.151.12:1521:pt10g" /&gt;

&lt;property name="username" value="pt" /&gt;

&lt;property name="password" value="pt" /&gt;

&lt;property name="maxActive" value="200" /&gt;

&lt;property name="maxIdle" value="5" /&gt;

&lt;property name="poolPreparedStatements" value="true" /&gt;

&lt;property name="removeAbandoned" value="true" /&gt;

&lt;property name="removeAbandonedTimeout" value="300" /&gt;

&lt;/bean&gt;

 

&lt;bean id="dataSource" class="com.common.bean.RoutingDataSource"&gt;

&lt;property name="targetDataSources"&gt;

&lt;map&gt;

&lt;entry key="1" value-ref="dataSourceFirst" /&gt;

&lt;entry key="2" value-ref="dataSourceSecond" /&gt;

&lt;/map&gt;

&lt;/property&gt;

&lt;property name="defaultTargetDataSource"&gt;

&lt;ref local="dataSourceFirst" /&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;!--配置事物--&gt;

&lt;bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;

&lt;property name="dataSource"&gt;

&lt;ref local="dataSource" /&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

&lt;bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"

lazy-init="true" /&gt;

 

&lt;bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"&gt;

&lt;property name="dataSource" ref="dataSource" /&gt;

&lt;property name="lobHandler" ref="lobHandler" /&gt;

&lt;property name="configLocations" value="classpath\*:/ibatis/config/sql-map.xml" /&gt;

&lt;/bean&gt;

 

&lt;bean id="txAttributeSource"

class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"&gt;

&lt;property name="properties"&gt;

&lt;props&gt;

&lt;prop key="add\*"&gt;PROPAGATION\_REQUIRED,-PtServiceException&lt;/prop&gt;

&lt;prop key="update\*"&gt;PROPAGATION\_REQUIRED,-PtServiceException&lt;/prop&gt;

&lt;prop key="delete\*"&gt;PROPAGATION\_REQUIRED,-PtServiceException&lt;/prop&gt;

&lt;prop key="batch\*"&gt;PROPAGATION\_REQUIRED,-PtServiceException&lt;/prop&gt;

&lt;prop key="get\*"&gt;PROPAGATION\_REQUIRED,-PtServiceException&lt;/prop&gt;

&lt;/props&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

&lt;bean id="transactionManagerProxy" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;

&lt;property name="proxyTargetClass"&gt;

&lt;value&gt;true&lt;/value&gt;

&lt;/property&gt;

&lt;property name="target"&gt;

&lt;ref bean="transactionManager" /&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

&lt;bean id="transactionDefinition"

class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

abstract="true"&gt;

&lt;property name="transactionManager"&gt;

&lt;ref bean="transactionManagerProxy" /&gt;

&lt;/property&gt;

&lt;property name="transactionAttributeSource"&gt;

&lt;ref bean="txAttributeSource" /&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

&lt;bean id="baseDao" class="com.common.dao.impl.BaseDaoImpl"&gt;

&lt;property name="sqlMapClient"&gt;

&lt;ref bean="sqlMapClient" /&gt;

&lt;/property&gt;

&lt;property name="dataSource"&gt;

&lt;ref bean="dataSource" /&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

&lt;bean id="dbInfoService" parent="transactionDefinition"&gt;

&lt;property name="target"&gt;

&lt;bean class="com.service.impl.DbInfoServiceImpl"&gt;

&lt;property name="baseDao" ref="baseDao" /&gt;

&lt;/bean&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;bean id="test" class="com.common.bean.Test"&gt;

&lt;property name="dbInfoService" ref="dbInfoService"&gt;&lt;/property&gt;

&lt;/bean&gt;

 

&lt;/beans&gt;

 

com.common.bean.RoutingDataSource类：

 

package com.common.bean;

 

import java.sql.Connection;

import java.sql.SQLException;

 

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

 

 

public class RoutingDataSource extends AbstractRoutingDataSource {

 

    protected Object determineCurrentLookupKey() {

        //获取当前线程处理的账号对应分片信息

        Shard shard = ThreadInfoHolder.getCurrentThreadShard();

 

        //动态选定DataSource

        String dbId = shard == null ? null : String.valueOf(shard.getDbId());

 

        return dbId;

    }

 

    @Override

    public String toString() {

        //获取当前线程处理的账号对应分片信息

        Shard shard = ThreadInfoHolder.getCurrentThreadShard();

 

        //动态选定DataSource

        String dbId = shard == null ? null : String.valueOf(shard.getDbId());

 

        return "DB ID " + dbId + ":" + super.toString();

    }

 

    public String getTargetDbId() throws SQLException {

        Connection conn = null;

        try {

            //jdbc:oracle:thin:@10.20.151.4:1521:ptdev, UserName=xx, Oracle JDBC driver

            conn = determineTargetDataSource().getConnection();

 

            if (conn != null) {

                String connectionDesc = conn.getMetaData().getURL();

                int beginIdx = connectionDesc.indexOf("@") + 1;

                int endIdx = connectionDesc.indexOf(":", beginIdx);

 

                return connectionDesc.substring(beginIdx, endIdx);

            }

        } finally {

            if (conn != null) {

                conn.close();

            }

        }

 

        return null;

    }

 

}

com.common.bean.ThreadInfoHolder类如下：

package com.common.bean;

public class ThreadInfoHolder {

// thread local， 获取、存储本线程处理的账号对应分片信息

    private static final ThreadLocal&lt;Shard&gt; shardLocal = new ThreadLocal&lt;Shard&gt;();

    /\**

     * 获取当前线程处理的账号对应分片信息

     * 

     * @return

     \*/

    public static Shard getCurrentThreadShard() {

        return ThreadInfoHolder.shardLocal.get();

    }

    /\**

     * 在当前线程存储账号对应分片信息

     * 

     * @param shard

     \*/

    public static void addCurrentThreadShard(Shard shard) {

        ThreadInfoHolder.shardLocal.set(shard);

    }

    /\**

     * 清空前线程存储分片信息

     * 

     * @param shard

     \*/

    public static void cleanCurrentThreadShard() {

        ThreadInfoHolder.shardLocal.remove();

    }

}

com.common.bean.Shard类如下：

public class Shard {

//存放Account数据的DB\_ID

    private Integer            dbId;

 /\**

     * @return the dbId

     \*/

    public Integer getDbId() {

        return dbId;

    }

    /\**

     * @param dbId the dbId to set

     \*/

    public void setDbId(Integer dbId) {

        this.dbId = dbId;

    }

}

com.service.DbInfoService接口：

package com.service;

import java.util.List;

import java.util.Map;

import com.common.dao.model.User;

public interface DbInfoService {

public List&lt;Map&lt;String,Object&gt;&gt; getUserInfo(User user);

}

com.service.impl.DbInfoServiceImpl实现类：

package com.service.impl;

import java.util.List;

import java.util.Map;

import com.common.bean.Shard;

import com.common.bean.ThreadInfoHolder;

import com.common.dao.BaseDao;

import com.common.dao.model.User;

import com.service.DbInfoService;

public class DbInfoServiceImpl implements DbInfoService{

public BaseDao baseDao;

public void setBaseDao(BaseDao baseDao) {

this.baseDao = baseDao;

}

public List&lt;Map&lt;String,Object&gt;&gt; getUserInfo(User user)

{

baseDao.add("login.addUser", user);

List&lt;Map&lt;String,Object&gt;&gt; result=baseDao.getList("login.getUserInfo",user.getName());

return result;

}

}

com.common.bean.Test类：

package com.common.bean;

import java.util.List;

import java.util.Map;

import com.common.dao.model.User;

import com.service.DbInfoService;

public class Test {

public DbInfoService dbInfoService;

public void setDbInfoService(DbInfoService dbInfoService) {

this.dbInfoService = dbInfoService;

}

public List&lt;Map&lt;String,Object&gt;&gt; getInfo(User user)

{

List&lt;Map&lt;String,Object&gt;&gt; result=dbInfoService.getUserInfo(user);

                return result;

}

}

测试main方法如下：

package com.transaction;

import java.util.List;

import java.util.Map;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.core.io.ClassPathResource;

import com.common.bean.Shard;

import com.common.bean.Test;

import com.common.bean.ThreadInfoHolder;

import com.common.dao.model.User;

import com.service.impl.DbInfoServiceImpl;

public class TransactionTest {

public static void main(String\[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath\*:spring/datasource-config.xml");

                Test t=(Test)ctx.getBean("test");

User user=new User();

user.setName("xj");

user.setPassword("123");

Shard shard=new Shard();

shard.setDbId(1);//使用datasource1

ThreadInfoHolder.addCurrentThreadShard(shard);

List&lt;Map&lt;String,Object&gt;&gt; result =t.getInfo(user);

shard.setDbId(2);//使用datasource2

ThreadInfoHolder.addCurrentThreadShard(shard);

List&lt;Map&lt;String,Object&gt;&gt; result1 =t.getInfo(user);

System.out.println(result);

}

}

运行结果是分别向数据库1和数据库2中插入了1调记录。

注意：

由于对DbInfoService配置了事物，如果将切换数据源的代码ThreadInfoHolder.addCurrentThreadShard(shard);放在在DbInfoServiceImpl类的getUserInfo方法中，如下：

public List&lt;Map&lt;String,Object&gt;&gt; getUserInfo(User user)

{

Shard shard=new Shard();

shard.setDbId(1);

ThreadInfoHolder.addCurrentThreadShard(shard);

baseDao.add("login.addUser", user);

shard.setDbId(2);

ThreadInfoHolder.addCurrentThreadShard(shard);

baseDao.add("login.addUser", user);

List&lt;Map&lt;String,Object&gt;&gt; result=baseDao.getList("login.getUserInfo",user.getName());

return result;

}

main方法改为：

public static void main(String\[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath\*:spring/datasource-config.xml");

Test t=(Test)ctx.getBean("test");

User user=new User();

user.setName("xj");

user.setPassword("123");

List&lt;Map&lt;String,Object&gt;&gt; result =t.getInfo(user);

System.out.println(result);

}

则运行结果将是向数据库1中插入2条相同的记录，而不是分别想数据库1,2各插一条记录，产生该结果的原因是应为由于DbInfoServiceImpl配置了事物，所以在getUserInfo方法中的第一次连数据库会新建一个连接，而后将该连接绑定在线程的本地变量即ThreadLoad中，当以后在需要访问数据库时不在新建连接而是使用这个绑定了老连接，在本例子中，即第一次的数据库连接是连数据库1，当第二次访问数据库时，使用的还是这个数据库1的连接，即切换数据源设置代码shard.setDbId(2);ThreadInfoHolder.addCurrentThreadShard(shard);失效。同理我们可以退出，如果将切换数据源代码放在Test类的getInfo方法中，即：

public List&lt;Map&lt;String,Object&gt;&gt; getInfo(User user)

{

Shard shard=new Shard();

shard.setDbId(1);

ThreadInfoHolder.addCurrentThreadShard(shard);

List&lt;Map&lt;String,Object&gt;&gt; result=dbInfoService.getUserInfo(user);

shard.setDbId(2);

ThreadInfoHolder.addCurrentThreadShard(shard);

List&lt;Map&lt;String,Object&gt;&gt; result1=dbInfoService.getUserInfo(user);

return result;

}

这样是能正确运行的，应为在调用事物前我们已经切换了数据源。