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 @@ -15,9 +15,9 @@
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.jboss.logging.Logger;

import static org.hibernate.cfg.DialectSpecificSettings.HANA_MAX_LOB_PREFETCH_SIZE;
import static org.hibernate.internal.CoreMessageLogger.CORE_LOGGER;

/**
* Utility class that extracts some initial configuration from the database for {@link HANADialect}.
Expand Down Expand Up @@ -58,9 +58,8 @@ public static HANAServerConfiguration fromDialectResolutionInfo(DialectResolutio
}
catch (SQLException e) {
// Ignore
CORE_LOGGER.debug(
"An error occurred while trying to determine the database version.",
e );
Logger.getLogger( HANAServerConfiguration.class )
.debug( "An error occurred while trying to determine the database version.", e );
}

if (databaseMajorVersion > 0 && databaseMajorVersion < 4) {
Expand All @@ -75,9 +74,8 @@ public static HANAServerConfiguration fromDialectResolutionInfo(DialectResolutio
}
catch (SQLException e) {
// Ignore
CORE_LOGGER.debug(
"An error occurred while trying to determine the value of the HANA parameter indexserver.ini / session / max_lob_prefetch_size.",
e );
Logger.getLogger( HANAServerConfiguration.class )
.debug( "An error occurred while trying to determine the value of the HANA parameter indexserver.ini / session / max_lob_prefetch_size", e );
}
}
else {
Expand Down Expand Up @@ -113,7 +111,8 @@ public static DatabaseVersion determineDatabaseVersion(DialectResolutionInfo inf
}
catch (SQLException e) {
// Ignore
CORE_LOGGER.debug( "An error occurred while trying to determine the HANA Cloud version.", e );
Logger.getLogger( HANAServerConfiguration.class )
.debug( "An error occurred while trying to determine the HANA Cloud version.", e );
}
}
return databaseVersion == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.function.Function;

import org.hibernate.engine.jdbc.internal.FormatStyle;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.jdbc.AbstractReturningWork;
Expand Down Expand Up @@ -59,28 +56,25 @@ public TemporaryTableCreationWork(

@Override
public Boolean execute(Connection connection) {
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();

final var jdbcServices = sessionFactory.getJdbcServices();
try {
final String creationCommand = exporter.getSqlCreateCommand( temporaryTable );
logStatement( creationCommand, jdbcServices );

try (Statement statement = connection.createStatement()) {
try ( var statement = connection.createStatement() ) {
statement.executeUpdate( creationCommand );
jdbcServices.getSqlExceptionHelper().handleAndClearWarnings( statement, WARNING_HANDLER );
return Boolean.TRUE;
}
catch (SQLException e) {
CORE_LOGGER.debugf(
"Unable to create temporary table [%s]; `%s` failed : %s",
CORE_LOGGER.unableToCreateTempTable(
temporaryTable.getQualifiedTableName(),
creationCommand,
e.getMessage()
e
);
}
}
catch( Exception e ) {
CORE_LOGGER.debugf( "Error creating temporary table(s) : %s", e.getMessage() );
CORE_LOGGER.errorCreatingTempTable( e );
}
return Boolean.FALSE;
}
Expand Down Expand Up @@ -116,27 +110,25 @@ public TemporaryTableDropWork(

@Override
public void execute(Connection connection) {
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();

final var jdbcServices = sessionFactory.getJdbcServices();
try {
final String dropCommand = exporter.getSqlDropCommand( temporaryTable );
logStatement( dropCommand, jdbcServices );

try (Statement statement = connection.createStatement()) {
try ( var statement = connection.createStatement() ) {
statement.executeUpdate( dropCommand );
jdbcServices.getSqlExceptionHelper().handleAndClearWarnings( statement, WARNING_HANDLER );
jdbcServices.getSqlExceptionHelper()
.handleAndClearWarnings( statement, WARNING_HANDLER );
}
catch (SQLException e) {
CORE_LOGGER.debugf(
"Unable to drop temporary table [%s]; `%s` failed : %s",
CORE_LOGGER.unableToDropTempTable(
temporaryTable.getQualifiedTableName(),
dropCommand,
e.getMessage()
e
);
}
}
catch( Exception e ) {
CORE_LOGGER.debugf( "Error dropping temporary table(s) : %s", e.getMessage() );
CORE_LOGGER.errorDroppingTempTable( e );
}
}
}
Expand All @@ -150,7 +142,7 @@ public static void cleanTemporaryTableRows(
TemporaryTableExporter exporter,
Function<SharedSessionContractImplementor,String> sessionUidAccess,
SharedSessionContractImplementor session) {
final JdbcCoordinator jdbcCoordinator = session.getJdbcCoordinator();
final var jdbcCoordinator = session.getJdbcCoordinator();
PreparedStatement preparedStatement = null;
try {
final String sql = exporter.getSqlTruncateCommand( temporaryTable, sessionUidAccess, session );
Expand Down Expand Up @@ -201,7 +193,7 @@ protected void logWarning(String description, String message) {


private static void logStatement(String sql, JdbcServices jdbcServices) {
final SqlStatementLogger statementLogger = jdbcServices.getSqlStatementLogger();
statementLogger.logStatement( sql, FormatStyle.BASIC.getFormatter() );
jdbcServices.getSqlStatementLogger()
.logStatement( sql, FormatStyle.BASIC.getFormatter() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
.classForName( candidate.toString() );
}
catch ( ClassLoadingException e ) {
CORE_LOGGER.debugf( "Unable to locate %s implementation class %s", expected.getName(), candidate.toString() );
CORE_LOGGER.debugf( "Unable to locate %s implementation class %s",
expected.getName(), candidate.toString() );
target = null;
}
}
Expand All @@ -95,10 +96,8 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
return target.newInstance();
}
catch ( Exception e ) {
CORE_LOGGER.debugf(
"Unable to instantiate %s class %s", expected.getName(),
target.getName()
);
CORE_LOGGER.debugf( "Unable to instantiate %s class %s",
expected.getName(), target.getName() );
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.CascadingAction;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.Status;
import org.hibernate.event.spi.DeleteContext;
import org.hibernate.event.spi.EventSource;
Expand Down Expand Up @@ -82,7 +79,7 @@ public static <T> void cascade(
if ( action.anythingToCascade( persister ) ) { // performance opt
final boolean traceEnabled = CORE_LOGGER.isTraceEnabled();
if ( traceEnabled ) {
CORE_LOGGER.tracev( "Processing cascade {0} for: {1}", action, persister.getEntityName() );
CORE_LOGGER.processingCascade( action, persister.getEntityName() );
}
final var bytecodeEnhancement = persister.getBytecodeEnhancementMetadata();
final EntityEntry entry;
Expand All @@ -100,11 +97,11 @@ public static <T> void cascade(

final Type[] types = persister.getPropertyTypes();
final String[] propertyNames = persister.getPropertyNames();
final CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
final var cascadeStyles = persister.getPropertyCascadeStyles();
final boolean hasUninitializedLazyProperties = bytecodeEnhancement.hasUnFetchedAttributes( parent );

for ( int i = 0; i < types.length; i++) {
final CascadeStyle style = cascadeStyles[ i ];
final var style = cascadeStyles[ i ];
final String propertyName = propertyNames[ i ];
final Type type = types[i];
final boolean isUninitializedProperty =
Expand Down Expand Up @@ -192,7 +189,7 @@ && isLogicalOneToOne( type ) ) {
}

if ( traceEnabled ) {
CORE_LOGGER.tracev( "Done processing cascade {0} for: {1}", action, persister.getEntityName() );
CORE_LOGGER.doneProcessingCascade( action, persister.getEntityName() );
}
}
}
Expand Down Expand Up @@ -288,8 +285,8 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
if ( style.hasOrphanDelete() && action.deleteOrphans() ) {
// value is orphaned if loaded state for this property shows not null
// because it is currently null.
final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
final EntityEntry entry = persistenceContext.getEntry( parent );
final var persistenceContext = eventSource.getPersistenceContextInternal();
final var entry = persistenceContext.getEntry( parent );
if ( entry != null && entry.getStatus() != Status.SAVING ) {
Object loadedValue;
if ( componentPath == null ) {
Expand Down Expand Up @@ -324,13 +321,11 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
// entity is managed (without first nulling and manually flushing).
if ( child == null || loadedValue != null && child != loadedValue ) {
EntityEntry valueEntry = persistenceContext.getEntry( loadedValue );

if ( valueEntry == null && isHibernateProxy( loadedValue ) ) {
// un-proxy and re-associate for cascade operation
// useful for @OneToOne defined as FetchType.LAZY
loadedValue = persistenceContext.unproxyAndReassociate( loadedValue );
valueEntry = persistenceContext.getEntry( loadedValue );

// HHH-11965
// Should the unwrapped proxy value be equal via reference to the entity's property value
// provided by the 'child' variable, we should not trigger the orphan removal of the
Expand All @@ -342,15 +337,12 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
}

if ( valueEntry != null ) {
final EntityPersister persister = valueEntry.getPersister();
final var persister = valueEntry.getPersister();
final String entityName = persister.getEntityName();
if ( CORE_LOGGER.isTraceEnabled() ) {
CORE_LOGGER.tracev(
"Deleting orphaned entity instance: {0}",
infoString( entityName, persister.getIdentifier( loadedValue, eventSource ) )
);
CORE_LOGGER.deletingOrphan(
infoString( entityName, persister.getIdentifier( loadedValue, eventSource ) ) );
}

if ( isForeignKeyToParent( type ) ) {
// If FK direction is to-parent, we must remove the orphan *before* the queued update(s)
// occur. Otherwise, replacing the association on a managed entity, without manually
Expand Down Expand Up @@ -487,10 +479,10 @@ private static <T> void cascadeCollection(
final CascadeStyle style,
final T anything,
final CollectionType type) {
final CollectionPersister persister =
final var persister =
eventSource.getFactory().getMappingMetamodel()
.getCollectionDescriptor( type.getRole() );
final Type elemType = persister.getElementType();
final var elemType = persister.getElementType();
//cascade to current collection elements
if ( elemType instanceof EntityType || elemType instanceof AnyType || elemType instanceof ComponentType ) {
cascadeCollectionElements(
Expand Down Expand Up @@ -530,15 +522,18 @@ private static <T> void cascadeToOne(
final List<String> componentPath) {
if ( style.reallyDoCascade( action ) ) {
//not really necessary, but good for consistency...
final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
final var persistenceContext = eventSource.getPersistenceContextInternal();
persistenceContext.addChildParent( child, parent );
final String childEntityName =
type instanceof EntityType entityType
? entityType.getAssociatedEntityName()
: null;
CORE_LOGGER.cascading( action, childEntityName );
try {
action.cascade(
eventSource,
child,
type instanceof EntityType entityType
? entityType.getAssociatedEntityName()
: null,
childEntityName,
parentEntityName,
propertyName,
componentPath,
Expand Down Expand Up @@ -572,14 +567,12 @@ private static <T> void cascadeCollectionElements(

final boolean reallyDoCascade = style.reallyDoCascade( action )
&& child != CollectionType.UNFETCHED_COLLECTION;

if ( reallyDoCascade ) {
final boolean traceEnabled = CORE_LOGGER.isTraceEnabled();
if ( traceEnabled ) {
CORE_LOGGER.tracev( "Cascade {0} for collection: {1}", action, collectionType.getRole() );
CORE_LOGGER.cascadingCollection( action, collectionType.getRole() );
}

final Iterator<?> iterator = action.getCascadableChildrenIterator( eventSource, collectionType, child );
final var iterator = action.getCascadableChildrenIterator( eventSource, collectionType, child );
while ( iterator.hasNext() ) {
cascadeProperty(
action,
Expand All @@ -597,14 +590,13 @@ private static <T> void cascadeCollectionElements(
isCascadeDeleteEnabled
);
}

if ( traceEnabled ) {
CORE_LOGGER.tracev( "Done cascade {0} for collection: {1}", action, collectionType.getRole() );
CORE_LOGGER.doneCascadingCollection( action, collectionType.getRole() );
}
}

// a newly instantiated collection can't have orphans
final PersistentCollection<?> persistentCollection =
final var persistentCollection =
child instanceof PersistentCollection<?> collection
? collection
: eventSource.getPersistenceContextInternal()
Expand All @@ -620,42 +612,46 @@ private static <T> void cascadeCollectionElements(
if ( deleteOrphans ) {
final boolean traceEnabled = CORE_LOGGER.isTraceEnabled();
if ( traceEnabled ) {
CORE_LOGGER.tracev( "Deleting orphans for collection: {0}", collectionType.getRole() );
CORE_LOGGER.deletingOrphans( collectionType.getRole() );
}
// we can do the cast since orphan-delete does not apply to:
// 1. newly instantiated collections
// 2. arrays (we can't track orphans for detached arrays)
final String elementEntityName = collectionType.getAssociatedEntityName( eventSource.getFactory() );
deleteOrphans( eventSource, elementEntityName, persistentCollection );

if ( traceEnabled ) {
CORE_LOGGER.tracev( "Done deleting orphans for collection: {0}", collectionType.getRole() );
CORE_LOGGER.doneDeletingOrphans( collectionType.getRole() );
}
}
}

/**
* Delete any entities that were removed from the collection
*/
private static void deleteOrphans(EventSource eventSource, String entityName, PersistentCollection<?> pc) {
private static void deleteOrphans(EventSource eventSource, String entityName, PersistentCollection<?> collection) {
//TODO: suck this logic into the collection!
final Collection<?> orphans;
if ( pc.wasInitialized() ) {
final CollectionEntry entry = eventSource.getPersistenceContextInternal().getCollectionEntry( pc );
orphans = entry == null ? EMPTY_LIST : entry.getOrphans( entityName, pc );
}
else {
orphans = pc.getQueuedOrphans( entityName );
}

for ( Object orphan : orphans ) {
for ( Object orphan : getOrphans( eventSource, entityName, collection ) ) {
if ( orphan != null ) {
CORE_LOGGER.tracev( "Deleting orphaned entity instance: {0}", entityName );
CORE_LOGGER.deletingOrphanOfType( entityName );
eventSource.delete( entityName, orphan, false, DeleteContext.create() );
}
}
}

private static Collection<?> getOrphans(EventSource eventSource, String entityName, PersistentCollection<?> collection) {
if ( collection.wasInitialized() ) {
final var collectionEntry =
eventSource.getPersistenceContextInternal()
.getCollectionEntry( collection );
return collectionEntry == null
? EMPTY_LIST
: collectionEntry.getOrphans( entityName, collection );
}
else {
return collection.getQueuedOrphans( entityName );
}
}

private static <T> boolean cascadeDeleteEnabled(CascadingAction<T> action, CollectionPersister persister) {
return action.directionAffectedByCascadeDelete() == ForeignKeyDirection.FROM_PARENT
&& persister.isCascadeDeleteEnabled();
Expand Down
Loading