Connect with MariaDB Connector/J
This page is part of MariaDB's Documentation.
The parent of this page is: MariaDB Connector/J
Topics on this page:
Overview
Java developers can use MariaDB Connector/J to establish client connections with MariaDB database products.
Connection URL Syntax
The connection URL syntax used to connect to MariaDB with Connector/J is as follows. Optional parameters are shown enclosed in []
:
jdbc:mariadb:[<haMode>:]//[<hostDescription>[,<hostDescription>]]/[<database>][?<key1>=<value1>[&<key2>=<value2>]] }
The connection URL:
Requires
jdbc:mariadb://
as the protocol component.
Accepts an optional High Availability (HA) mode in the
haMode
field.Accepts an optional Host Description in the
hostDescription
field.
Accepts an optional database name after the slash.
Accepts optional connection parameters in
key=value
format.
Example Connection URLs
Some example connection URLs:
jdbc:mariadb://192.0.2.1/
jdbc:mariadb://192.0.2.1/?user=db_user&password=db_user_password
jdbc:mariadb://192.0.2.1/database?user=db_user&password=db_user_password
jdbc:mariadb://192.0.2.1:3307/database?user=db_user&password=db_user_password
jdbc:mariadb://mariadb.example.com/database?user=db_user&password=db_user_password
jdbc:mariadb://mariadb.example.com:3307/database?user=db_user&password=db_user_password
jdbc:mariadb://localhost/database?localSocket=/var/lib/mysql/mysql.sock&user=db_user&password=db_user_password
High Availability (HA) Modes
MariaDB Connector/J supports the use of multiple nodes in JDBC connection URL. For multi-node environments, Connector/J provides basic failover between the nodes when establishing connections.
Connector/J also features optional High Availability (HA) modes, which provide additional support for load balancing and failover.
HA modes are useful in multi-node environments, such as:
topology, powered by Galera
topology, which uses MariaDB Replication
Topologies with multiple redundant MariaDB MaxScale nodes
HA Mode | Description |
---|---|
Alias of the loadbalance HA mode. | |
Balances read and write load between configured nodes. | |
Balances read and write load between configured primary servers. Balances read load between configured replica servers. | |
Provides connection failover between configured nodes. |
Load Balancing Mode
In the loadbalance
HA mode, MariaDB Connector/J load balances JDBC connections across multiple nodes, as specified in the JDBC connection string. When sending read and write queries, Connector/J balances the load by randomly selecting from configured nodes. This HA mode does not support load balancing read queries across replica servers.
To enable the loadbalance
HA mode:
jdbc:mariadb:loadbalance://192.0.2.1,192.0.2.2,192.0.2.3/test
The failover
HA mode is an alias of the loadbalance
mode.
Replication Mode
In the replication
HA mode, MariaDB Connector/J load balances JDBC connections across multiple nodes, as specified in the JDBC connection string. When sending read and write queries, Connector/J balances the load between available primary servers. It can also balance the read query load across available replica servers.
To enable the replication
HA mode:
jdbc:mariadb:replication://192.0.2.1,192.0.2.2,192.0.2.3/test
Sequential Mode
In the sequential
HA mode, MariaDB Connector/J provides failover for JDBC connections across multiple nodes, as specified in the JDBC connection string. Connector/J selects the first node from the configured sequence. If the connection attempt fails, it attempts to connect to the next node in the sequence.
To use the Sequential HA mode:
jdbc:mariadb:sequential://192.0.2.1,192.0.2.2,192.0.2.3/test
Transaction Replay
Starting with MariaDB Connector/J 3.0, in-progress transactions can be replayed after failover occurs.
Connector/J implements transaction replay by caching statements for each transaction in a transaction cache. If a failover occurs before a transaction is committed or rolled back, the transaction's cached statements are re-executed on the new primary server. If the number of statements in the transaction cache exceeds transactionReplaySize
, caching will be disabled until the transaction is committed or rolled back.
When transaction replay is enabled, Connector/J requires that applications only use idempotent queries.
Transaction replay is enabled by setting the transactionReplay
parameter:
jdbc:mariadb:replication://192.0.2.1,192.0.2.2,192.0.2.3/test?transactionReplay=true&transactionReplaySize=128
Host Description
The hostDescription
field in the connection URL has a simple syntax and a complex syntax.
For TCP/IP connections, the hostDescription
field is required.
However, the hostDescription
field can be omitted in some scenarios. For example:
For local Unix socket connections:
jdbc:mariadb:///database?localSocket=/var/lib/mysql/mysql.sock
For local named pipe connections:
jdbc:mariadb:///database?pipe=mariadb
Simple Syntax
The hostDescription
simple syntax is as follows:
<host>:<port>
Requires an IP address or Fully Qualified Domain Name (FQDN) to the target node.
Accepts optional port number to connect to on the target node.
For example:
jdbc:mariadb://192.0.2.1:3306/database?user=db_user&password=db_user_password
Complex Syntax
The hostDescription
complex syntax is as follows:
address=[(type=(master|replica))][(port=<portnumber>)](host=<host>)
type
sets whether the node is a primary server or a replicaport
sets the port number on which to connecthost
sets the IP address or Fully Qualified Domain Name (FQDN) where the node is located
For example:
jdbc:mariadb://address=(type=master)(host=primary1),address=(port=3307)(type=replica)(host=replica1)/database?user=greg&password=pass
Optional Connection Parameters
MariaDB Connector/J supports several optional connection parameters
:Parameter Name | Description | Data Type | Default Value | Version Added | Version Removed |
---|---|---|---|---|---|
|
|
| 2.2.0 | 3.0.3 | |
|
|
| 1.3.0 | 3.0.3 | |
| Defines whether the connector automatically reconnects after a connection failure. |
|
| 1.2.0 | 3.0.3 |
| Defines whether the prepared statement cache is enabled. |
|
| 1.3.0 | 3.0.3 |
|
|
| 1.4.0 | 3.0.3 | |
| Defines the connect timeout value in milliseconds. When set to |
|
| 1.1.8 | |
|
|
| 3.0.3 | ||
| Deprecated in 3.0.3. Use |
|
| 2.4.0 | |
| Forces the TLS protocol to only use the specified comma-separated list of TLS ciphers. |
| 1.5.0 | ||
| Forces the TLS protocol to only use the specified comma-separated list of TLS versions. |
| 1.5.0 | ||
|
|
| 1.6.0, 2.0.1 | 3.0.3 | |
| 3.0.3 | ||||
| Defines whether an exception is thrown when only read-only servers are available. |
|
| 1.2.0 | 3.0.3 |
| Applies only when HA mode is specified. Defines the number of times the connector silently loops through available hosts and attempts to reconnect after a connection failure. Between loops, the connector will pause for 250 ms if no servers are available. When set to 0, the connector does not loop through available hosts or attempt to reconnect. |
|
| 1.1.8 | 3.0.3 |
| Enables execution of a SQL statement upon connection creation. |
| 3.0.6 | ||
|
|
| 1.4.0 | 3.0.3 | |
|
| 1.5.3 | 3.0.3 | ||
| File path to the keyStore file that contains the private key store and associated certificates. This parameter is similar to the Java System property javax.net.ssl.keyStore, but ensures that only the private key entries are used. |
| 1.3.4 | ||
| Password for the client certificate keyStore. This parameter is similar to the Java System property javax.net.ssl.keyStorePassword. |
| 1.3.4 | ||
| Defines the keyStore type (JKS/PKCS12). |
| 2.4.0 | ||
|
|
| 1.2.0 | 3.0.3 | |
| Defines the Unix socket file to use for connections to |
| 1.1.4 | ||
| Defines the hostname or IP address to use for the connection socket when connecting to |
| 1.1.8 | ||
|
|
| 1.5.0 | 3.0.3 | |
| 3.0.3 | ||||
| 3.0.3 | ||||
| Defines the password of the user account to connect with. |
| 1.0.0 | ||
| Defines the character set used to encode the password. When not set, the connector uses the platform's default character set. |
| 1.5.9 | 3.0.3 | |
| Enables the use of |
|
| 3.0.3 | |
| For Windows. Defines the name of the named pipe to use for connections to localhost via named pipe. Default named pipe name is |
| 1.1.3 | ||
| 3.0.3 | ||||
| Defines the maximum length for a prepared statement in the cache. This parameter only applies if cachePrepStmts is enabled. |
|
| 1.3.0 | 3.0.3 |
|
|
| 1.5.0 | 3.0.3 | |
| Sets a comma-separated list of client authentication plugins to allow. The following client authentication plugins are supported:
|
|
| 3.0.3 | |
|
|
| 1.2.0 | 3.0.3 | |
|
|
| 1.1.8 | 3.0.3 | |
| Defines the server certificate or the CA chain. It accepts:
|
| 1.1.3 | ||
| 1.1.7 | 3.0.3 | |||
| For Windows. Defines the shared memory base name to use for connections to localhost via shared memory. |
| 1.1.4 | 3.0.3 | |
|
|
| 1.5.0 | 3.0.3 | |
| Defines the class to use for a custom socket factory. Specify the full name of the class that implements |
| 1.0.0 | ||
| Defines the network socket timeout ( |
|
| 1.1.8 | |
| Enables SSL/TLS in a specific mode. The following values are supported:
This new option replaces the deprecated options:
|
|
| 3.0.3 | |
|
|
| 2.2.0 | 3.0.3 | |
| Sets the |
|
| 3.0.3 | |
| Sets the |
|
| 3.0.3 | |
| Sets the |
|
| 3.0.3 | |
| 1.0.0 | 3.0.3 | |||
| 1.0.0 | 3.0.3 | |||
| 1.0.0 | 3.0.3 | |||
|
|
| 2.5.4 | 3.0.3 | |
| Enables transaction caching. If a failover occurs before a transaction is committed or rolled back, the transaction's cached statements are re-executed on the new primary server. Connector/J requires that applications only use idempotent queries. If the number of statements in the transaction cache exceeds |
|
| 3.0.3 | |
| Sets the number of statements that should be saved in the transaction cache when |
|
| 3.0.3 | |
| Deprecated in 3.0.3. Use |
|
| 1.1.1 | |
|
| 1.3.4 | 3.0.3 | ||
|
| 1.3.4 | 3.0.3 | ||
|
|
| 2.4.0 | 3.0.3 | |
|
|
| 1.5.0 | 3.0.3 | |
|
|
| 1.5.0 | 3.0.3 | |
|
|
| 1.0.0 | 3.0.3 | |
| 3.0.3 | ||||
|
|
| 1.1.9 | 3.0.3 | |
| Defines whether pipelines are used to send internal queries during the initial authentication and connection process. Several queries are executed during this stage. When pipelines are used, all queries are sent simultaneously, and then all results are reads. This approach can decrease overhead and permit faster connection creation. |
|
| 1.6.0 | |
| Defines the user name of the user account to connect with. |
| Current user | 1.0.0 | |
| Defines whether TLS is used for the connection. Deprecated in 3.0.3. Use |
|
| 1.1.0 | |
| Defines the valid connection timeout in seconds. When the connector is using multiple hosts and a connection has not been used longer than the configured valid connection timeout, the connector will verify that the connection is still valid. When set to |
|
| 1.2.0 | 3.0.3 |
Code Example: Connect
The following example program opens a single database connection to a server at the IP address 192.0.2.1
using the user account created in the example setup. If your application requires multiple database connections, see Connection Pools for a more appropriate example.
import java.sql.*;
import java.util.Properties;
public class App {
public static void main(String[] argv) {
Properties connConfig = new Properties();
connConfig.setProperty("user", "db_user");
connConfig.setProperty("password", "db_user_password");
try (Connection conn = DriverManager.getConnection(
"jdbc:mariadb://192.0.2.1:3306", connConfig)) {
// Use Connection
// ...
} catch (Exception e) {
e.printStackTrace();
}
}
}