Connecting to databases in different environments

Hello Everyone,
I have recently migrated from hsql to postgres for my cuba reports application. Here are the DB connection settings in my build.gradle file

task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
appName = ‘cuba-reports-core’
appJars(‘cuba-reports-global’, ‘cuba-reports-core’)
}

task createDb(dependsOn: assembleDbScripts, description: 'Creates local database', type: CubaDbCreation) {
    dbms = 'postgres'
    dbUrl = ''
    dbName = ''
    dbUser = ''
    dbPassword = ''
}

task updateDb(dependsOn: assembleDbScripts, description: 'Updates local database', type: CubaDbUpdate) {
    dbms = 'postgres'
    host = ''
    dbName = ''
    dbUser = ''
    dbPassword = ''
}

Migrating to postgres was successfully done.
Here the details are hard coded in builde.gradle file, but now I’m having a multiple environment where these details will differ. Right now I have to change in build.gradle and build it again to execute for environment specific. Instead I want these data to be dynamic so I don’t have to change the details and build for each environment. I thought of reading it from properties file but build.gradle is used during build time itself and properties file will be available on server for environment specific. So reading from properties file in build.gradle is not making sense to(I might be wrong please correct me here).
Can someone please help me or share some example to achieve this?

Also I have a context.xml file where I need to define the DataSource and the DB credentials. Those needs to be removed from the code and read dynamically as well

Hi,
Which CUBA version do you use? In CUBA 7.2 this is possible.

You can set cuba.dataSourceProvider = application in app.properties, and set all database properties in the same file, removing them from context.xml.
See Connecting to Databases - CUBA Platform. Developer’s Manual

If you store you database connection parameters in app.properties, then in other environments you can override them using OS environment variables or using local.app.properties file.
see Application Properties - CUBA Platform. Developer’s Manual

If you keep database connection settings in the context.xml file - then you can just change the contents of the context.xml file in the corresponding environment.

You can also parameterize Tomcat’s context.xml file by assigning Java system properties, see example here: java - Tomcat 8 - context.xml use Environment Variable in Datasource - Stack Overflow

I am using cuba 6.5.7

Well, then you can use the last link and parameterize Tomcat’s context.xml file by assigning Java system properties in setenv.sh script (or systemd service file).

1 Like

Thanks for the response, Alexander. I understood that the values for context.xml can be configured from java system properties. But in case of having those credentials in build.gradle file, Does the below approach work here?

https://doc.cuba-platform.com/manual-latest/db_connection.html#db_connection_app

The reason why I am asking this is because the documentation is referring to cuba 7.2.x

Values from build.gradle file refer only to development environment, they are not related to the runtime environment.

If you want to have build-time properties to depend on environment variables, it is totally possible in Gradle, which allows to call arbitrary Java APIs from the build script.

Instead of string literal, call System.getenv(‘VARIABLE_NAME’).
See examples here: In Gradle, is there a better way to get Environment Variables? - Stack Overflow
Gradle for DevOps, Part 2

1 Like

Thanks Alexander.
After migrating to postgres from hsql, When trying to deploy the app in the runtime environment, I am getting the below error

Caused by: java.lang.ClassNotFoundException: org.hsqldb.jdbc.JDBCDriver
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_131]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_131]
	at com.haulmont.cuba.core.sys.CubaSingleAppClassLoader.loadClass(CubaSingleAppClassLoader.java:52) ~[cuba-shared-lib-6.5.7.jar:6.5.7]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_131]
	at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2136) ~[tomcat-dbcp.jar:8.5.20]
	... 27 common frames omitted

When I grep in my tomcat folder for hsql entries, I found there is a file called cuba-reports.xml (./conf/Catalina/localhost/cuba-reports.xml) which has the below entries

How does this cuba-reports.xml file gets generated? and how does it populate the values? Is there a way to fix it?

Please create new topics for questions not directly related to the solved problem.

1 Like