Skip to content

Commit bbd981e

Browse files
committed
Add support for MariaDB driver fallback to MySQL in ConnectionManager
- Implemented a fallback mechanism for the MariaDB driver to use MySQL if the MariaDB driver is not present. - Added configuration option to enable or disable this fallback behavior. - Updated Javadoc to reflect new functionality and usage.
1 parent 42edc32 commit bbd981e

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

SimpleAPI/src/main/java/com/bencodez/simpleapi/sql/mysql/ConnectionManager.java

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
import lombok.Getter;
1010
import lombok.Setter;
1111

12+
/**
13+
* Manages a HikariCP-backed SQL connection pool for MySQL/MariaDB/PostgreSQL.
14+
*
15+
* Supports a failover mode where if {@link DbType#MARIADB} is selected but the MariaDB JDBC
16+
* driver is not present, it will automatically fall back to the MySQL driver.
17+
*/
1218
public class ConnectionManager {
1319

1420
@Getter
@@ -94,6 +100,14 @@ public class ConnectionManager {
94100
@Setter
95101
private DbType dbType = DbType.MYSQL;
96102

103+
/**
104+
* If true and {@link DbType#MARIADB} is selected, but the MariaDB driver is not present,
105+
* fall back to using the MySQL driver automatically.
106+
*/
107+
@Getter
108+
@Setter
109+
private boolean mariadbFallbackToMysqlDriver = true;
110+
97111
public ConnectionManager(String host, String port, String username, String password, String database) {
98112
this.host = host;
99113
this.port = port;
@@ -143,29 +157,54 @@ private void ensureDriverPresent(String className) throws ClassNotFoundException
143157
Class.forName(className);
144158
}
145159

160+
/**
161+
* Resolves the JDBC driver class to use.
162+
*
163+
* Behavior:
164+
* - If {@link #mysqlDriver} is explicitly set, that driver must be present and is used.
165+
* - Otherwise, uses the driver implied by {@link #dbType}.
166+
* - If {@link DbType#MARIADB} is selected and the MariaDB driver isn't present, optionally falls back to the MySQL driver.
167+
*
168+
* @return The resolved driver class name.
169+
* @throws ClassNotFoundException if no suitable driver is available.
170+
*/
146171
private String resolveDriver() throws ClassNotFoundException {
147172
// Allow explicit override (keeps field name mysqlDriver for minimal churn)
148173
if (mysqlDriver != null && !mysqlDriver.isEmpty()) {
149174
ensureDriverPresent(mysqlDriver);
150175
return mysqlDriver;
151176
}
152177

153-
String className;
154178
switch (dbType) {
155179
case POSTGRESQL:
156-
className = "org.postgresql.Driver";
157-
break;
180+
ensureDriverPresent("org.postgresql.Driver");
181+
return "org.postgresql.Driver";
158182
case MARIADB:
159-
className = "org.mariadb.jdbc.Driver";
160-
break;
183+
return resolveMariaDbWithFallback();
161184
case MYSQL:
162185
default:
163-
className = "com.mysql.cj.jdbc.Driver";
164-
break;
186+
ensureDriverPresent("com.mysql.cj.jdbc.Driver");
187+
return "com.mysql.cj.jdbc.Driver";
165188
}
189+
}
166190

167-
ensureDriverPresent(className);
168-
return className;
191+
/**
192+
* Resolves MariaDB driver with optional fallback to MySQL driver if MariaDB driver is missing.
193+
*
194+
* @return Driver class name to use.
195+
* @throws ClassNotFoundException if neither MariaDB nor fallback MySQL driver is present.
196+
*/
197+
private String resolveMariaDbWithFallback() throws ClassNotFoundException {
198+
try {
199+
ensureDriverPresent("org.mariadb.jdbc.Driver");
200+
return "org.mariadb.jdbc.Driver";
201+
} catch (ClassNotFoundException e) {
202+
if (!mariadbFallbackToMysqlDriver) {
203+
throw e;
204+
}
205+
ensureDriverPresent("com.mysql.cj.jdbc.Driver");
206+
return "com.mysql.cj.jdbc.Driver";
207+
}
169208
}
170209

171210
private String buildJdbcUrl(String driverClassName) {

0 commit comments

Comments
 (0)