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 Fri, 20 Apr 2007 00:54:09 GMT

I thought I would update the list with the solution I ended up with. Not sure
that its brilliant but it does seem to work...

When the user selects a database environment to use, the servlet creates the
datasource as before but stores it in a ThreadLocal (actually, a wrapper to
a ThreadLocal for convenience).

My DAO is wired to a custom AbstractDataSource which, when asked for a
connection in getConnection, retrieves the datasource from the ThreadLocal
wrapper and returns it's connection. 

Took a long time to get to that but I appreciate the people here who helped
nudge my understanding. Just in case someone as clueless as I comes along
and wants some code, here are some snippets.

Thanks again

Paul
+++++++++
  <bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation"
value="classpath:/sqlMapConfig.xml"/>
        <property name="dataSource" ref="mediusProdDataSource"/> 
   </bean>

   <bean id="mediusProdDataSource"
class="persistence.dao.MediusCentralRoutingDataSource" />

and the class:

public class MediusCentralRoutingDataSource extends AbstractDataSource
{
	public Connection getConnection() throws SQLException
	{
		DataSource ds = MediusContextDataSourceHolder.getDataSource();
		return ds.getConnection();
	}

	public Connection getConnection(String username, String password)
			throws SQLException
	{
		DataSource ds = MediusContextDataSourceHolder.getDataSource();		
		return ds.getConnection(username, password);
	}
}



-- 
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#a10089746
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Mime
View raw message