cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sébastien Pérès-Labourdette <sebastien.peres-labourde...@omerin.com>
Subject Dynamic datasource switch
Date Fri, 26 Apr 2013 12:21:39 GMT
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