cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrus Adamchik <>
Subject Re: Replacing DataSource in Cayenne 3.1 has no effect
Date Thu, 26 May 2011 14:00:04 GMT
It is still possible with 3.1. The approach is the same - find all classes that use DataSource
and replace it (the set of affected classes may be different). And you've discovered one way
of doing that already.

Another more DI'ish way is to implement MyDataSourceFactory to return a "dynamic" DataSource
that changes the returned connection under the covers based on a system property or a ThreadLocal
value. So when you switch to processing a new DB, you start by setting that value, and your
DataSource picks it up and returns connections pointing to the new DB. The advantage of that
is that your code doesn't need to know Cayenne internals to work, it is all encapsulated in
your own classes.


On May 26, 2011, at 9:50 AM, Wernke zur Borg wrote:

> Thanks, but I am not sure whether this is what I need.
> I have hundreds of MS Access Database files (*.mdb), which I need to process in a single
run of my program. Obviously I am using the JdbcOdbc bridge, which is working fine with Cayenne
so far. My intention is to create a single ServerRuntime and then switch the data source dynamically
for every file.
> This used to be possible with 3.0 according to the FAQ entry - is it no longer possible
with 3.1 ?
> Thanks, Wernke
> On 26/05/2011 15:25, Andrus Adamchik wrote:
>> Yeah, the Wiki page refers to Cayenne 3.0 and earlier. In 3.1 it is much easier and
can be done via a DI container. And you don't have to remember all the classes that might
use the DataSource. So you would create a custom module, bind a custom DataSourceFactory,
and pass it to ServerRuntime constructor:
>> Module m = new Module() {
>>  public void configure(Binder binder) {
>>    binder.bind(DataSourceFactory.class).to(MyDataSourceFactory.class);
>>  }
>> };
>> ServerRuntime r = new ServerRuntime("cayenne-myconfig.xml", m);
>> On May 26, 2011, at 6:19 AM, Wernke zur Borg wrote:
>>> Hi all,
>>> I am referring to FAQ "Setting Database Connection" here:
>>> Firstly, in 3.1 the SharedConfiguration no longer exists, so we have to use the
ServerRuntime instead:
>>>   ServerRuntime runtime = ...;
>>>   DataDomain domain = runtime.getDataDomain();
>>>   DataNode node = domain.getNode("DB1");
>>>   node.setDataSource(dataSource);
>>> However, this setting does not have the desired effect. When the connection to
the database is established, still the original definition from the config file is taken.
>>> I have traced down this problem and found that the DataNode has a DbAdapter,
which has its own DataSource object. This gets set in the ServerRuntime.getDataDomain() call
>>> The only way I found around this is by explicitly creating a new adapter with
my data source:
>>>   node.setAdapter(new AutoAdapter(dataSource));
>>> However, the AutoAdapter default constructor is deprecated, and I could not find
an easy way to use the recommended method with a DbAdapterFactory.
>>> Any ideas?
>>> Thanks for any hint.
>>> Wernke

View raw message