Re: Configuring multiple DataNodes

Hugi Thordarson <[email protected]>
Newsgroups gmane.comp.java.cayenne.user
Message-ID <[email protected]>
Since I use HikariCP (which I can recommend) I use a DataSourceFactory to configure my connections, something similar to the following pseudocode.
In this case you can read any available environment variables/properties to provide configuration.


public static ServerRuntime createServerRuntime() {
	return ServerRuntime
			.builder()
			.addConfig( "cayenne-project.xml" )
			.addModule( b -> b.bind( DataSourceFactory.class ).to( AppDataSourceFactory.class ) )
			.build();
}

private static class AppDataSourceFactory implements DataSourceFactory {

	@Override
	public DataSource getDataSource( DataNodeDescriptor dataNodeDescriptor ) throws Exception {
		final String dataNodeName = dataNodeDescriptor.getName();

		if( dataNodeName.equals( "somenode" ) ) {
			final HikariConfig config = new HikariConfig();
			config.setUsername( "someuser" );
			config.setPassword( "somepass" );
			config.setJdbcUrl( "someurl" );
			config.setMaximumPoolSize( 4 );
			return new HikariDataSource( config );
		}

		if( dataNodeName.equals( "othernode" ) ) {
			final HikariConfig config = new HikariConfig();
			config.setUsername( "otheruser" );
			config.setPassword( "otherpass" );
			config.setJdbcUrl( "otherurl" );
			config.setOtherStuff( ... );
			return new HikariDataSource( config );
		}

		throw new IllegalArgumentException( "Unknown dataNode: " + dataNodeName );
	}
}

Cheers,
- hugi


> On 23 Apr 2021, at 14:32, Tony Giaccone <[email protected]> wrote:
> 
> I have a datadomain with two datamaps, and two datasource each talking to a
> different database.
> 
> Right now, I have had the datasource configured in the config file, but I
> need to remove that and set it programmatically, from an environment
> variable.  Simple enough to do with a defauldata source, unclear how to do
> when you have multiple data sources and data nodes.
> 
> After browsing through the code and the app as it's running, it's unclear
> to me how to do this.
> 
> Does anyone know the secret sauce?
> 
> 
> 
> Tony
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.