0

I have 2 different databases being used by one application. In the xml file, I can declare 2 nodes with different URLs and credentials without a problem.

Is there a way to do this in the code? Without creating 2 runtimes.

Trying...
  • 127
  • 9

1 Answers1

1

It is possible to do via properties wrapped in a module:

ServerRuntime.builder()
  .addModule(b -> ServerModule.contributeProperties(b)
    .put("cayenne.jdbc.url.project.node1", "jdbc:url1")
    .put("cayenne.jdbc.url.project.node2", "jdbc:url2")
    // similarly add properties for driver, user, password, etc.
  )
  .build();

Alternatively for each DataNode in the Modeler you can set a custom "DataSource Factory", pointing it to your own Java class similar to this:

public class MyDataSourceFactory implements DataSourceFactory {
    
    @Override
    public DataSource getDataSource(DataNodeDescriptor nd) throws Exception {
        DataSourceInfo info = nd.getDataSourceDescriptor();

        // use the configuration in the DataSourceInfo and/or supplement it 
        // your own values
        ...

        // Create a DataSource
        Driver driver = (Driver)objectFactory.getJavaClass(driverClass).newInstance();
        return DataSourceBuilder
            .url(url)
            .driver(driver)
            .userName(username)
            .password(password)
            .pool(minConnections, maxConnections)
            .maxQueueWaitTime(maxQueueWaitTime)
            .validationQuery(validationQuery)
            .build();
    }
}
andrus_a
  • 2,528
  • 1
  • 16
  • 10
  • Can I have an xml file for everything else but url, username and password? So I would add the configuration file using .addConfig and then add the module to it? Or does it need to be all in the code? Also in "cayenne.jdbc.url.project.node1", project also needs to be replaced with "domain_name", correct? – Trying... Dec 01 '22 at 14:27
  • Yes, the "project" part is the domain name. And yes, you can only override parts of the DataNode definition. Let me update the answer with an example. – andrus_a Dec 01 '22 at 20:01
  • I have an xml file I'm trying to override and I can't figure out where to set the domain name. Any help? – Trying... Aug 08 '23 at 14:43
  • domain name is what comes after the dash in the filename. So if the filename is cayenne-myserver.xml, domain name is myserver – Trying... Aug 21 '23 at 18:38
  • Yes, you can control the domain name by renaming your project XML file. Or are you looking to override it in runtime, regardless of what file was loaded? – andrus_a Aug 30 '23 at 16:01
  • I was looking to add more configuration to the existing items in the XML file and figured it out. Thank you. – Trying... Aug 31 '23 at 17:07