cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrus Adamchik <and...@objectstyle.org>
Subject Re: Dynamic datasource switch
Date Fri, 26 Apr 2013 13:00:16 GMT
Hi,

if that all happens in the same application, then it is not thread-safe. Cayenne stack below
DataContext is usually shared. Only DataContext is dedicated to a user (if you make it so).
A safer solution is to have 3 stacks. In Cayenne 3.0 this can be achieved with multiple DataDomains
per Configuration (although it is a pain to maintain). In Cayenne 3.1 you'd simply create
3 ServerRuntime's. 

Then the trick is to use the right backend when creating a new DataContext. 

You are using the opposite method - switching stack underneath the same DataContext. While
it might work in some scenarios, it generally looks counterintuitive and error-prone.

Cheers,
Andrus


On Apr 26, 2013, at 8:21 AM, Sébastien Pérès-Labourdette <sebastien.peres-labourdette@omerin.com>
wrote:
> Hi.
> 
> I have 3 databases which shares exactly the same schema.
> I sometimes need to switch from one to another programatically.
> 
> For that purpose, i wrote the folowing code :
> 
>    public void setCompany(int salesAgencyId) {
>        this.salesAgencyId = salesAgencyId;
>        switch (salesAgencyId) {
>            case COMPANY_C1:
>                setC1DataContext();
>                break;
>            case COMPANY_C2:
>                setC2DataContext();
>                break;
>            case COMPANY_C3:
>                setC3DataContext();
>                break;
> 
>            default:
>                setC1DataContext();
>                break;
>        }
>    }
> 
>    private static void setC1DataContext() {
>        if (COMPANY_CURRENT != COMPANY_C1) {
>            LOG.debug("switching context from " + COMPANY_CURRENT + " to
> " + COMPANY_C1);
>            COMPANY_CURRENT = COMPANY_C1;
>            getDataContext().getQueryCache().clear();
>            getDataContext().getObjectStore().getDataRowCache().clear();
> 
> 
> Configuration.getSharedConfiguration().getDomain().getNode("c").setDataSource(c1DataSource);
>        }
>    }
> 
> It just works perfectly most of the time.
> But sometimes it fails and keep the connection on the previous datasource.
> 
> Any of you may give me any hint on that issue ?
> 
> Thanks in advance.
> SPL
> 


Mime
View raw message