MySQL database type plugin for Flyway database migration engine providing specialized MySQL and MariaDB support for schema migrations.
—
Connection management classes handle MySQL-specific connection state, variable management, and AWS RDS detection. They ensure proper connection restoration and provide specialized functionality for MySQL environments.
MySQL-specific connection implementation with state management, variable restoration, and environment detection.
/**
* MySQL-specific connection implementation
* Handles connection state, user variables, and AWS RDS detection
*/
public class MySQLConnection extends Connection<MySQLDatabase> {
/**
* Creates a new MySQL connection instance
* Initializes connection state and detects environment capabilities
* @param database MySQL database instance
* @param connection Raw JDBC connection
*/
public MySQLConnection(MySQLDatabase database, java.sql.Connection connection);
/**
* Detects if connection is to AWS RDS
* @return true if connected to AWS RDS MySQL instance
*/
public boolean isAwsRds();
/**
* Restores original connection state on connection close
* Resets user variables and restores system variables
* @throws SQLException if restoration fails
*/
@Override
protected void doRestoreOriginalState() throws SQLException;
/**
* Gets current schema name (database name in MySQL)
* @return Current database name or null if none selected
* @throws SQLException if query fails
*/
@Override
protected String getCurrentSchemaNameOrSearchPath() throws SQLException;
/**
* Changes current schema (database) to specified name
* @param schema Schema name to switch to, or null to clear
* @throws SQLException if schema change fails
*/
@Override
public void doChangeCurrentSchemaOrSearchPathTo(String schema) throws SQLException;
/**
* Gets current schema object
* @return MySQLSchema instance for current database, or null if none
* @throws SQLException if query fails
*/
@Override
protected Schema doGetCurrentSchema() throws SQLException;
/**
* Gets schema object for specified name
* @param name Schema (database) name
* @return MySQLSchema instance
*/
@Override
public Schema getSchema(String name);
/**
* Executes callable with table lock protection
* Uses named locks when available, otherwise falls back to superclass
* @param <T> Return type
* @param table Table to lock
* @param callable Operation to execute with lock
* @return Result from callable
*/
@Override
public <T> T lock(Table table, Callable<T> callable);
/**
* Determines if named lock template can be used
* Based on database version and user permissions
* @return true if named locks are available
*/
protected boolean canUseNamedLockTemplate();
}Constants and Fields:
// System table names for user variable queries
private static final String USER_VARIABLES_TABLE_MARIADB = "information_schema.user_variables";
private static final String USER_VARIABLES_TABLE_MYSQL = "performance_schema.user_variables_by_thread";
private static final String FOREIGN_KEY_CHECKS = "foreign_key_checks";
private static final String SQL_SAFE_UPDATES = "sql_safe_updates";
// Connection state fields
private final String userVariablesQuery;
private final boolean canResetUserVariables;
private final int originalForeignKeyChecks;
private final int originalSqlSafeUpdates;
private final boolean awsRds; // Accessible via isAwsRds() getterUsage Examples:
// Connection is typically created by MySQLDatabase
MySQLDatabase database = new MySQLDatabase(config, jdbcFactory, interceptor);
MySQLConnection connection = database.getConnection();
// Check AWS RDS status
boolean isRds = connection.isAwsRds();
if (isRds) {
// Handle RDS-specific behavior
System.out.println("Connected to AWS RDS MySQL");
}
// Check named lock capability
boolean canUseLocks = connection.canUseNamedLockTemplate();
if (canUseLocks) {
// Named locks are available for concurrent operations
}The connection automatically manages and restores MySQL system variables:
Foreign Key Checks:
foreign_key_checks value on connectionSQL Safe Updates:
sql_safe_updates value on connectionFor supported MySQL/MariaDB versions and permissions:
MySQL (5.7+):
performance_schema.user_variables_by_threadMariaDB (10.2+):
information_schema.user_variablesRequirements:
// User variable reset capability is automatically detected
// and used when available during connection restorationThe connection detects AWS RDS environments using multiple indicators:
Detection Methods:
// AWS RDS detection example
if (connection.isAwsRds()) {
// AWS RDS specific handling:
// - Different privilege model
// - Specific feature limitations
// - Different monitoring approaches
}The connection performs capability detection during initialization:
User Variable Reset:
// Capability detection logic
if (database.isMariaDB() && !database.getVersion().isAtLeast("10.2")) {
// User variable reset disabled for older MariaDB
}
if (!database.isMariaDB() && !database.getVersion().isAtLeast("5.7")) {
// User variable reset disabled for older MySQL
}Event Scheduler Access:
// Event scheduler queryability detection
boolean canQueryEvents = database.eventSchedulerQueryable;
if (canQueryEvents) {
// Can query information_schema.events
} else {
// Event scheduler is OFF or DISABLED (MariaDB)
}Named locks provide coordination for concurrent Flyway operations:
/**
* Creates named lock template when supported
* @param jdbcTemplate JDBC template for operations
* @param discriminator Unique identifier for lock name
* @return MySQLNamedLockTemplate instance
*/
MySQLNamedLockTemplate lockTemplate = new MySQLNamedLockTemplate(jdbcTemplate, discriminator);
// Execute with lock protection
Object result = lockTemplate.execute(() -> {
// Critical section protected by named lock
return performCriticalOperation();
});Named locks are available when:
GET_LOCK() and RELEASE_LOCK() functionsConnections are configured with MySQL-specific properties:
// Set during connection establishment
props.put("connectionAttributes", "program_name:" + APPLICATION_NAME);This helps identify Flyway connections in:
SHOW PROCESSLIST)The connection implements automatic state restoration:
// Restoration happens automatically on connection close
@Override
protected void doRestoreOriginalState() throws SQLException {
// Reset user variables (if supported)
resetUserVariables();
// Restore system variables
jdbcTemplate.execute(
"SET foreign_key_checks=?, sql_safe_updates=?",
originalForeignKeyChecks,
originalSqlSafeUpdates
);
}The connection provides diagnostic information for troubleshooting:
Variable Access Issues:
// When user variable reset fails
LOG.debug("Disabled user variable reset as " + tableNamel + " cannot be queried " +
"(SQL State: " + e.getSQLState() + ", Error Code: " + e.getErrorCode() + ")");Version Compatibility:
// When features are disabled due to version
LOG.debug("Disabled user variable reset as it is only available from MySQL 5.7 onwards");
LOG.debug("Disabled user variable reset as it is only available from MariaDB 10.2 onwards");Permission Problems:
performance_schema.user_variables_by_thread (MySQL)information_schema.user_variables (MariaDB)GET_LOCK()/RELEASE_LOCK() permissionsVersion Issues:
Environment Issues:
The connection integrates with other database components:
Schema Operations:
MySQLSchema schema = connection.getSchema("myschema");
// Connection provides MySQL-specific schema behaviorTable Operations:
MySQLTable table = schema.getTable("mytable");
// Connection enables MySQL-specific table operationsTransaction Management:
// MySQL connections handle DDL auto-commit behavior
// No transaction support for DDL operations
boolean supportsDdl = connection.getDatabase().supportsDdlTransactions(); // falseInstall with Tessl CLI
npx tessl i tessl/maven-org-flywaydb--flyway-mysql