【问题标题】:Changing datasource connection url runtime更改数据源连接 url 运行时
【发布时间】:2013-01-11 09:53:44
【问题描述】:

我正在开发一个使用 spring + hibernate + mysql 和 c3p0 进行连接池的项目。

目前,连接池的属性是通过在 src 之外定义的属性加载的。 (例如:${db_uname})

当我们创建 spring bean 时,一切都开始正常。

我们连接的数据库可能由于某种原因无法访问,我们想切换主机。

需要实现一个回调,它应该连接到新主机并重新初始化池

任何关于如何优雅地覆盖现有数据源/连接池的指针都会有很大帮助。

这是我的 spring 配置文件的样子

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!-- Component scans -->
<import resource="component-scans-1.xml" />
<import resource="component-scans-2.xml" />


<util:properties id="serviceManagerProperties" 
    location="classpath:servicemanagers.properties" />

<!-- Properties file -->
<context:property-placeholder location="classpath:database.config.properties,classpath:framework.properties" />

<!-- context:annotation-config / -->
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<context:mbean-server id="mbeanServer" />
<context:mbean-export server="mbeanServer" default-domain="a.b.c" />

<bean id="cacheManager" factory-method="getInstance"
    class="net.sf.ehcache.CacheManager" />

<bean class="net.sf.ehcache.management.ManagementService" init-method="init">
    <constructor-arg ref="cacheManager" />
    <constructor-arg ref="mbeanServer" />
    <constructor-arg value="false" />
    <constructor-arg value="false" />
    <constructor-arg value="false" />
    <constructor-arg value="true" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="jpaAdapter" />
    <property name="persistenceXmlLocation" value="classpath*:META-INF/framework-persistence.xml" />
    <property name="persistenceUnitName" value="PU-NAME" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
            <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
            <prop key="hibernate.cache.region.factory_class">
                org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
            </prop>

            <prop key="hibernate.ejb.cfgfile">hibernate.cfg.xml</prop>
            <prop key="hibernate.generate_statistics">true</prop>


            <!-- CONNECTION SETTINGS -->
            <prop key="hibernate.connection.driver_class">
                com.mysql.jdbc.Driver
            </prop>
            <prop key="hibernate.connection.url">
                jdbc:mysql://${dbhost}/${dbschema}?zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=UTF-8
            </prop>
            <prop key="hibernate.connection.username">${dbuser}</prop>
            <prop key="hibernate.connection.password">${dbpass}</prop>

            <!-- CONNECTION POOLING -->
            <prop key="hibernate.connection.provider_class">
                org.hibernate.connection.C3P0ConnectionProvider
            </prop>
            <prop key="hibernate.c3p0.maxPoolSize">${hibernate.c3p0.maxSize}</prop>
            <prop key="hibernate.c3p0.minPoolSize">${hibernate.c3p0.minSize}</prop>
            <prop key="hibernate.c3p0.acquireIncrement">${hibernate.c3p0.acquireIncrement}</prop>
            <prop key="hibernate.c3p0.idleConnectionTestPeriod">${hibernate.c3p0.idleTestPeriod}</prop>
            <prop key="hibernate.c3p0.maxStatements">${hibernate.c3p0.maxStatements}</prop>
            <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>

        </props>
    </property>
</bean>

<bean id="jpaAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

假设数据库架构是正确的,假设我在事件中获取了数据库凭据和主机信息,我需要“重新设置”连接池。

【问题讨论】:

    标签: spring hibernate datasource connection-pooling


    【解决方案1】:

    AbstractRoutingDataSource 是个不错的选择。

    XML 或注解使用如下:

    <bean id="ds1" class="..c3p0.DataSource">
        ...
    </bean>
    
    <bean id="ds2" class="..c3p0.DataSource">
        ...
    </bean>
    
    <bean id="dataSource" class="..xxx.RoutingDataSource">
       <property name="targetDataSources">
          <map key-type="java.lang.String">
             <entry key="ds1" value-ref="ds1"/>
             <entry key="ds2" value-ref="ds2"/>             
          </map>
       </property>
       <property name="defaultTargetDataSource" ref="ds1"/>
    </bean>
    
    
    
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" /> 
    ...
    </bean>
    

    然后构建一个类来确定当前的数据源。

    public class RoutingDataSource extends AbstractRoutingDataSource {
        private static final ThreadLocal<String> holder = new ThreadLocal<String>();
    
        protected Object determineCurrentLookupKey()
        {
            return holder.get();
        }
    
        public static void clear(){
            holder.remove();
        }
    
        public static void setDataSourceKey(String key){
            holder.set(key);
        }
    
    }
    

    顺便说一句,'try-finally' 语句很无聊!

    try{
        RoutingDataSource.setDataSourceKey("ds1");
        myDao.doXXX();
    }finally{
        RoutingDataSource.clear();
    }
    

    【讨论】:

    • 抽象路由数据源将帮助我路由到现有数据源之一。我们的要求是从现在开始使用不同的数据源,它应该在事件发生时创建。
    • 我已将 spring 配置文件添加为问题的一部分
    • RoutingDataSource 可以动态修改数据源映射。检查 AbstractRoutingDataSource API。
    • 这太棒了!非常感谢。
    【解决方案2】:
    <beans:bean id="dataSource"
        class="org.springframework.aop.framework.ProxyFactoryBean">
        <beans:property name="targetSource" ref="swappableDataSource" />
    </beans:bean>
    
    <beans:bean id="dummyDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close" />
    
    <beans:bean name="swappableDataSource"
        class="org.springframework.aop.target.HotSwappableTargetSource">
        <beans:constructor-arg ref="dummyDataSource" />
    </beans:bean>
    

    在你的代码中的某些地方你可以做到这一点。

    @Autowired
    HotSwappableTargetSource swapable;
    
    public void changeDatasource() throws Exception
    {
                swapable.swap(createNewSource();
    
    }
    
    
    ComboPooledDataSource createNewSource() throws Exception {
        ComboPooledDataSource ds2 = new ComboPooledDataSource();
        ds2.setJdbcUrl(url);
        ds2.setDriverClass(driver);
        ds2.setUser(username);
        ds2.setPassword(password);
    
    
        return ds2;
    }
    

    【讨论】:

      【解决方案3】:

      如果您要求多个数据库连接 .. 可以通过创建多个 hibernate.cfg.file 来使用休眠,这有些不合适

      您可以通过以下方式实现。

      SessionFactory sf = new Configuration().configure("somename.cfg.xml").buildSessionFactory();
      

      当您的主要连接未建立时,您已加载另一个 hibernate configuration ...否则将不可能。

      【讨论】:

      • cfg.xml 不是 hibernate.ejb.cfgfile 的配置文件,它只定义了 l2 缓存配置吗?
      猜你喜欢
      • 2022-06-12
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      相关资源
      最近更新 更多