Skip to content

various improvements #10605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 23, 2025
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 @@ -54,8 +54,8 @@
* Certain rules applying to stateful sessions are relaxed in a stateless
* session:
* <ul>
* <li>it is not necessary to discard a session and its entities after an
* exception is thrown by a stateless sessions, and
* <li>it's not necessary to discard a stateless session and its entities
* after an exception is thrown by the stateless session, and
* <li>when an exception is thrown by a stateless session, the current
* transaction is not automatically marked for rollback.
* </ul>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Iterator;
import java.util.LinkedHashSet;

import org.hibernate.internal.util.ExceptionHelper;

public class AggregatedClassLoader extends ClassLoader {
private final ClassLoader[] individualClassLoaders;
Expand Down Expand Up @@ -122,7 +121,7 @@ else if ( !sysCLReturned && systemClassLoader != null ) {

private Iterator<ClassLoader> newTcclNeverIterator() {
final ClassLoader systemClassLoader = locateSystemClassLoader();
return new Iterator<ClassLoader>() {
return new Iterator<>() {
private int currentIndex = 0;
private boolean sysCLReturned = false;

Expand Down Expand Up @@ -182,7 +181,7 @@ public URL nextElement() {

@Override
protected URL findResource(String name) {
final Iterator<ClassLoader> clIterator = newClassLoaderIterator();
final var clIterator = newClassLoaderIterator();
while ( clIterator.hasNext() ) {
final ClassLoader classLoader = clIterator.next();
final URL resource = classLoader.getResource( name );
Expand All @@ -195,22 +194,18 @@ protected URL findResource(String name) {

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
final Iterator<ClassLoader> clIterator = newClassLoaderIterator();
Throwable t = null;
final var clIterator = newClassLoaderIterator();
final var exception = new ClassNotFoundException( "Could not load requested class: " + name );
while ( clIterator.hasNext() ) {
final ClassLoader classLoader = clIterator.next();
try {
return classLoader.loadClass( name );
}
catch (Exception ex) {
ExceptionHelper.combine( ( t == null ? t = new Throwable() : t ), ex );
}
catch (LinkageError le) {
ExceptionHelper.combine( ( t == null ? t = new Throwable() : t ), le );
catch (Exception|LinkageError ex) {
exception.addSuppressed( ex );
}
}

throw new ClassNotFoundException( "Could not load requested class : " + name, ( t != null && t.getSuppressed().length > 0 ? t : null ) );
throw exception;
}

private static ClassLoader locateSystemClassLoader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public int getSize() {
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
if ( entry == null ) {
throwLazyInitializationExceptionIfNotConnected();
throwLazyInitializationException("collection not associated with session");
throw new AssertionFailure("impossible");
throwLazyInitializationException( "collection not associated with session" );
throw new AssertionFailure( "impossible" );
}
else {
if ( hasQueuedOperations() ) {
Expand Down Expand Up @@ -233,23 +233,23 @@ private <T> T withTemporarySessionIfNeeded(LazyInitializationWork<T> lazyInitial
tempSession = openTemporarySessionForLoading();
}
else {
throwLazyInitializationException( "could not initialize proxy - no Session" );
throwLazyInitializationException( "no session" );
}
}
else if ( !session.isOpenOrWaitingForAutoClose() ) {
if ( allowLoadOutsideTransaction ) {
tempSession = openTemporarySessionForLoading();
}
else {
throwLazyInitializationException( "could not initialize proxy - the owning Session was closed" );
throwLazyInitializationException( "the owning session was closed" );
}
}
else if ( !session.isConnected() ) {
if ( allowLoadOutsideTransaction ) {
tempSession = openTemporarySessionForLoading();
}
else {
throwLazyInitializationException( "could not initialize proxy - the owning Session is disconnected" );
throwLazyInitializationException( "the owning session is disconnected" );
}
}

Expand All @@ -273,8 +273,7 @@ else if ( !session.isConnected() ) {
}

final CollectionPersister collectionDescriptor =
session.getSessionFactory()
.getMappingMetamodel()
session.getSessionFactory().getMappingMetamodel()
.getCollectionDescriptor( getRole() );
session.getPersistenceContextInternal()
.addUninitializedDetachedCollection( collectionDescriptor, this );
Expand All @@ -301,20 +300,22 @@ else if ( !session.isConnected() ) {
else {
// Whenever the collection lazy loading is triggered during the loading process,
// closing the connection will cause an error when RowProcessingStateStandardImpl#next() will be called.
final PersistenceContext persistenceContext = session.getPersistenceContext();
if ( !session.isTransactionInProgress()
&& ( !persistenceContext.hasLoadContext()
|| persistenceContext.hasLoadContext()
&& persistenceContext.getLoadContexts().isLoadingFinished() ) ) {
if ( !session.isTransactionInProgress() && !unfinishedLoading() ) {
session.getJdbcCoordinator().afterTransaction();
}
}
}
}

private boolean unfinishedLoading() {
final PersistenceContext persistenceContext = session.getPersistenceContext();
return persistenceContext.hasLoadContext()
&& !persistenceContext.getLoadContexts().isLoadingFinished();
}

private SharedSessionContractImplementor openTemporarySessionForLoading() {
if ( sessionFactoryUuid == null ) {
throwLazyInitializationException( "SessionFactory UUID not known to create temporary Session for loading" );
throwLazyInitializationException( "SessionFactory UUID not known; cannot create temporary session for loading" );
}

final SessionImplementor session =
Expand Down Expand Up @@ -373,8 +374,8 @@ public boolean elementExists(Object element) {
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
if ( entry == null ) {
throwLazyInitializationExceptionIfNotConnected();
throwLazyInitializationException("collection not associated with session");
throw new AssertionFailure("impossible");
throwLazyInitializationException( "collection not associated with session" );
throw new AssertionFailure( "impossible" );
}
else {
if ( hasQueuedOperations() ) {
Expand Down Expand Up @@ -427,8 +428,8 @@ public Object elementByIndex(Object index) {
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
if ( entry == null ) {
throwLazyInitializationExceptionIfNotConnected();
throwLazyInitializationException("collection not associated with session");
throw new AssertionFailure("impossible");
throwLazyInitializationException( "collection not associated with session" );
throw new AssertionFailure( "impossible" );
}
else {
if ( hasQueuedOperations() ) {
Expand Down Expand Up @@ -646,9 +647,8 @@ private void throwLazyInitializationExceptionIfNotConnected() {

private void throwLazyInitializationException(String message) {
throw new LazyInitializationException(
"failed to lazily initialize a collection" +
(role == null ? "" : " of role: " + role) +
": " + message
String.format( "Cannot lazily initialize collection%s (%s)",
role == null ? "" : " of role '" + role + "'", message )
);
}

Expand Down Expand Up @@ -1305,11 +1305,7 @@ protected static <E> Collection<E> getOrphans(
currentSaving.add( current );
}
else {
final Object currentId = getEntityIdentifierIfNotUnsaved(
entityName,
current,
session
);
final Object currentId = getEntityIdentifierIfNotUnsaved( entityName, current, session );
currentIds.add( useIdDirect ? currentId : new TypedValue( idType, currentId ) );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ private void firePersist(final PersistEvent event) {
}
}
if ( originalException != null ) {
ExceptionHelper.doThrow( originalException );
ExceptionHelper.rethrow( originalException );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,28 @@ private ExceptionHelper() {
}

/**
* Throws the given throwable even if it is a checked exception.
* Throws the given {@link Throwable}, even if it's a checked exception.
*
* @param e The throwable to throw.
* @param throwable The {@code Throwable} to throw.
*/
public static void doThrow(Throwable e) {
ExceptionHelper.doThrow0(e);
public static void rethrow(Throwable throwable) {
sneakyThrow( throwable );
}

public static Throwable getRootCause(Throwable error) {
Throwable toProcess = error;
while ( toProcess.getCause() != null ) {
toProcess = toProcess.getCause();
}
return toProcess;
@SuppressWarnings("unchecked")
private static <T extends Throwable> void sneakyThrow(Throwable e)
throws T {
throw (T) e;
}

public static <T extends Throwable> T combine(T throwable, T otherThrowable) {
T toThrow = throwable;
if ( otherThrowable != null ) {
if ( toThrow != null ) {
toThrow.addSuppressed( otherThrowable );
}
else {
toThrow = otherThrowable;
public static Throwable getRootCause(Throwable error) {
var next = error;
while ( true ) {
final var current = next;
next = current.getCause();
if ( next == null ) {
return current;
}
}
return toThrow;
}

@SuppressWarnings("unchecked")
private static <T extends Throwable> void doThrow0(Throwable e) throws T {
throw (T) e;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void release() {
}
}
if ( originalException != null ) {
ExceptionHelper.doThrow( originalException );
ExceptionHelper.rethrow( originalException );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ public class CustomBeforeCompletionTest extends BaseCoreFunctionalTestCase {
public void success() {
inSession( session -> {
AtomicBoolean called = new AtomicBoolean( false );
session.getActionQueue().registerProcess( new BeforeTransactionCompletionProcess() {
@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
called.set( true );
}
} );
session.getActionQueue().registerProcess( s -> called.set( true ) );
Assert.assertFalse( called.get() );
inTransaction( session, theSession -> {
theSession.persist( new SimpleEntity( "jack" ) );
Expand Down
Loading