ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Sanders <tendan...@gmail.com>
Subject Re: How can I change datasource connect info on the fly w/iBATIS and Spring?
Date Tue, 17 Apr 2007 21:10:05 GMT


Chris Lamey wrote:
> 
> That should work ok, that Map could get modified at servlet init time.
> 

Drat, doesn't appear to, at least not how I've written it. I think there are
two problems - first, the map is already set by the time my servlet gets
created and a call to setTargetDataSources will not overwrite it. Second,
the map is String to String, rather than a key to a dataSource name. But I
don't know how to give my dataSource a name in the context when it doesn't
come from a config file.

Here's what I did - maybe I did something stupid:

my bean def:
   <bean id="mediusDataSource" class="MediusRoutingDataSource">
      <property name="targetDataSources">
          <map>
        <entry key="medius1" value-ref="mediusSource" />
        <entry key="medius2" value-ref="oldSource" />
          </map>
      </property>
   </bean>

public class MediusRoutingDataSource extends AbstractRoutingDataSource
{
	protected static boolean didOnce = false;
	
	@Override
	protected Object determineCurrentLookupKey()
	{
		if (didOnce) 	return "xxxMedius";
		else 	{ didOnce = true; return "medius2";}
	}
}

test code:
	public void testGetBanPoliciesFromDifferentSource()
	{
		BanPolicyManager mgr = (BanPolicyManager)ctx.getBean("banPolicyDAO");
		System.out.println(mgr.fetchBanPolicies()); // uses 'medius2' datasource,
returns empty list

		BasicDataSource newDS = new BasicDataSource();
		newDS.setUsername("psanders");
		newDS.setUrl("jdbc:oracle:thin:@toad002:1521:sid");
		newDS.setPassword("psanders");
		newDS.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		
		MediusRoutingDataSource mds =
(MediusRoutingDataSource)ctx.getBean("mediusDataSource");
		// some code omitted to create a Map<String, BasicDataSource>
		routingMap.put("xxxMedius", newDS); // add new datasource to the routing
map
		mds.setTargetDataSources(routingMap);

		System.out.println(mgr.fetchBanPolicies()); // now uses 'xxxMedius'
datasource
		// exception raised java.lang.IllegalStateException: Cannot determine
target DataSource for lookup key [xxxMedius]
	}
-- 
View this message in context: http://www.nabble.com/How-can-I-change-datasource-connect-info-on-the-fly-w-iBATIS-and-Spring--tf3573169.html#a10047766
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Mime
View raw message