springmvc mysql 动态切换数据源

在Spring MVC项目中,通常通过配置多个dataSource来实现动态切换数据源。每个dataSource对应一个数据库,但配置多个sessionFactory并不符合开闭原则。正确做法是在后台通过切换不同的dataSource连接不同数据库。配置过程中可能出现的错误包括value元素间空格、value换行以及读取jdbc.properties变量值的问题。解决这些问题可以确保数据源配置的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring mvc中可以将数据库源配置在applicationContext.xml文件dataSource中,一个dataSource对应一个数据库源,绑定到sessionFactory中,在后台dao层通过sessionFactory连接数据库。若项目中有多个数据源,那若配置多个sessionFactory显然不符合开闭原则,正确的是配置多个dataSource,通过在sessionFactory关联不同的dataSource连接不同的数据库。


下面是applicationContext.xml的配置

首先,配置多个dataSource

<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${url1}" />  
		<property name="driverClass" value="${driverClassName}"/>  
        <property name="user" value="${usernames}"/>  
        <property name="password" value="${passwords}"/>  
	</bean>
	<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${url2}" />  
		<property name="driverClass" value="${driverClassName}" />  
        <property name="user" value="videoread"/>  
        <property name="password" value="2sd5j4fcg0h"/>  
</bean>	
配置dynamicDataSource

	<bean id="dynamicDataSource" class="com.sohu.util.DynamicDataSource">
		<property name="targetDataSources">
			<map key-type="java.lang.String">  
                <entry value-ref="dataSource1" key="dataSource1"></entry>  
                <entry value-ref="dataSource2" key="dataSource2"></entry>  
            </map> 
		</property>
		<property name="defaultTargetDataSource" ref="dataSource1"></property>
	</bean>
配置sessionFactory

	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dynamicDataSource"/>
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean>
配置transactionManger

	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>
DynamicDataSource类

public class DynamicDataSource extends AbstractRoutingDataSource{
    public static final String DATA_SOURCE_1 = "dataSource1";
    public static final String DATA_SOURCE_2 = "dataSource2";
    
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
    public static void setCustomerType(String customerType) {
    	contextHolder.set(customerType);
    }
    public static String getCustomerType() {
    	return contextHolder.get();
    }
    public static void clearCustomerType() {
    	contextHolder.remove();
    }
    @Override
    protected Object determineCurrentLookupKey() {
    	System.out.println(getCustomerType());
    	return getCustomerType();
    }
}

IndexController

对数据源的切换必须在进入业务层之前进行切换

@Controller
@RequestMapping("indexController")
public class IndexController {
	@Resource
	private SessionFactory sessionFactory;
	
	@RequestMapping("dynamicSouce")
	public String dynamicSouce(){
		DynamicDataSource.setCustomerType(DynamicDataSource.DATA_SOURCE_2);
		Session session = sessionFactory.openSession();
		BigInteger count = (BigInteger) session.createSQLQuery("select count(*) from videoinfo").uniqueResult();
		System.out.println(count);
		return "result";
	}
}
报错:A ResourcePool could not acquire a resource from its primary factory or sour

解决:dataSource配置出错

情况1:value元素值之间出现空格(人为添加)

情况2:value结束标签换行(当value值比较长时,Ctrl+Shift+F的时就会换行)

情况3:


1、applicationContext.xml读取jdbc.properties的变量值,${username} ${password}一直报错

2、若在value值里直接填入jdbcURL的值,应该把用于转义的反斜杠去掉


参考:

http://blog.youkuaiyun.com/wangpeng047/article/details/8866239

http://www.itpub.net/thread-1906608-1-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值