You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by John Huss <jo...@gmail.com> on 2011/09/26 16:49:44 UTC

Switching data nodes at runtime

At runtime how do I switch my model to use a different data node that was
previously defined in the in model?  I'm using trunk.

Thanks,
John

Re: Switching data nodes at runtime

Posted by John Huss <jo...@gmail.com>.
Thanks for help.

To document this for future users, the DI approach then looks something like
this:

import org.apache.cayenne.configuration.DefaultRuntimeProperties;
import org.apache.cayenne.di.Binder;
import org.apache.cayenne.di.Module;

public class AppModule implements Module {
  public void configure(Binder binder) {
    binder.bindMap(DefaultRuntimeProperties.PROPERTIES_MAP)
      .put("cayenne.jdbc.driver.MyDomain.mynode", "org.postgresql.Driver")
      .put("cayenne.jdbc.url.MyDomain.mynode",
"jdbc:postgresql://localhost/my_db")
      .put("cayenne.jdbc.username.MyDomain.mynode", "postgres")
      .put("cayenne.jdbc.password.MyDomain.mynode", "mypassword");
    }
  }
}

On Mon, Sep 26, 2011 at 12:05 PM, Andrus Adamchik <an...@objectstyle.org>wrote:

> This is a good recipe for 3.0. In 3.1 you are allowed to merge multiple
> projects when creating a ServerRuntime, so you can merge multiple mappings
> to get the behavior that you need. However... Are you sure you want to
> switch DataNodes? DataNodes by themselves don't have many configuration
> properties. All the DB connection info is really about the underlying
> javax.sql.DataSource and that can be tweaked differently. Earlier you wrote:
>
> > Yeah, I'm mainly interested in supporting the same schema on multiple
> databases
>
> So this means 1 database in a given app, just a different database between
> the apps (or app executions)? In this case you can either use JNDI to map
> the DataSource, without changing anything in Cayenne (there is a nifty thing
> called AutoAdapter that automatically installs the right DbAdapter depending
> on the target DB type, so no need to know the adapter upfront). Or check
> this PDF (Chapter 11 and Appendix A) for properties that allow to specify
> the DB connection in runtime:
>
> http://people.apache.org/~aadamchik/misc/cayenne-guide-08302011.pdf
>
> AutoAdapter is at play here is well.
>
> Andrus
>
>
>
> On Sep 26, 2011, at 6:35 PM, Michael Gentry wrote:
> > Hi John,
> >
> > This is completely untested, but try something like:
> >
> > Configuration  configuration = Configuration.getSharedConfiguration();
> > DataDomain  dataDomain = configuration.getDomain();
> > DataMap  dataMap = dataDomain.getMap("my map name");
> > DataNode  oldNode = dataDomain.getNode("old node name);
> > DataNode  newNode = dataDomain.getNode("new node name);
> >
> > oldNode.removeDataMap(dataMap);
> > newNode.addDataMap(dataMap);
> > dataDomain.reindexNodes(); // maybe
> >
> > mrg
> >
> >
> > On Mon, Sep 26, 2011 at 10:49 AM, John Huss <jo...@gmail.com> wrote:
> >> At runtime how do I switch my model to use a different data node that
> was
> >> previously defined in the in model?  I'm using trunk.
> >>
> >> Thanks,
> >> John
> >>
> >
>
>

Re: Switching data nodes at runtime

Posted by Andrus Adamchik <an...@objectstyle.org>.
This is a good recipe for 3.0. In 3.1 you are allowed to merge multiple projects when creating a ServerRuntime, so you can merge multiple mappings to get the behavior that you need. However... Are you sure you want to switch DataNodes? DataNodes by themselves don't have many configuration properties. All the DB connection info is really about the underlying javax.sql.DataSource and that can be tweaked differently. Earlier you wrote:

> Yeah, I'm mainly interested in supporting the same schema on multiple databases

So this means 1 database in a given app, just a different database between the apps (or app executions)? In this case you can either use JNDI to map the DataSource, without changing anything in Cayenne (there is a nifty thing called AutoAdapter that automatically installs the right DbAdapter depending on the target DB type, so no need to know the adapter upfront). Or check this PDF (Chapter 11 and Appendix A) for properties that allow to specify the DB connection in runtime:

http://people.apache.org/~aadamchik/misc/cayenne-guide-08302011.pdf

AutoAdapter is at play here is well.

Andrus



On Sep 26, 2011, at 6:35 PM, Michael Gentry wrote:
> Hi John,
> 
> This is completely untested, but try something like:
> 
> Configuration  configuration = Configuration.getSharedConfiguration();
> DataDomain  dataDomain = configuration.getDomain();
> DataMap  dataMap = dataDomain.getMap("my map name");
> DataNode  oldNode = dataDomain.getNode("old node name);
> DataNode  newNode = dataDomain.getNode("new node name);
> 
> oldNode.removeDataMap(dataMap);
> newNode.addDataMap(dataMap);
> dataDomain.reindexNodes(); // maybe
> 
> mrg
> 
> 
> On Mon, Sep 26, 2011 at 10:49 AM, John Huss <jo...@gmail.com> wrote:
>> At runtime how do I switch my model to use a different data node that was
>> previously defined in the in model?  I'm using trunk.
>> 
>> Thanks,
>> John
>> 
> 


Re: Switching data nodes at runtime

Posted by Michael Gentry <mg...@masslight.net>.
Hi John,

This is completely untested, but try something like:

Configuration  configuration = Configuration.getSharedConfiguration();
DataDomain  dataDomain = configuration.getDomain();
DataMap  dataMap = dataDomain.getMap("my map name");
DataNode  oldNode = dataDomain.getNode("old node name);
DataNode  newNode = dataDomain.getNode("new node name);

oldNode.removeDataMap(dataMap);
newNode.addDataMap(dataMap);
dataDomain.reindexNodes(); // maybe

mrg


On Mon, Sep 26, 2011 at 10:49 AM, John Huss <jo...@gmail.com> wrote:
> At runtime how do I switch my model to use a different data node that was
> previously defined in the in model?  I'm using trunk.
>
> Thanks,
> John
>