Datasources
Last updated
Last updated
A datasource is a named connection to a specific database with specified credentials. You can define a datasource in one of three locations:
For web applications,
The datasource is then used to control the database's connection pool and allow the BoxLang engine to execute JDBC calls against it.
The following database vendors are supported and available:
Each database we support comes with an installable BoxLang module which either
provides the necessary client dependencies for making JDBC connections to a running database server (MySQL, Postgres, etc.)
OR contains the database vendor itself, as in the case of Apache Derby or HyperSQL, which are both in-memory database.
To use any of these databases you'll need to install its BoxLang module to support JDBC connections to that datasource.
The datasource configuration struct should be defined exactly the same whether you are using an inline, ad-hoc datasource or configuring a datasource in your boxlang.json
or Application.bx
. Make sure you have a "driver" key defined OR the driver clearly denoted in the JDBC url:
boxlang.json
You can define a datasource at the BoxLang runtime level by placing it in your boxlang.json
:
Application.bx
For web runtimes, you can also define the datasources in the Application.bx
, which is sometimes our preferred approach as the connections are versioned controlled and more visible than in the admin. You will do this by defining a struct called this.datasources
. Each key will be the name of the datasource to register and the value of each key a struct of configuration information for the datasource. However, we recommend that you setup environment variables in order to NOT store your passwords in plain-text in your source code.
For the inline approach, you will use the struct definition, as you see in the Application.bx
above and pass it into the bx:query
or queryexecute
call.
Finally, for smaller or simpler applications with few queries, you may find it useful to define your datasource at query time. So instead of giving the name of the datasource
, it can be a struct
definition of the datasource you want to connect to:
You can also define a default datasource to allow you to omit the datasource
completely from query calls.
To do this, you'll need to define a default datasource in one of two locations:
In your BoxLang runtime's boxlang.json
config file via the defaultDatasource
key
or, for web server runtimes, in a this.datasource
variable in your Application.bx
file
CF Mappings
Data sources
Mail servers
Request, session, or application timeouts
Licensing information
Passwords
Template caching settings
Basically any settings in the web based administrator
You can easily place a .cfconfig.json
in the web root of your project, and if you start up a CommandBox server on any BoxLang engine, CFConfig will transfer the configuration to the engine's innards:
maxConnections
minConnections
connectionTimeout
idleTimeout
maxLifetime
keepaliveTime
BoxLang offers a number of pool statistics which can be retrieved from the datasource object. To do this, first find your datasource name from the list of registered datasources:
Next, retrieve the datasource by its unique datasource name, and call .getPoolStats()
on the result:
This returns a struct of pool metadata including the following keys:
pendingThreads
idleConnections
totalConnections
activeConnections
maxConnections
minConnections
Find out what datasources you have defined by dumping out this getBoxContext().getRuntime().getDatasourceService().getNames()
Note the use of BoxLang's environment variable replacement syntax for the datasource properties: ${env.MYSQL_HOST:localhost}
. See for more info.
You can also make your data sources portable from application to application or BoxLang engine to engine by using our project. CFConfig allows you to manage almost every setting that shows up in the web administrator, but instead of logging into a web interface, you can manage it from the command line by hand or as part of a scripted server setup. You can seamlessly transfer config for all the following:
Property | Type | Default | Description |
---|
In addition to the above properties, you can include any you'd like in your datasource configuration:
BoxLang uses under the hood for connection pooling. Each datasource gets a dedicated connection pool. Use these configuration properties to adjust the pool size and behavior: