Finally, I found a solution. I created a class MyTransactionConfig extends BaseTransactionConfig.
Then I plugged it into iBatis by setting the transactionManager 'type' attribute equal to my new class name. In MyTransactionConfig's newTransaction method, I lookup the company id from thread local and using that I find the appropriate datasource from my service locator. // MyTransactionConfig: public Transaction newTransaction(int transactionIsolation) throws SQLException, TransactionException { // get company id from thread local // get datasource from service locator based on company id DataSource newDataSource = <get from service locator> return new ExternalTransaction(newDataSource, defaultAutoCommit, setAutoCommitAllowed, transactionIsolation); } Everyone's help is really appreciated. -----Original Message----- From: Brandon Goodin [mailto:[EMAIL PROTECTED] Sent: Thursday, April 21, 2005 6:45 PM To: ibatis-user-java@incubator.apache.org Subject: Re: Dynamically Discovering DataSource Write a custome DataSource. On 4/21/05, Rafiq, Adnan <[EMAIL PROTECTED]> wrote: > I was able to implement the DataSourceFactory implement and plug it into > ibatis. > > Unfortunately, since I am using the DAO framework, it seems that the > DataSourceFactory.getDataSource() method gets called upon initialization > only. From that point onward, it uses the one it has cached. Is there a way > to always go to the factory when getting a new connection? > > -----Original Message----- > From: Brandon Goodin [mailto:[EMAIL PROTECTED] > Sent: Thursday, April 21, 2005 12:48 PM > To: ibatis-user-java@incubator.apache.org > Subject: Re: Dynamically Discovering DataSource > > You would simply use the Fully Qualified class name and make sure it > implements DataSourceFactory. > > <transactionManager ...> > <dataSource type="com.foo.MyCustomerFactory"> > <property name="propertyA" value="valueA"/> > <property name="propertyB" value="valueB"/> > </dataSource> > </transactionManager> > > Where you set the company is up to your architecture. Where do you get > the id from in the first place? > > Brandon > > On 4/21/05, Rafiq, Adnan <[EMAIL PROTECTED]> wrote: > > > > Larry, > > > > Using ThreadLocal just might solve my problem. This is what I am thinking: > > > > > 1 - Extend the JndiDataSourceFactory class and override the getDataSource > > method > > 2 - Obtain the company id from ThreadLocal (assuming that its been set in > > the DaoImpl class) > > 3 - In the getDataSource method of my new factory class, lookup the > > datasource name based on the company id > > 4 - Return the looked-up datasource name (instead of the one defined in > sql > > maps xml file) > > > > However, I need to figure out the following: > > 1 - If extend the JndiDataSourceFactory class, how do I plug it into > iBatis? > > 2 - What will be the best place to set the company id in ThreadLocal > > a) In the DaoImpl class constructor, or > > b) In each DaoImpl class method > > > > Your help is really appreciated in this matter. > > > > Thanks, > > > > - Adnan > > > > -----Original Message----- > > From: Larry Meadors [mailto:[EMAIL PROTECTED] > > Sent: Thursday, April 21, 2005 11:15 AM > > To: ibatis-user-java@incubator.apache.org > > Subject: Re: Dynamically Discovering DataSource > > > > This is such an unusual case that IMO, the DataSourceFactory should not be > > changed to accommodate it. > > > > You could probably use a ThreadLocal class to set the parameter, then in > > your datasource, examine it to decide the real datasource to use. > > > > Larry > > > > > > On 4/21/05, Rafiq, Adnan <[EMAIL PROTECTED]> wrote: > > > > > > Larry, > > > > > > Thanks for the suggestion. > > > > > > I looked at the DataSourceFactory. The method getDataSource() does not > > take any parameters. The flow of our application is like this: > > > > > > 1 - Contact the system datasource and get a list of all companies and > > their associated datasource jndi names > > > 2 - When the user logs in, we get its company id and using it as a key > we > > find the corresponding datasource jndi name > > > 3 - The company datasource jndi name is then returned and used for all > > connections. > > > > > > What seems like a good solution is to extend the JNDIDataSourceFactory > and > > overload the method getDataSource so that it can accept a company id as an > > argument: getDataSource(int companyId). Internally, we have a map of > > companyid (key) and datasource name (value). > > > > > > However, what I can't figure out is where that method is actually called > > in iBatis. > > > > > > Thanks, > > > > > > - Adnan > > > > > > > > > -----Original Message----- > > > From: Larry Meadors [mailto:[EMAIL PROTECTED] > > > Sent: Thursday, April 21, 2005 10:26 AM > > > To: ibatis-user-java@incubator.apache.org > > > Subject: Re: Dynamically Discovering DataSource > > > > > > Roll your own javax.sql.DataSource (6 methods) and > > com.ibatis.sqlmap.engine.datasource.DataSourceFactory (2 > > methods). > > > > > > Larry > > > > > > > > > On 4/21/05, Rafiq, Adnan <[EMAIL PROTECTED]> wrote: > > > > > > > > I have a situation where we have an ASP-like environment. There is one > > system database and several customer-specific databases. Each customer > > database is mapped to a different datasource. In iBatis we have to > hardcode > > the JNDI name for the datasource in the sql-map-confi.xml file. Is there > > anyway to make this dynamic? In other words, I want to query the customer > > datasource name (the names are stored in the system database) at run time > > based on the user id, and then use that name for getting my connections. > > > > > > > > Any help is greatly appreciated. > > > > > > > > > > >