MariaDB Connector/J with azure redirection support
Перейти к файлу
hzhang87 157fa84400
Create codeql.yml
2024-03-20 13:53:15 +08:00
.github/workflows Create codeql.yml 2024-03-20 13:53:15 +08:00
.travis [CONJ-827] update maxscale test to recent version 2020-09-14 10:40:56 +02:00
documentation [CONJ-769] Add a hint about createDatabaseIfNotExist 2020-03-09 14:42:10 +01:00
src add back serverStatus 2020-10-27 10:07:48 +08:00
.gitattributes Migrated from Bazaar to Git. 2015-01-30 14:19:04 +01:00
.gitignore [CONJ-369] Encoding correction on clob column with useServerPrepStmts set to true 2016-10-19 17:14:14 +02:00
.java.policy Fix for CONJ-121: implement Connection.getNetworkTimeout and Connection.setNetworkTimeout 2014-12-04 06:40:18 -05:00
.travis.yml [CONJ-827] update maxscale test to recent version 2020-09-14 10:40:56 +02:00
CHANGELOG.md [CONJ-810] normalization of resultset getDate/getTime of timestamp field. 2020-09-24 14:31:05 +02:00
CONTRIBUTING.md Support the Data Service Specification for JDBC™ Technology 2019-02-28 12:30:09 +01:00
LICENSE Adding LICENSE file, LGPL2.1 2013-04-04 18:59:30 +02:00
README.md Update README.md 2021-02-04 10:31:26 +08:00
SECURITY.md Microsoft mandatory file 2023-06-02 18:21:57 +00:00
appveyor-download.bat [misc] adding MariaDB 10.5 tests on travis and Appveyor 2020-01-29 13:42:07 +01:00
appveyor.yml [misc] update appveyor server version to latest 2020-08-21 16:27:59 +02:00
azure-pipelines.yml [MENT-28] azure testing on ubuntu using tar server artifact 2019-07-19 10:42:36 +02:00
build.xml [misc] update copyright 2020-03-02 15:05:17 +01:00
codecov.yml [misc] Disabling github codecov comment 2019-05-21 11:03:10 +02:00
maven-build.properties [misc] update copyright 2020-03-02 15:05:17 +01:00
maven-build.xml [misc] update copyright 2020-03-02 15:05:17 +01:00
pom.xml update pom 2021-02-03 10:37:32 +08:00

README.md

MariaDB Java Connector For Azure MySQL/MariaDB

For general introduction for MariaDB java connector, please check on https://github.com/MariaDB/mariadb-connector-j. The source code here is in the latest version same with the related branch on https://github.com/MariaDB/mariadb-connector-j with redirection feature support. Take advantage of this client to improve the connection latency between application front end and Azure Database for MySQL/MariaDB. Following is a brief guide of how to install and test the driver.

Available versions

Maven repository version

This driver is now available from Maven central repository search from https://mvnrepository.com/artifact/com.microsoft.azure/azure-mariadb-connector-java. The version number is same with related branch on https://github.com/MariaDB/mariadb-connector-j with redirection feature support. You may use following sample pom definition to include the driver in your project:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-mariadb-connector-java</artifactId>
    <version>2.7.0</version>
</dependency>

Source code version

The source code here is in the latest status same with the realted branch on https://github.com/MariaDB/mariadb-connector-j with redirection feature support(2020-06-03). Valid branches:

  • master: based on MariaDB/mariadb-connector-j branch master with redirection support.
  • 2.7.0-with-redirection: based on MariaDB/mariadb-connector-j tag 2.7.0 with redirection support.
  • 2.6.0-with-redirection: based on MariaDB/mariadb-connector-j tag 2.6.0 with redirection support.
  • 2.5.1-with-redirection: based on MariaDB/mariadb-connector-j tag 2.5.1 with redirection support.

Notice There is an issue traced for MariaDB/mariadb-connector-j 2.5.2+ on https://jira.mariadb.org/browse/CONJ-807. The problem has been fixed on 2.7.0. The recommended branch is 2.7.0-with-redirection or 2.5.1-with-redirection.

Redirection option Usage

A new connection option is introduced for redirection and the option name is enableRedirect, default value: off.

The detailed usage of the option enableRedirect is as follows:

off(0) - Redirection will not be used.
on(1) Will enforce redirection.
- If redirection is not supported on the server, the connection will be aborted and the following error is returned: "Connection aborted because redirection is not enabled on the MySQL server or the network package doesn't meet redirection protocol."
- If the MySQL server supports redirection, but the redirected connection failed for any reason, also abort the first proxy connection. Return the error of the redirected connection.
preferred(2) - It will use redirection if possible.
- If the connection does not use SSL on the driver side, the server does not support redirection, or the redirected connection fails to connect for any non-fatal reason while the proxy connection is still a valid one, it will fall back to the first proxy connection.

Step to build and install from source

Tools prerequisite to build the drivers

  • Java 1.8+
  • Maven

Build

mvn package

The default test database names is testj which need to be created ahead, user is root, and without password. You can also specify the connection string as follows for test:

mvn -DdbUrl=jdbc:mariadb://localhost:3306/testj?user=root&password=xxx -DlogLevel=FINEST package

Please notice that the unit test sets is not designed fully compatibitale with Azure MySQL server, so don't run this test against Azure MySQL server and expect a full pass.

If you want to build without running unit tests and document check, use:

mvn -Dmaven.javadoc.skip=true -Dmaven.test.skip=true package

Install

After build, you should have JDBC jar mariadb-java-client-x.y.z.jar in the 'target' subdirectory, e.g. mariadb-java-client-2.5.1.jar. Replace this jar with the mariadb-java-client jar package you currently used in your environemnt. Following are two use examples in different scenario:

After install, specify the connection string setting with enableRedirect option, e.g.

jdbc:mysql://xxx.mysql.database.azure.com/testj/?user=xx&password=xx&useSSL=true&serverSslCert=xx/BaltimoreCyberTrustRoot.crt.pem&enableRedirect=on"

Notice: Please notice that there is a limitation for Azure DB for MySQL where redirection is only possible when the connection is configured with SSL and only works with TLS 1.2 with a FIPS approved cipher for redirection.

Test

    // Load the JDBC driver
    Class.forName("org.mariadb.jdbc.Driver");
    System.out.println("Driver loaded");

    int count = 10;
    String query = "SELECT 1";
    int i=0;
    // Try to connect
    String url = "jdbc:mysql://xxx.mysql.database.azure.com"+
            "?verifyServerCertificate=false"+
            "&useSSL=true"+
            "&requireSSL=true" +
			"&enableRedirect=on";

    Connection connection = DriverManager.getConnection (url, "username", "password");
    double t1 = System.nanoTime();
    Statement s1 = connection.createStatement();
    for(i=0;i<count;i++)
    {
        s1.executeQuery(query);
    }
    double t2 = System.nanoTime();
    System.out.println (" time = " + (t2 - t1)/count/1000000);
    connection.close();