Skip to content

Commit 18c54b2

Browse files
committed
[#2768] Add exception for the execution on the wrong context
For now we use it only when the connection is used
1 parent 8bedb0b commit 18c54b2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/common/InternalStateAssertions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
import java.lang.invoke.MethodHandles;
99
import java.util.Locale;
10+
import java.util.Objects;
1011

1112
import org.hibernate.reactive.logging.impl.Log;
1213
import org.hibernate.reactive.logging.impl.LoggerFactory;
1314

1415
import io.vertx.core.Context;
16+
import io.vertx.core.internal.ContextInternal;
1517

1618
/**
1719
* Commonly used assertions to verify that the operations
@@ -52,4 +54,13 @@ public static void assertCurrentThreadMatches(Thread expectedThread) {
5254
}
5355
}
5456

57+
public static void assertCurrentContextMatches(Object object, ContextInternal expectedContext) {
58+
if ( ENFORCE ) {
59+
final ContextInternal currentContext = ContextInternal.current();
60+
Objects.requireNonNull( currentContext, "Current context cannot be null" );
61+
if ( !currentContext.equals( expectedContext ) ) {
62+
throw LOG.unexpectedContextDetected( object, expectedContext, currentContext );
63+
}
64+
}
65+
}
5566
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/logging/impl/Log.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
package org.hibernate.reactive.logging.impl;
77

88

9-
109
import java.sql.SQLException;
1110
import java.sql.SQLWarning;
1211

13-
import jakarta.persistence.PersistenceException;
14-
1512
import org.hibernate.HibernateException;
1613
import org.hibernate.JDBCException;
1714
import org.hibernate.LazyInitializationException;
@@ -26,6 +23,9 @@
2623
import org.jboss.logging.annotations.Message;
2724
import org.jboss.logging.annotations.MessageLogger;
2825

26+
import io.vertx.core.internal.ContextInternal;
27+
import jakarta.persistence.PersistenceException;
28+
2929
import static org.jboss.logging.Logger.Level.DEBUG;
3030
import static org.jboss.logging.Logger.Level.ERROR;
3131
import static org.jboss.logging.Logger.Level.INFO;
@@ -277,6 +277,9 @@ public interface Log extends BasicLogger {
277277
@Message(id = 87, value = "Retrieved key was null, but to-one is not nullable : %s")
278278
IllegalStateException notNullableToOneAssociationMissingKey(String toOneNavigablePath);
279279

280+
@Message(id = 88, value = "Expected to use the object %1$s on context %2$s but was %3$s")
281+
HibernateException unexpectedContextDetected(Object obj, ContextInternal expectedContext, ContextInternal currentContext);
282+
280283
// Same method that exists in CoreMessageLogger
281284
@LogMessage(level = WARN)
282285
@Message(id = 104, value = "firstResult/maxResults specified with collection fetch; applying in memory!" )

0 commit comments

Comments
 (0)