Datasource being ignored in server.json

Running Coldbox with lucee 5.3.7+47 CF Engine. It looks like the server.json datasource settings are being ignored during server startup. I have the following datasource in my server.json:

"datasources":{
    "mydatasource":{
        "allowAlter":true,
        "allowCreate":true,
        "allowDelete":true,
        "allowDrop":true,
        "allowGrant":true,
        "allowInsert":true,
        "allowRevoke":true,
        "allowSelect":true,
        "allowUpdate":true,
        "class":"com.mysql.jdbc.Driver",
        "connectionLimit":"100",
        "connectionTimeout":"1",
        "custom":"useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true&autoReconnect=true",
        "database":"mydatabase",
        "dbdriver":"MySQL",
        "dsn":"jdbc:mysql://{host}:{port}/{database}",
        "host":"localhost",
        "password":"*******",
        "port":"3306",
        "username":"myusername"

However, when I try to browse to the app that uses the datasource set above, I’m getting the following error:

**Type:** database
**Messages:** datasource [mydatasource] doesn't exist

So it looks like the datasource gets completely ignored in the server.json file.

Datasources don’t go in server.json. You are probably thinking about CFConfig.

https://cfconfig.ortusbooks.com/

https://cfconfig.ortusbooks.com/using-the-cli/command-overview/manage-datasources

1 Like

If I was not interested in using cfconfig. Which file would they go?

You have no choice. Cfconfig is the library to make ColdFusion settings modular. Your alternative is you doing it manually via confit xml files.

So adding datasources in Application.cfc is not supported either?

There are several ways to add data sources to the running engine Dino.

  • Vis a cfconfig file. Modular and encapsulated.
  • manually via cfadmin
  • adding them to config files inside the engines
  • application.cfc

Your choice.

1 Like

I’m trying to use the cfconfig command but it doesn’t seem to be working. I try this:

cfconfig datasource save name=mydatasource dbdriver=MySQL host=localhost port=3306 database=dbase username=usrname password=****

It completes succesfully but still getting the following error even after I restart the server:

**Type:** database
**Messages:** datasource [mydatasource] doesn't exist

So I try the following:

cfconfig datasource save name=mydatasource dbdriver=mysql host=localhost port=3306 database=dbase username=username password=**** to=Server-Name

Completes succesfully but after a server reload still the following error:

**Type:** database
**Messages:** datasource [mydatasource] doesn't exist

So I try the following:

cfconfig datasource save name=mydatasource dbdriver=mysql host=localhost port=3306 database=dbase username=usrname password=**** to=/opt/Coldbox-App/

It throws the following error:

ERROR (5.2.0+00280)
You gave the location of the [to] server, but we couldn't figure out the format to use.

Please help us by adding [toFormat=...] to your command.

So now, I need to provide the toFormat= parameter but what do I put there I wonder? You see that’s nowhere to be found in the docs at:

https://cfconfig.ortusbooks.com/using-the-cli/command-overview/manage-datasources

Tab doesn’t work either.

So I take a stab with this:

cfconfig datasource save name=mydatasource dbdriver=mysql host=localhost port=3306 database=dbase username=usrname password=**** to=/opt/Coldbox-App/ toFormat=lucee@5.x

So now I’m getting this error:

Sorry, no config provider could be found for [lucee@5.x]. Please be more specific or check your spelling.

The cfconfig docs need some expansion in my humble opinion. Maybe I’m wrong.

If I try to Application.cfc route, it doesn’t work at all. The datasource gets ignored completely with the following in the Application.cfc file:

<cfcomponent displayname="MyApp" output="false" hint="My App">

<!---
    //Define hermes Datasource
    <cfscript>
      this.datasources["mydatasource"] = {
      class: 'com.mysql.jdbc.Driver'
      , bundleName: 'com.mysql.jdbc'
      , bundleVersion: '5.1.40'
      , connectionString: 'jdbc:mysql://localhost:3306/dbase?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true'
      , username: 'username'
      , password: "***********"
      // optional settings
      , blob:true // default: false
      , clob:true // default: false
      , connectionLimit:100 // default:-1
      };
  </cfscript>

<cfcomponent>

The only thing that works is cfadmin manually or editing the WEB-INF/lucee-server/context/lucee-server.xml file.

It would be good at this point to lay the groundwork.

Are you using CommandBox as your server? It you are not, please read about transferring your config to your server in the docs.

CommandBox and Coldbox doesn’t so anything to mess with this.datasources in your Application.cfc. To help you debug that we would need more information, like a stack trace of the error you are seeing. One thing I do notice is while you are setting it as a datasource above, there is no default datasource set. That means you’d need to at least pass the datasource around to your cfquery or queryExecute calls.

1 Like

cfconfig datasource save

So hold on right here… Why are you using this command at all? You already have the JSON definition of your datasource right? Just put it in a .cfconfig.json file in the web root and you’re done! Why are you even messing with the one-off command to create a datasource in the first place?

https://cfconfig.ortusbooks.com/using-the-cli/commandbox-server-interceptors/server-start#import-from-json-file

It completes succesfully but still getting the following error …

What directory did you run the command in? Was it in the web root of the server. This is a CommandBox server, right? Do you see the datasource when you run

cfconfig datasource list

or

cfconfig show

Do you see the datasource when you log into the web admin for the server? Also, do you have an empty, or mostly empty .cfconfig.json file in the web root? if so, that will be imported and overwrite all your settings. If you start your server with the --verbose flag like so

server start --verbose

then look for a green line of log output that says “config transferred” to confirm if CFConfig is performing an import from a JSON file.

to=/opt/Coldbox-App/

This doesn’t seem like a server home. The rules for the server home are defined in the docs here along with examples

https://cfconfig.ortusbooks.com/using-the-cli/usage#specifying-a-server-home

But to be clear, you’re already very far down the wrong path at this point. You shouldn’t need a to set, nor even the server name. Just run the command in the web root and that’s it. There’s like a 95% chance you’re blowing away your settings on every restart with another .cfconfig file. Note, our ColdBox app templates come with a .cfconfig.json file in the webroot that defines a datasource.

Some basic troubleshooting to see what datasources are defined after you restart the server should go a long way in figuring out what is happening.

You gave the location of the [to] server, but we couldn’t figure out the format to use.

This is because you didn’t provide a correct to location per the docs! You’re way down the wrong road here. You don’t need to be messing with a toFormat because you don’t really need a to in the first place. The CLI really is just guessing at what you want at this point.

So now, I need to provide the toFormat= parameter but what do I put there I wonder?

Again, fully documented here:

https://cfconfig.ortusbooks.com/using-the-cli/usage#server-format-engine-version

You see that’s nowhere to be found in the docs at:

Yes, of course. i’m not going to copy and paste the definition of what to/from and toFormat/fromFormat mean in every single page of the docs! It’s covered once early on in one of the first pages under “usage”. There is also a search feature in the docs you can use to help find it. The command help for a few of the most common commands also has the same information:

 cfconfig import ?

toFormat=lucee@5.x

This is not valid per the docs. The format does not take a semantic version range, but the exact version that you want the config written for. But again, this is all barking up the wrong tree. Your very first command was fine so long as you ran it in he web root.

The cfconfig docs need some expansion in my humble opinion.

Feel free to submit some pull requests-- all the docs are stored in Github as markdown and there is an “edit on Gihub” button on every page. But so far, everything you’ve asked about is already documented. I just don’t repeat the basics over again on every page.

If I try to Application.cfc route, it doesn’t work at all.

This is a core feature of the CF engine, Lucee. I don’t see anything wrong right off with the Application.cfc file you have there, but perhaps check to ensure you’re editing the right file and the code that’s erroring isn’t in another application.

The only thing that works

I assure you, CFConfig and Application.cfc datasources work. We just need to understand all the moving pieces you have in place. And in this case, I’m pretty certain you need to edit or remove a stock .cfconfig.json file that’s overwriting your datasource every time you auto-import it on server start!

To get up and running, all you need to do is likely add the datasource once via the admin and run

cfconfig export .cfconfig.json

and then you’ll probably be good to go.

Because it was suggested by @elpete and @lmajano

I ran it in the web root of the server. Yes it’s a CommandBox server. When I run cfconfig datasource list I see the datasource:

 cfconfig datasource list
Name: mydatasource
  DB Driver: MySQL
  Host: localhost
  Port: 3306
  Database: username
  Username: dbase
  Password: *******

Yes, there is a .cfconfig.json file in the web root

I don’t see the “config transferred” line as you suggest. Here’s the full output:

server start --verbose
√ | Starting Server
|------------------------------
| Looking for server JSON file by convention: /opt/Colbox-App//server.json
| webroot defaulted to location of server’s JSON file: /opt/Colbox-App/
| Switching to the last-used server JSON file for this server: /opt/Colbox-App/server.json
| Recalculating web root based on new server JSON file.
| webroot defaulted to location of server’s JSON file: /opt/Colbox-App/
| start server in - /opt/Colbox-App/
| server name - Colbox-App
| server config file - /opt/Colbox-App/server.json
| Server start command:
| /bin/bash /root/.CommandBox/cfml/system/modules_app/server-commands/bin/server_spawner.sh /opt/Coldbo
| x-App//nohup.log /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
| -jar /root/.CommandBox/lib/runwar-4.3.13.jar
| --background=true
| --host 0.0.0.0
| --stop-port 40489
| --processname Colbox-App
| --log-dir /root/.CommandBox/server/DF496FC123B4D39BCE473BD328EB4F66-Colbox-App/logs
| --server-name Colbox-App
| --tray-enable true
| --dock-enable true
| --directoryindex false
| --timeout 240
| --proxy-peeraddress true
| --cookie-secure true
| --cookie-httponly true
| --tray-icon /root/.CommandBox/cfml/system/config/server-icons/trayicon-lucee.png
| --tray-config /root/.CommandBox/server/DF496FC123B4D39BCE473BD328EB4F66-Colbox-App/trayOptions.json
| --servlet-rest-mappings /rest/,/api/
| --gzip-enable true
| --cfengine-name lucee
| -war /opt/Colbox-App/
| --web-xml-path /opt/Colbox-App//WEB-INF/web.xml
| --http-enable true
| --ssl-enable false
| --ajp-enable false
| --open-browser true
| --open-url http://0.0.0.0:8282
| --port 8282
| --urlrewrite-enable true
| --urlrewrite-file /root/.CommandBox/cfml/system/config/urlrewrite.xml
| --predicate-file /root/.CommandBox/server/DF496FC123B4D39BCE473BD328EB4F66-Colbox-App/.predicateFile.txt

The server for /opt/Colbox-App/ is starting on http://0.0.0.0:8282

[INFO ] Runwar: Starting RunWAR 4.3.13
[INFO ] Java version 1.8.0.275 (requires >= 1.8)
[INFO ] Runwar: HTTP2 Enabled:false
[INFO ] Runwar: HTTP sslEnable:false
[INFO ] Runwar: HTTP ajpEnable:false
[INFO ] Runwar: HTTP warFile exists:true
[INFO ] Runwar: HTTP warFile isDirectory:true
[INFO ] Runwar: Starting background Colbox-App from: /root/.CommandBox/lib/runwar-4.3.13.jar
[INFO ] Starting in background -

Not sure what you mean this doesn’t seem like a server home. Can you clarify?

To be fair, I didn’t go down the “wrong path” on my own, just trying to utilize cfconfig commands as described here:

https://cfconfig.ortusbooks.com/using-the-cli/command-overview/manage-datasources

As I previously mentioned running the below command in the web root, completed succesfully but I still got the error that the datasource doesn’t exist:

After that is when I started going down the “wrong path” but only because of the docs.

I’m not sure what you mean here. What IS the correct to location if it’s not the /path/to/server/home.

Well, shouldn’t it be covered in this particular instance since it’s relevant to the cfconfig command referenced? Idk maybe I’m wrong. Not trying to criticize, certain pieces may need repeating if they are relevant to a specific command described.

So I tried that, it exported the config, I am clearly able to see the datasource in .cfconfig.json, but still getting the error that the datasource doesn’t exist. Here’s the relevant part out of the '.cfconfig.json` in my web root:

 "datasources":{
        "mydatasource":{
            "allowAlter":true,
            "allowCreate":true,
            "allowDelete":true,
            "allowDrop":true,
            "allowGrant":true,
            "allowInsert":true,
            "allowRevoke":true,
            "allowSelect":true,
            "allowUpdate":true,
            "class":"com.mysql.jdbc.Driver",
            "custom":"useUnicode=true&characterEncoding=UTF8&useLegacyDatetimeCode=true",
            "database":"dbase",
            "dbdriver":"MySQL",
            "dsn":"jdbc:mysql://{host}:{port}/{database}",
            "host":"localhost",
            "password":"*******",
            "port":"3306",
            "username":"username"
        }
    },

I agree, but even though the command was successful and when I listed the datasources with cfconfig datasource list the datasource appeared, it wasn’t in cfadmin and I was still getting the error that the datasource didn’t exist. So I tried the other cfconfig datasource commands to see if I can get it to work.

Because it was suggested…

Yes, I see Eric did include a link to the docs for that command, but the JSON file is by far the most common way of doing this, and I think that was the expectation since you were already trying to use a JSON file-- just the wrong one.

When I run cfconfig datasource list I see the datasource:

Before or after you restarted the server? I’m still not clear from your reply if the datasource is still there after you’ve restarted, which will import any existing .cfconfig.json file in your web root.

Yes, there is a .cfconfig.json file in the web root

And does it define any datasoruces, even if it’s an empty struct? This is important since it determines whether or not the server start will replace any existing datasources with what’s in the JSON file.

I don’t see the “config transferred” line as you suggest.

That’s interesting and a bit of a head scratcher. Unless you have an older version of CFConfig installed, it should output that. Previously, the --debug flag was the only way to trigger that text. So long as there is a file called exactly .cfconfig.json in your web root AND you haven’t overridden the cfconfigfile path in your server.json to point elsewhere, AND you have the CFConfig module installed, it should be importing the settings of that file, no question.

Not sure what you mean this doesn’t seem like a server home. Can you clarify?

The “server home” as defined by CFConfig is covered in the docs. Did you read it?
https://cfconfig.ortusbooks.com/using-the-cli/usage#specifying-a-server-home

Lucee 4/5 Web Context
The folder containing the lucee-web.xml.cfm file.

Lucee 4/5 Server Context
Path to the lucee-server folder containing the /context/lucee-server.xml file.

So the server home is where the actual ColdFusion or Lucee server itself is installed to, not the web root where your cfm files live. But again, you really don’t need to use this since any CFConfig command run in the web root of a CommandBox server will automatically apply to that server without the need to specify a to or a from server home.

As I previously mentioned running the below command in the web root, completed succesfully but I still got the error that the datasource doesn’t exist:

I get that. What I’m trying to tell you is your time would have been better spent if you stopped to find out why the datasource wasn’t there rather than trying 100 more commands. Just… log in to the admin and look if it’s there. This take about 20 seconds and will answer the question instantly of whether the saving of the datasource worked!

I’m not sure what you mean here. What IS the correct to location if it’s not the /path/to/server/home.

Again, actually read the docs I linked to above. The server home is the actual installation directory of the server. The docs above have examples and everything.

Well, shouldn’t it be covered in this particular instance since it’s relevant to the cfconfig command referenced?

No. Literally every single CFConfig command uses a to or a from parameter. It’s global functionality that applies to every single part of the CLI. There are 36 separate commands in the CFConfig module and I’m not going to paste the details of what to and from means on every single page of the docs. Which is why it’s covered once, at the top of the docs, under the usage section. And in your case, you don’t even need to be using a to or a from parameter. In fact, most people who use CFGonfig don’t need to use it based on their setup.

still getting the error that the datasource doesn’t exist.

I would recommend backing up a little bit?

  • Are there other servers running that you don’t realize (user server list --running )
  • Do you have another installation of ACF or Lucee on the same machine but on a different port?
  • Are you hitting the correct server in the URL when you test?
  • At the point you get the error that the datasource doesn’t exist, what do you see when you log into the server context for Lucee and look at the list of datasources? Is the datasource listed there??

it wasn’t in cfadmin

Before or after you restarted? Web or server context? There’s so many important details missing here.

I ended up deleting the application, re-creating, edited the .cfconfig.json with the datasource and it finally showed up in cfadmin. I don’t know what happenned there.

Glad you got it working. And along the way you learned a lot about CFConfig :wink:

That’s true. Thanks for your assistance.

1 Like