Skip to content

Commit c864574

Browse files
dreab8Dan Kristensen
authored andcommitted
HHH-19752 Allow setting MariaDB/MySQL storage engine not only using System properties but also configuration properties
1 parent 60b7996 commit c864574

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/SchemaToolingSettings.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,10 @@ public interface SchemaToolingSettings {
296296

297297
/**
298298
* Specifies the default storage engine for a relational database that supports
299-
* multiple storage engines. This property must be set either as an {@link Environment}
300-
* variable or JVM System Property, since the {@link org.hibernate.dialect.Dialect} is
301-
* instantiated before Hibernate property resolution.
299+
* multiple storage engines.
300+
*
301+
* This property can be set as an {@link Environment} variable, a JVM System Property
302+
* or a configuration property.
302303
* <p>
303304
* For MySQL, the legal values are {@code innodb} (the default) and {@code myisam}.
304305
*

hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import java.time.temporal.TemporalAccessor;
9494
import java.util.Calendar;
9595
import java.util.Date;
96+
import java.util.Locale;
9697
import java.util.TimeZone;
9798

9899
import static java.lang.Integer.parseInt;
@@ -146,7 +147,7 @@ public class MySQLDialect extends Dialect {
146147

147148
private static final DatabaseVersion MINIMUM_VERSION = DatabaseVersion.make( 8 );
148149

149-
private final MySQLStorageEngine storageEngine = createStorageEngine();
150+
private final MySQLStorageEngine storageEngine;
150151

151152
private final SizeStrategy sizeStrategy = new SizeStrategyImpl() {
152153
@Override
@@ -207,14 +208,19 @@ public MySQLDialect(DatabaseVersion version, int bytesPerCharacter) {
207208
}
208209

209210
public MySQLDialect(DatabaseVersion version, MySQLServerConfiguration serverConfiguration) {
210-
this( version, serverConfiguration.getBytesPerCharacter(), serverConfiguration.isNoBackslashEscapesEnabled() );
211+
super( version );
212+
maxVarcharLength = maxVarcharLength( getMySQLVersion(), serverConfiguration.getBytesPerCharacter() ); //conservative assumption
213+
maxVarbinaryLength = maxVarbinaryLength( getMySQLVersion() );
214+
noBackslashEscapesEnabled = serverConfiguration.isNoBackslashEscapesEnabled();
215+
storageEngine = createStorageEngine( serverConfiguration.getConfiguredStorageEngine() );
211216
}
212217

213218
public MySQLDialect(DatabaseVersion version, int bytesPerCharacter, boolean noBackslashEscapes) {
214219
super( version );
215220
maxVarcharLength = maxVarcharLength( getMySQLVersion(), bytesPerCharacter ); //conservative assumption
216221
maxVarbinaryLength = maxVarbinaryLength( getMySQLVersion() );
217222
noBackslashEscapesEnabled = noBackslashEscapes;
223+
storageEngine = createStorageEngine( Environment.getProperties().getProperty( AvailableSettings.STORAGE_ENGINE ) );
218224
}
219225

220226
public MySQLDialect(DialectResolutionInfo info) {
@@ -257,13 +263,10 @@ protected void initDefaultProperties() {
257263
getDefaultProperties().setProperty( FetchSettings.MAX_FETCH_DEPTH, "2" );
258264
}
259265

260-
private MySQLStorageEngine createStorageEngine() {
261-
final String storageEngine =
262-
Environment.getProperties()
263-
.getProperty( AvailableSettings.STORAGE_ENGINE );
264-
return storageEngine == null
266+
private MySQLStorageEngine createStorageEngine(String configuredStorageEngine) {
267+
return configuredStorageEngine == null
265268
? getDefaultMySQLStorageEngine()
266-
: switch ( storageEngine ) {
269+
: switch ( configuredStorageEngine.toLowerCase(Locale.ROOT) ) {
267270
case "innodb" -> InnoDBStorageEngine.INSTANCE;
268271
case "myisam" -> MyISAMStorageEngine.INSTANCE;
269272
default -> throw new UnsupportedOperationException(

hibernate-core/src/main/java/org/hibernate/dialect/MySQLServerConfiguration.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.sql.SQLException;
1010

1111
import org.hibernate.Internal;
12+
import org.hibernate.cfg.AvailableSettings;
1213
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
14+
import org.hibernate.internal.util.config.ConfigurationHelper;
1315

1416
import static org.hibernate.cfg.DialectSpecificSettings.MYSQL_BYTES_PER_CHARACTER;
1517
import static org.hibernate.cfg.DialectSpecificSettings.MYSQL_NO_BACKSLASH_ESCAPES;
@@ -26,10 +28,18 @@
2628
public class MySQLServerConfiguration {
2729
private final int bytesPerCharacter;
2830
private final boolean noBackslashEscapesEnabled;
31+
private final String storageEngine;
2932

3033
public MySQLServerConfiguration(int bytesPerCharacter, boolean noBackslashEscapesEnabled) {
3134
this.bytesPerCharacter = bytesPerCharacter;
3235
this.noBackslashEscapesEnabled = noBackslashEscapesEnabled;
36+
this.storageEngine = null;
37+
}
38+
39+
public MySQLServerConfiguration(int bytesPerCharacter, boolean noBackslashEscapesEnabled, String storageEngine) {
40+
this.bytesPerCharacter = bytesPerCharacter;
41+
this.noBackslashEscapesEnabled = noBackslashEscapesEnabled;
42+
this.storageEngine = storageEngine;
3343
}
3444

3545
public int getBytesPerCharacter() {
@@ -40,6 +50,10 @@ public boolean isNoBackslashEscapesEnabled() {
4050
return noBackslashEscapesEnabled;
4151
}
4252

53+
public String getConfiguredStorageEngine() {
54+
return storageEngine;
55+
}
56+
4357
static Integer getBytesPerCharacter(String characterSet) {
4458
final int collationIndex = characterSet.indexOf( '_' );
4559
// According to https://dev.mysql.com/doc/refman/8.0/en/charset-charsets.html
@@ -78,7 +92,11 @@ public static MySQLServerConfiguration fromDialectResolutionInfo(DialectResoluti
7892
if ( noBackslashEscapes == null ) {
7993
noBackslashEscapes = getBoolean( MYSQL_NO_BACKSLASH_ESCAPES, info.getConfigurationValues() );
8094
}
81-
return new MySQLServerConfiguration( bytesPerCharacter, noBackslashEscapes );
95+
96+
return new MySQLServerConfiguration(
97+
bytesPerCharacter,
98+
noBackslashEscapes,
99+
ConfigurationHelper.getString( AvailableSettings.STORAGE_ENGINE, info.getConfigurationValues() ) );
82100
}
83101

84102
/**

0 commit comments

Comments
 (0)