Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
import org.hibernate.stat.Statistics;
import org.hibernate.type.format.FormatMapper;
import org.hibernate.type.format.jackson.JacksonIntegration;
import org.hibernate.type.format.jaxb.JaxbXmlFormatMapper;

import jakarta.persistence.criteria.Nulls;
Expand Down Expand Up @@ -118,6 +117,7 @@
import static org.hibernate.type.format.jackson.JacksonIntegration.getJsonJacksonFormatMapperOrNull;
import static org.hibernate.type.format.jackson.JacksonIntegration.getOsonJacksonFormatMapperOrNull;
import static org.hibernate.type.format.jackson.JacksonIntegration.getXMLJacksonFormatMapperOrNull;
import static org.hibernate.type.format.jackson.JacksonIntegration.isJacksonOsonExtensionAvailable;
import static org.hibernate.type.format.jakartajson.JakartaJsonIntegration.getJakartaJsonBFormatMapperOrNull;

/**
Expand Down Expand Up @@ -309,13 +309,13 @@ public SessionFactoryOptionsBuilder(StandardServiceRegistry serviceRegistry, Boo
settings.get( AvailableSettings.JAKARTA_VALIDATION_FACTORY )
);

jsonFormatMapper = determineJsonFormatMapper(
jsonFormatMapper = jsonFormatMapper(
settings.get( AvailableSettings.JSON_FORMAT_MAPPER ),
!getBoolean( ORACLE_OSON_DISABLED ,settings),
strategySelector
);

xmlFormatMapper = determineXmlFormatMapper(
xmlFormatMapper = xmlFormatMapper(
settings.get( AvailableSettings.XML_FORMAT_MAPPER ),
strategySelector,
xmlFormatMapperLegacyFormatEnabled =
Expand Down Expand Up @@ -620,17 +620,20 @@ private SqmMultiTableMutationStrategy resolveSqmMutationStrategy(
strategyName,
(SqmMultiTableMutationStrategy) null,
strategyClass -> {
Constructor<SqmMultiTableMutationStrategy> dialectConstructor = null;
Constructor<SqmMultiTableMutationStrategy> emptyConstructor = null;
Constructor<? extends SqmMultiTableMutationStrategy> dialectConstructor = null;
Constructor<? extends SqmMultiTableMutationStrategy> emptyConstructor = null;
// todo (6.0) : formalize the allowed constructor parameterizations
for ( Constructor<?> declaredConstructor : strategyClass.getDeclaredConstructors() ) {
final Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
for ( var declaredConstructor : strategyClass.getDeclaredConstructors() ) {
final var parameterTypes = declaredConstructor.getParameterTypes();
final var constructor =
(Constructor<? extends SqmMultiTableMutationStrategy>)
declaredConstructor;
if ( parameterTypes.length == 1 && parameterTypes[0] == Dialect.class ) {
dialectConstructor = (Constructor<SqmMultiTableMutationStrategy>) declaredConstructor;
dialectConstructor = constructor;
break;
}
else if ( parameterTypes.length == 0 ) {
emptyConstructor = (Constructor<SqmMultiTableMutationStrategy>) declaredConstructor;
emptyConstructor = constructor;
}
}

Expand All @@ -646,11 +649,13 @@ else if ( emptyConstructor != null ) {
}
catch (Exception e) {
throw new StrategySelectionException(
String.format( "Could not instantiate named strategy class [%s]", strategyClass.getName() ),
"Could not instantiate named strategy class [" + strategyClass.getName() + "]",
e
);
}
throw new IllegalArgumentException( "Cannot instantiate the class [" + strategyClass.getName() + "] because it does not have a constructor that accepts a dialect or an empty constructor" );
throw new IllegalArgumentException( "Cannot instantiate the class ["
+ strategyClass.getName()
+ "] because it does not have a constructor that accepts a dialect or an empty constructor" );
}
);
}
Expand All @@ -669,17 +674,20 @@ private SqmMultiTableInsertStrategy resolveSqmInsertStrategy(
strategyName,
(SqmMultiTableInsertStrategy) null,
strategyClass -> {
Constructor<SqmMultiTableInsertStrategy> dialectConstructor = null;
Constructor<SqmMultiTableInsertStrategy> emptyConstructor = null;
Constructor<? extends SqmMultiTableInsertStrategy> dialectConstructor = null;
Constructor<? extends SqmMultiTableInsertStrategy> emptyConstructor = null;
// todo (6.0) : formalize the allowed constructor parameterizations
for ( Constructor<?> declaredConstructor : strategyClass.getDeclaredConstructors() ) {
final Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
for ( var declaredConstructor : strategyClass.getDeclaredConstructors() ) {
final var parameterTypes = declaredConstructor.getParameterTypes();
final var constructor =
(Constructor<? extends SqmMultiTableInsertStrategy>)
declaredConstructor;
if ( parameterTypes.length == 1 && parameterTypes[0] == Dialect.class ) {
dialectConstructor = (Constructor<SqmMultiTableInsertStrategy>) declaredConstructor;
dialectConstructor = constructor;
break;
}
else if ( parameterTypes.length == 0 ) {
emptyConstructor = (Constructor<SqmMultiTableInsertStrategy>) declaredConstructor;
emptyConstructor = constructor;
}
}

Expand All @@ -695,11 +703,13 @@ else if ( emptyConstructor != null ) {
}
catch (Exception e) {
throw new StrategySelectionException(
String.format( "Could not instantiate named strategy class [%s]", strategyClass.getName() ),
"Could not instantiate named strategy class [" + strategyClass.getName() + "]",
e
);
}
throw new IllegalArgumentException( "Cannot instantiate the class [" + strategyClass.getName() + "] because it does not have a constructor that accepts a dialect or an empty constructor" );
throw new IllegalArgumentException( "Cannot instantiate the class ["
+ strategyClass.getName()
+ "] because it does not have a constructor that accepts a dialect or an empty constructor" );
}
);
}
Expand All @@ -708,23 +718,15 @@ private HqlTranslator resolveHqlTranslator(
String producerName,
StandardServiceRegistry serviceRegistry,
StrategySelector strategySelector) {
if ( isEmpty( producerName ) ) {
return null;
}
else {
//noinspection Convert2Lambda
return strategySelector.resolveDefaultableStrategy(
HqlTranslator.class,
producerName,
new Callable<>() {
@Override
public HqlTranslator call() throws Exception {
return (HqlTranslator) serviceRegistry.requireService( ClassLoaderService.class )
.classForName( producerName ).newInstance();
}
}
);
}
return isEmpty( producerName )
? null
: strategySelector.<HqlTranslator>resolveDefaultableStrategy(
HqlTranslator.class,
producerName,
() -> (HqlTranslator)
serviceRegistry.requireService( ClassLoaderService.class )
.classForName( producerName ).newInstance()
);
}

private SqmTranslatorFactory resolveSqmTranslator(
Expand Down Expand Up @@ -754,8 +756,7 @@ private static Interceptor determineInterceptor(
private static Supplier<? extends Interceptor> determineStatelessInterceptor(
Map<String,Object> configurationSettings,
StrategySelector strategySelector) {
Object setting = configurationSettings.get( SESSION_SCOPED_INTERCEPTOR );

final Object setting = configurationSettings.get( SESSION_SCOPED_INTERCEPTOR );
if ( setting == null ) {
return null;
}
Expand All @@ -782,7 +783,7 @@ private static Supplier<? extends Interceptor> interceptorSupplier(Class<? exten
return clazz.newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
throw new HibernateException( "Could not supply session-scoped SessionFactory Interceptor", e );
throw new org.hibernate.InstantiationException( "Could not instantiate session-scoped Interceptor", clazz, e );
}
};
}
Expand All @@ -798,32 +799,41 @@ private PhysicalConnectionHandlingMode interpretConnectionHandlingMode(
.getDefaultConnectionHandlingMode();
}

private static FormatMapper determineJsonFormatMapper(Object setting, boolean osonExtensionEnabled, StrategySelector strategySelector) {
return strategySelector.resolveDefaultableStrategy(
FormatMapper.class,
private static FormatMapper jsonFormatMapper(Object setting, boolean osonExtensionEnabled, StrategySelector selector) {
return formatMapper(
setting,
(Callable<FormatMapper>) () -> {
selector,
() -> {
// Prefer the OSON Jackson FormatMapper by default if available
final FormatMapper jsonJacksonFormatMapper =
(osonExtensionEnabled && JacksonIntegration.isJacksonOsonExtensionAvailable())
osonExtensionEnabled && isJacksonOsonExtensionAvailable()
? getOsonJacksonFormatMapperOrNull()
: getJsonJacksonFormatMapperOrNull();
return jsonJacksonFormatMapper != null ? jsonJacksonFormatMapper : getJakartaJsonBFormatMapperOrNull();
return jsonJacksonFormatMapper != null
? jsonJacksonFormatMapper
: getJakartaJsonBFormatMapperOrNull();
}
);
}

private static FormatMapper determineXmlFormatMapper(Object setting, StrategySelector strategySelector, boolean legacyFormat) {
return strategySelector.resolveDefaultableStrategy(
FormatMapper.class,
private static FormatMapper xmlFormatMapper(Object setting, StrategySelector selector, boolean legacyFormat) {
return formatMapper(
setting,
(Callable<FormatMapper>) () -> {
final FormatMapper jacksonFormatMapper = getXMLJacksonFormatMapperOrNull( legacyFormat );
return jacksonFormatMapper != null ? jacksonFormatMapper : new JaxbXmlFormatMapper( legacyFormat );
selector,
() -> {
final FormatMapper jacksonFormatMapper =
getXMLJacksonFormatMapperOrNull( legacyFormat );
return jacksonFormatMapper != null
? jacksonFormatMapper
: new JaxbXmlFormatMapper( legacyFormat );
}
);
}

private static FormatMapper formatMapper(Object setting, StrategySelector selector, Callable<FormatMapper> defaultResolver) {
return selector.resolveDefaultableStrategy( FormatMapper.class, setting, defaultResolver );
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// SessionFactoryOptionsState
Expand Down Expand Up @@ -1601,8 +1611,8 @@ public void disableJtaTransactionAccess() {
}

public SessionFactoryOptions buildOptions() {
if ( jpaCompliance instanceof MutableJpaCompliance ) {
jpaCompliance = mutableJpaCompliance().immutableCopy();
if ( jpaCompliance instanceof MutableJpaCompliance mutableJpaCompliance ) {
jpaCompliance = mutableJpaCompliance.immutableCopy();
}
return this;
}
Expand Down Expand Up @@ -1692,7 +1702,7 @@ private Map<String, Object> initializeDefaultSessionProperties(ConfigurationServ
LegacySpecHints.HINT_JAVAEE_QUERY_TIMEOUT
};

final Map<String, Object> configurationServiceSettings = configurationService.getSettings();
final var configurationServiceSettings = configurationService.getSettings();
for ( String key : ENTITY_MANAGER_SPECIFIC_PROPERTIES ) {
if ( configurationServiceSettings.containsKey( key ) ) {
settings.put( key, configurationServiceSettings.get( key ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ public static Integer getIsolation(DataSource dataSource) {
}
}

static Integer getFetchSize(ConnectionCreator creator) {
try ( var conn = creator.createConnection() ) {
try ( var statement = conn.createStatement() ) {
return statement.getFetchSize();
}
}
catch ( SQLException ignored ) {
return null;
}
}

static Integer getIsolation(ConnectionCreator creator) {
try ( var conn = creator.createConnection() ) {
return conn.getTransactionIsolation();
}
catch ( SQLException ignored ) {
return null;
}
}

@Override
public String getJdbcUrl() {
return jdbcUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand Down Expand Up @@ -40,6 +39,8 @@
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.extractIsolation;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.getConnectionProperties;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName;
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getFetchSize;
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getIsolation;
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
import static org.hibernate.internal.util.config.ConfigurationHelper.getInt;
import static org.hibernate.internal.util.config.ConfigurationHelper.getLong;
Expand Down Expand Up @@ -142,43 +143,49 @@ private static ConnectionCreator buildCreator(
final Integer isolation = extractIsolation( configurationValues );
final String initSql = (String) configurationValues.get( INIT_SQL );

final ConnectionCreatorFactory factory = getConnectionCreatorFactory( configurationValues, serviceRegistry );
final var connectionCreator =
getConnectionCreatorFactory( configurationValues, serviceRegistry )
.create(
driver,
serviceRegistry,
url,
connectionProps,
autoCommit,
isolation,
initSql,
configurationValues
);

;

dbInfo = new DatabaseConnectionInfoImpl(
DriverManagerConnectionProviderImpl.class,
url,
driverList,
SimpleDatabaseVersion.ZERO_VERSION,
Boolean.toString( autoCommit ),
isolation != null ? toIsolationNiceName( isolation ) : null,
isolation != null
? toIsolationNiceName( isolation )
: toIsolationNiceName( getIsolation( connectionCreator ) ),
getInt( MIN_SIZE, configurationValues, 1 ),
getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 ),
null
getFetchSize( connectionCreator )
);

return factory.create(
driver,
serviceRegistry,
url,
connectionProps,
autoCommit,
isolation,
initSql,
configurationValues
);
return connectionCreator;
}

private static String driverList() {
//we're hoping that the driver is already loaded
ConnectionInfoLogger.INSTANCE.jdbcDriverNotSpecified();
final StringBuilder list = new StringBuilder();
final Enumeration<Driver> drivers = DriverManager.getDrivers();
while ( drivers.hasMoreElements() ) {
if ( !list.isEmpty() ) {
list.append(", ");
}
list.append( drivers.nextElement().getClass().getName() );
}
final var list = new StringBuilder();
DriverManager.drivers()
.forEach( driver -> {
if ( !list.isEmpty() ) {
list.append( ", " );
}
list.append( driver.getClass().getName() );
} );
return list.toString();
}

Expand Down