Spring+Struts2+hibernate实现动态切换数据源

如题,直接上代码:

applicationContext.xml

    <bean id="defaultDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialPoolSize" value="${pool.initialPoolSize}" />
        <property name="minPoolSize" value="${pool.minPoolSize}" />
        <property name="maxPoolSize" value="${pool.maxPoolSize}" />
        <property name="maxIdleTime" value="${pool.maxIdleTime}" />
        <property name="acquireIncrement" value="${pool.acquireIncrement}" />
        <property name="checkoutTimeout" value="${pool.checkoutTimeout}" />
        <property name="maxIdleTimeExcessConnections" value="${pool.maxIdleTimeExcessConnections}" />
    bean>

     <bean id="dataSource" class="com.naton.core.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry key="dataSourceOracle" value-ref="defaultDataSource" />
            map>
        property>
        <property name="defaultTargetDataSource" ref="defaultDataSource" />
    bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}prop>
                <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}prop>
                <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}prop>
                <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}prop>
                <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}prop>
                <prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProviderprop>
                <prop key="hibernate.search.default.indexBase">${java.io.tmpdir}/shop/indexprop>
            props>
        property>
        <property name="packagesToScan" value="com.xxx" />
    bean>

DynamicDataSource.java

public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return "dataSourceOracle";
    }

    @Override
    protected DataSource determineTargetDataSource() {
        DataSource dataSource = super.determineTargetDataSource();
        try {
            HttpSession session = ServletActionContext.getRequest().getSession();
            if (session == null)
                return dataSource;

            Object dataSourceObj = session.getAttribute("dataSource");
            if (dataSourceObj == null)
                return dataSource;
            else
                dataSource = (DataSource) dataSourceObj;
        } catch (Exception e) {

        }
        return dataSource;
    }
}

login.java

public String login() {
    // 验证密码 略
    try {
        ComboPooledDataSource dataSource = CommonUtil.dataSourceMap.get("username");
        if (dataSource == null) {
            dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
            dataSource.setJdbcUrl("jdbc:oracle:thin:@172.30.70.18:1521:orcl");
            dataSource.setUser("SXNWGSPPORTAL");
            dataSource.setPassword("SXNWGSPPORTAL");

            CommonUtil.dataSourceMap.put("username", dataSource);
        }
        httpSession.setAttribute("dataSource", dataSource);
    } catch (PropertyVetoException e) {
        e.printStackTrace();
    }

    return "login";
}

【总监】十二春秋之,[email protected]
【Master】zelo,[email protected];【运营】运维艄公,[email protected]
【产品设计】流浪猫,[email protected];【体验设计】兜兜,[email protected]
【iOS】淘码小工,[email protected];iMcG33K,[email protected]
【Android】人猿居士,[email protected];思路的顿悟,[email protected]
【Java】首席工程师MR_W,[email protected]【测试】土镜问道,[email protected]
【数据】fox009521,[email protected];【安全】保密,你懂的。

你可能感兴趣的:(java)