Skip to content

Commit

Permalink
feat(jdbc): Remove MySQL ConnectorJ and use MariaDb instead (#2506)
Browse files Browse the repository at this point in the history
Backporting from #2502
  • Loading branch information
ev-codes committed May 8, 2024
1 parent 2bdd9e1 commit ccbd444
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
19 changes: 13 additions & 6 deletions connectors/jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,25 @@
<version>12.6.1.jre11</version>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.3</version>
</dependency>

<!-- Mariadb is used because of an issue with the MySQL ConnectorJ licence (GPL). We're not mentioning Mariadb in our docs -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.3.3</version>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public enum SupportedDatabase {
MSSQL("com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://"),
MYSQL("com.mysql.cj.jdbc.Driver", "jdbc:mysql://"),
MYSQL("org.mariadb.jdbc.Driver", "jdbc:mysql://"),
POSTGRESQL("org.postgresql.Driver", "jdbc:postgresql://");

private final String driverClassName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import io.camunda.connector.api.error.ConnectorException;
import io.camunda.connector.jdbc.model.request.JdbcRequest;
import io.camunda.connector.jdbc.model.request.SupportedDatabase;
import io.camunda.connector.jdbc.model.request.connection.JdbcConnection;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Expand All @@ -20,20 +22,41 @@ public class ConnectionHelper {
private static final Logger LOG = LoggerFactory.getLogger(ConnectionHelper.class);

public static Connection openConnection(JdbcRequest request) {
SupportedDatabase database = request.database();
String driverClassName = database.getDriverClassName();
try {
LOG.debug("Executing JDBC request: {}", request);
LOG.debug("Loading JDBC driver: {}", request.database().getDriverClassName());
Class.forName(request.database().getDriverClassName());
LOG.debug("Loading JDBC driver: {}", driverClassName);
Class.forName(driverClassName);
JdbcConnection connection = request.connection();
Connection conn =
DriverManager.getConnection(
connection.getConnectionString(request.database()), connection.getProperties());
LOG.debug("Connection established for Database {}: {}", request.database(), conn);
ensureMySQLCompatibleUrl(connection.getConnectionString(database), database),
connection.getProperties());
LOG.debug("Connection established for Database {}: {}", database, conn);
return conn;
} catch (ClassNotFoundException e) {
throw new ConnectorException("Cannot find class: " + request.database().getDriverClassName());
throw new ConnectorException("Cannot find class: " + driverClassName);
} catch (URISyntaxException e) {
throw new ConnectorException("Cannot parse the Database connection URL: " + e.getMessage());
} catch (SQLException e) {
throw new ConnectorException("Cannot create the Database connection: " + e.getMessage());
}
}

/**
* Ensure MySQL compatibility as we are using MariaDB driver for MySQL.
*
* @return Properties with permitMysqlScheme set to true if the database is MySQL.
* @see <a
* href="https://mariadb.com/kb/en/about-mariadb-connector-j/#jdbcmysql-scheme-compatibility">Compatibility
* details</a>
*/
private static String ensureMySQLCompatibleUrl(String url, SupportedDatabase database)
throws URISyntaxException {
if (database == SupportedDatabase.MYSQL) {
return ConnectionParameterHelper.addQueryParameterToURL(url, "permitMysqlScheme");
}
return url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,13 @@ public class ConnectionStringHelper {
public static String buildConnectionString(
SupportedDatabase database, DetailedConnection connection) {
return switch (database) {
case MYSQL -> buildMySqlConnectionString(database, connection);
case POSTGRESQL -> buildPostgresConnectionString(database, connection);
case MYSQL, POSTGRESQL -> buildCommonConnectionString(database, connection);
case MSSQL -> buildMssqlConnectionString(database, connection);
default -> throw new ConnectorException("Unsupported database: " + database);
};
}

private static String buildMySqlConnectionString(
SupportedDatabase database, DetailedConnection connection) {
String host = connection.host();
String port = connection.port();
String username = connection.username();
String password = connection.password();
String databaseName = connection.databaseName();
String authentication = "";
if (username != null && !username.isEmpty()) {
authentication += username;
if (password != null && !password.isEmpty()) {
authentication += ":" + password + "@";
}
}
String connectionString = database.getUrlSchema() + authentication + host + ":" + port;
if (databaseName != null && !databaseName.isEmpty()) {
connectionString += "/" + databaseName;
}
return connectionString;
}

private static String buildPostgresConnectionString(
private static String buildCommonConnectionString(
SupportedDatabase database, DetailedConnection connection) {
String host = connection.host();
String port = connection.port();
Expand Down

0 comments on commit ccbd444

Please sign in to comment.