simple router :
Code:
String current = "one";
Log logger = LogFactory.getLog(getClass());
protected Object determineCurrentLookupKey() {
if(current == "one"){
current = "two";
}else if(current == "two"){
current = "three";
}else if(current == "three"){
current = "one";
}
logger.info(current);
return current;
}
configuration :
Code:
<jdbc:embedded-database id="datasource-one" type="H2"/>
<jdbc:embedded-database id="datasource-two" type="H2"/>
<jdbc:embedded-database id="datasource-three" type="H2"/>
<util:map id="datasource-map">
<entry key="one" value-ref="datasource-one"/>
<entry key="two" value-ref="datasource-two"/>
<entry key="three" value-ref="datasource-three"/>
</util:map>
<bean id="datasource-lookup" class="org.springframework.jdbc.datasource.lookup.MapDataSourceLookup"
p:dataSources-ref="datasource-map"
/>
<bean id="datasource" class="SimpleDataSourceRouter"
p:dataSourceLookup-ref="datasource-lookup"
p:targetDataSources-ref="datasource-map"
/>
<bean id="jdbc-template" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="datasource"
/>
test:
Code:
@Inject JdbcTemplate template;
@Test
public void read(){
template.execute("select 1");
template.execute("select 1");
template.execute("select 1");
}
results :
Code:
INFO EmbeddedDatabaseFactory - Creating embedded database 'datasource-one'
INFO EmbeddedDatabaseFactory - Creating embedded database 'datasource-two'
INFO EmbeddedDatabaseFactory - Creating embedded database 'datasource-three'
DEBUG JdbcTemplate - Executing SQL statement [select 1]
DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
INFO ThreadLocalDataSourceRouter - two
DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-two;DB_CLOSE_DELAY=-1]
DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
DEBUG JdbcTemplate - Executing SQL statement [select 1]
DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
INFO ThreadLocalDataSourceRouter - three
DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-three;DB_CLOSE_DELAY=-1]
DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
DEBUG JdbcTemplate - Executing SQL statement [select 1]
DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
INFO ThreadLocalDataSourceRouter - one
DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-one;DB_CLOSE_DELAY=-1]
DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
INFO GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@f01a1e: ; root of context hierarchy
INFO DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b815bfb: defining beans [datasource-one,datasource-two,datasource-three,datasource-map,datasource-lookup,datasource,jdbc-template,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-three;DB_CLOSE_DELAY=-1]
DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-two;DB_CLOSE_DELAY=-1]
DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-one;DB_CLOSE_DELAY=-1]
i think it works