Setting up datasources via environment variables

I have a docker container running FarCry w/ Lucee. I’ve tried passing in environment variables for the DSN via the docker-compose.yml file without success. So my workaround, for now, has been to add the datasource to the lucee-web.xml.cfm file. What is the downside of going this route? The the db password is encrypted and we could limit the permissions of the db user.

1 Like

Use the <cfadmin> tag:

<cfadmin
	action="updateDatasource"
	type="server"
	password="lucee_admin_password"
	classname="org.gjt.mm.mysql.Driver"
	dsn="jdbc:mysql://{host}:{port}/{database}"
	newname="dbname"
	name="dbname"
	host="localhost"
	database="dbname"
	port="3306"
	dbusername="username"
	dbpassword="password"
	connectionLimit="500"
	connectionTimeout="60"
	blob="false"
	clob="true"
	allowed_select="true"
	allowed_insert="true"
	allowed_update="true"
	allowed_delete="true"
	allowed_alter="true"
	allowed_drop="true"
	allowed_revoke="true"
	allowed_create="true"
	allowed_grant="true">

where type is either server or web depending on whether you want to create a server level DSN or just for the specific web context. You’ll also need to set the lucee admin password, database username/password, driver & dsn URL, port, etc.

If you can paste the environment variables you’re using I might be able to spot the problem (just remove the password and change the hostname to some kind of placeholder so that you aren’t giving us sensitive info).

You can also set up the data source in the Lucee admin and then view the code snippet to see the exact settings you need.

Also remember that you need to set this.bUseEnv = true in the farcryConstructor.cfm to enable configuration via environment variables.

The downside of setting the data source in the Lucee web XML is that you now might need different files for Dev vs Production. Using environment variables gives you more flexibility.

I thought I’d just follow this up with some example environment variables that should work with different DB servers;

MySQL

    - "FARCRY_DSN=_your_datasource_name_"
    - "FARCRY_DBTYPE=mysql"
    - "FARCRY_DSN_CLASS=org.gjt.mm.mysql.Driver"
    - "FARCRY_DSN_CONNECTIONSTRING=jdbc:mysql://_your_mysql_hostname_or_IP_:3306/_your_database_name_?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useLegacyDatetimeCode=true"
    - "FARCRY_DSN_USERNAME=_your_db_username_"
    - "FARCRY_DSN_PASSWORD=_your_db_password_"

SQL Server

    - "FARCRY_DSN=_your_datasource_name_"
    - "FARCRY_DBTYPE=mssql2005"
    - "FARCRY_DSN_CLASS=net.sourceforge.jtds.jdbc.Driver"
    - "FARCRY_DSN_CONNECTIONSTRING=jdbc:jtds:sqlserver://_your_sql_hostname_or_IP_:1433/_your_database_name_"
    - "FARCRY_DSN_USERNAME=_your_db_username_"
    - "FARCRY_DSN_PASSWORD=_your_db_password_"

H2 (embedded)

    - "FARCRY_DSN=_your_datasource_name_"
    - "FARCRY_DSN_CLASS=org.h2.Driver"
    - "FARCRY_DSN_CONNECTIONSTRING=jdbc:h2:db/_your_database_name_;MODE=MySQL"

If you want to use the encrypted:... password format (recommended) you can set up the datasource manually in the Lucee admin, then copy it from the code that is generated for the datasource.

And again, these environment variables only get applied when you’ve enabled this.bUseEnv = true in your farcryConstructor.cfm.

1 Like

Thanks for the examples! I’m still not having any luck. I verified that THIS.bUseEnv is set to true, though I admit I had to correct it from THIS.bUseENV. But after making the correction, my luck didn’t change. Here’s the environment variables I was setting:

- "FARCRY_DSN=fandango"
- "FARCRY_DSN_DBTYPE=mssql2005"
- "FARCRY_DSN_CLASS=com.microsoft.jdbc.sqlserver.SQLServerDriver"
- "FARCRY_DSN_CONNECTIONSTRING=jdbc:jtds:sqlserver://_IP_ADDRESS:1433/fandango"
- "FARCRY_DSN_USERNAME=myuser"
- "FARCRY_DSN_PASSWORD=encrypted:string copied from lucee-web.xml.cfm file in another container"

For the FARCRY_DSN_CLASS, I also tried “net.sourceforge.jtds.jdbcDriver”

When I set the above variables to point to an empty database I get a “java.lang.NullPointerException” message when trying to run an install:

The Error Occurred in
/var/www/farcry/core/packages/lib/fapi.cfc: line 57

    55: <cfset var message = '' />
    56:
    57: <cfif structKeyExists(application.stCoapi, arguments.typename) and (arguments.singleton or isdefined("request.inthread"))>
    58: <cfset oResult = application.stcoapi["#arguments.typename#"].oFactory />
    59: <cfelseif structKeyExists(application.stCoapi, arguments.typename)>  

When I set the variables to go to a database that already has the FC tables, when I try to go to the homepage, I get the error message “key [STCOAPI] doesn’t exist”:

The Error Occurred in
/var/www/farcry/core/packages/lib/fapi.cfc: line 57

    55: <cfset var message = '' />
    56:
    57: <cfif structKeyExists(application.stCoapi, arguments.typename) and (arguments.singleton or isdefined("request.inthread"))>
    58: <cfset oResult = application.stcoapi["#arguments.typename#"].oFactory />
    59: <cfelseif structKeyExists(application.stCoapi, arguments.typename)>

For now, I’ll continue to use lucee-web.xml.cfm and use volumes in the docker-compose.yml file to point to dev and prod versions of the file.

We use this feature day in day out and have done so for some considerable time. Has this issue resolved for you? And if not, what version of FarCry are you using?