开发者

Dynamic DataSource Routing

Sorry for my bad English. I wrote implementation for AbstractRoutingDataSource:

public class DatabaseRoutingDataSource extends AbstractRoutingDataSource{

    @Override
    protected Object determineCurrentLookupKey() {      
        return DatabaseContextHolder.getDatabaseType();
    }

}

And I created new class for switching between databases:

public class DatabaseContextHolder {

    private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<DatabaseType>();

    public static void setDatabaseType(DatabaseType databaseType) {
        contextHolder.set(databaseType);
    }

    public static DatabaseType getDatabaseType() {
        return (DatabaseType) contextHolder.get();
    }

    public static void clearDatabaseType() {
        contextHolder.remove();
    }
}

where DatabaseType is:

public enum DatabaseType {
    MAIN,
    BACKUP
}

in my beans.xml:

<bean id="mainDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:/jdbc/DBMIDS"/>
</bean>
<bean id="backupDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:/jdbc/DBMIDS2"/>
</bean>
<bean id="dataSource" class="DatabaseRoutingDataSource">
    <property name="targetDataSources">
        <map key-type="DatabaseType">
            <entry key="MAIN" value-ref="mainDataSource"/>
            <entry key="BACKUP" value-ref="backupDataSource"/>
        </map>
    </property>
    <property name="defaultTargetDataSource" ref="mainDataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<bean id="databaseTarget" class="DatabaseBean">
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean id="database" class="org.springframework.transaction开发者_如何学C.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="target" ref="databaseTarget"/>
    <property name="proxyInterfaces">
        <value>Database</value>
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="*">PROPAGATION_REQUIRED,-MyException</prop>
        </props>
    </property>
</bean>

Now, when I am trying change datasource in my DAO:

public class DatabaseBean extends JdbcDaoSupport implements Database
    public void myMethod() {
       DatabaseContextHolder.setDatabaseType(DatabaseType.MAIN);
       getJdbcTemplate().execute("INSERT INTO test(test) VALUES('test')");      
       DatabaseContextHolder.setDatabaseType(DatabaseType.BACKUP);
       getJdbcTemplate().execute("INSERT INTO test(test) VALUES('test')");        
}

determineCurrentLookupKey() called once, when first executed getJdbcTemplate(), and datasource do not switch.


Part of Spring's transaction management for JDBC transactions is to bind the connection to the thread upon a transaction start. Until the transaction ends and the connection is unbound, every persistence operation on the same datasource will use that same connection. Because you're using a single datasource to mask two others, you'll only ever get the one connection. If you explicitly used two separate datasources, each would be seen as a separate resource, and a separate connection would be started and bound to the thread for each. See "Synchronizing resources with transactions" in the reference guide for at least a hint of what's going on under the hood when you use transactions with a JdbcDaoSupport and JdbcTemplate.


You should be calling DatabaseContextHolder.setDatabaseType(DatabaseType.MAIN) before method call. Hibernate looks for the connection once transaction is started . In your case Transaction is started before DatabaseContextHolder.setDatabaseType(DatabaseType.MAIN) got executed. And once transaction has started schema can not be changed. Your implementation is correct try setting DatabaseType before method call i.e. before transaction is started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜