BoxLang 🚀 A New JVM Dynamic Language Learn More...
|:------------------------------------------------------: |
| ⚡︎ B o x L a n g ⚡︎
| Dynamic : Modular : Productive
|:------------------------------------------------------: |
Copyright Since 2023 by Ortus Solutions, Corp
www.boxlang.io | www.ortussolutions.com
This module provides a BoxLang JDBC driver for MySQL, enabling
integration between BoxLang applications and MySQL databases through
BoxLang's queryExecute() and datasource management.
com.mysql:mysql-connector-j driver with
performance-tuned connection defaultscustom
queryExecute() and datasource definitionsloadbalance and replication connection protocolsbox install bx-mysql
# Into the BoxLang HOME
install-bx-module bx-mysql
# Or a local folder
install-bx-module bx-mysql --local
Once installed, define a datasource and use it:
// Application.bx
this.datasources[ "myDB" ] = {
"driver" : "mysql",
"host" : "localhost",
"port" : 3306,
"database": "mydb",
"username": "root",
"password": "secret"
};
// Use it in your code
result = queryExecute( "SELECT 1 AS test", [], { "datasource": "myDB" } );
See BoxLang's Defining Datasources documentation for full examples on where and how to construct a datasource connection pool.
this.datasources[ "myDB" ] = {
"driver" : "mysql",
"host" : "db.example.com",
"port" : 3306,
"database": "mydb",
"username": "app",
"password": "secret"
};
Define the datasource directly in the queryExecute() options:
result = queryExecute(
"SELECT * FROM users WHERE id = ?",
[ 1 ],
{
"datasource": {
"driver" : "mysql",
"host" : "localhost",
"database": "mydb",
"username": "root",
"password": "secret"
}
}
);
Set protocol to use a high availability connection mode.
Valid values are loadbalance and
replication. Any other value throws an error.
this.datasources[ "haDB" ] = {
"driver" : "mysql",
"protocol": "loadbalance",
"host" : "db.example.com",
"database": "mydb",
"username": "app",
"password": "secret"
};
Every default below is appended to the JDBC URL as a query string
parameter. Because they are URL parameters and not fixed pool
properties, you can override any of them with the custom struct.
See the Connector/J performance notes for background.
| Parameter | Default | Purpose |
|---|---|---|
prepStmtCacheSize
| 250
| Number of prepared statements the driver caches per connection |
prepStmtCacheSqlLimit
| 2048
| Maximum length of a prepared SQL statement the driver will cache |
cachePrepStmts
| true
| Enables the prepared statement cache. The two settings above have no effect without it |
useServerPrepStmts
| true
| Uses server-side prepared statements for a large performance boost |
useLocalSessionState
| true
| Uses local session state instead of extra round trips to the server |
rewriteBatchedStatements
| true
| Rewrites batched statements into multi-statement or multi-value queries |
cacheResultSetMetadata
| true
| Caches result set metadata |
cacheServerConfiguration
| true
| Caches server configuration variables |
elideSetAutoCommits
| true
| Skips redundant SET autocommit calls |
maintainTimeStats
| false
| Disables internal time statistics tracking |
Add any MySQL
Connector/J parameter to custom. Your values win
over the defaults, and the remaining defaults still apply.
this.datasources[ "myDB" ] = {
"driver" : "mysql",
"database": "mydb",
"username": "root",
"password": "secret",
"custom" : {
// Override a default
"useServerPrepStmts": false,
// Add your own
"connectTimeout": 5000
}
};
custom can also be a query string:
"custom": "useServerPrepStmts=false&connectTimeout=5000"
// Create a table
queryExecute( "
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
", [], { "datasource": "myDB" } );
// Insert data
queryExecute(
"INSERT INTO users ( name, email ) VALUES ( ?, ? )",
[ "John Doe", "[email protected]" ],
{ "datasource": "myDB" }
);
// Query data
users = queryExecute(
"SELECT * FROM users WHERE email = ?",
[ "[email protected]" ],
{ "datasource": "myDB" }
);
// Update data
queryExecute(
"UPDATE users SET name = ? WHERE id = ?",
[ "John Smith", 1 ],
{ "datasource": "myDB" }
);
transaction {
queryExecute(
"INSERT INTO users ( name, email ) VALUES ( ?, ? )",
[ "User 1", "[email protected]" ],
{ "datasource": "myDB" }
);
queryExecute(
"INSERT INTO users ( name, email ) VALUES ( ?, ? )",
[ "User 2", "[email protected]" ],
{ "datasource": "myDB" }
);
}
gradle.properties)# Clone the repository
git clone https://github.com/ortus-boxlang/bx-mysql.git
cd bx-mysql
# Build the module
./gradlew build
# Run tests
./gradlew test
# Create the module structure for local testing
./gradlew createModuleStructure
bx-mysql/
├── src/
│ ├── main/
│ │ ├── bx/
│ │ │ └── ModuleConfig.bx # Module configuration
│ │ ├── java/
│ │ │ └── ortus/boxlang/modules/
│ │ │ └── mysql/
│ │ │ └── MySQLDriver.java # JDBC driver implementation
│ │ └── resources/
│ └── test/
│ └── java/ # Unit tests
├── build.gradle # Build configuration
├── box.json # ForgeBox module manifest
└── readme.md # This file
# Run all tests
./gradlew test
# Run a specific test class
./gradlew test --tests "MySQLDriverTest"
git checkout -b feature/amazing-feature)./gradlew test)./gradlew spotlessApply)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)The database property is required. Ensure your datasource configuration includes:
"database": "mydb"
The protocol 'x' is not valid for the MySQL Driver. Available protocols are [loadbalance, replication]
See changelog.md for a complete list of changes and version history.
Licensed under the Apache License, Version 2.0. See LICENSE for details.
BoxLang is a professional open-source project and it is completely funded by the community and Ortus Solutions, Corp. Ortus Patreons get many benefits like a cfcasts account, a FORGEBOX Pro account and so much more. If you are interested in becoming a sponsor, please visit our patronage page: https://patreon.com/ortussolutions
"I am the way, and the truth, and the life; no one comes to the Father, but by me (JESUS)" Jn 14:1-12
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
bx-mysql/1.2.0/) instead of bx-mysql/1.2.0-snapshot/, so bx-mysql@be installs returned a 404. The release workflow now appends -snapshot to the version on development../gradlew) instead of the Gradle setup action and a separately pinned Gradle version.prepStmtCacheSize, cachePrepStmts, useServerPrepStmts, etc.) are now default JDBC URL params (defaultCustomParams) instead of default Hikari properties, so they can be overridden via the datasource custom struct.ubuntu-latest (the retired ubuntu-20.04 runner left the PR workflow queued forever). Removed the CommandBox format:check step (no such script exists; ./gradlew spotlessCheck is the format check).mysql-connector-j to version 9.2.0 to address SNYK-JAVA-COMGOOGLEPROTOBUF-8055227
$
box install bx-mysql