Skip to content

Commit bdc1aa9

Browse files
committed
generalize loaders to handle stateless sessions
cc @jrenaat
1 parent 00e552e commit bdc1aa9

26 files changed

+205
-104
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HSQLLegacyDialect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder;
4242
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
4343
import org.hibernate.engine.spi.SessionFactoryImplementor;
44-
import org.hibernate.event.spi.EventSource;
44+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
4545
import org.hibernate.exception.ConstraintViolationException;
4646
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
4747
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
@@ -797,7 +797,7 @@ private ReadUncommittedLockingStrategy(EntityPersister lockable, LockMode lockMo
797797
}
798798

799799
@Override
800-
public void lock(Object id, Object version, Object object, int timeout, EventSource session)
800+
public void lock(Object id, Object version, Object object, int timeout, SharedSessionContractImplementor session)
801801
throws StaleObjectStateException, JDBCException {
802802
if ( getLockMode().greaterThan( LockMode.READ ) ) {
803803
LOG.hsqldbSupportsOnlyReadCommittedIsolation();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
2626
import org.hibernate.engine.jdbc.env.spi.SchemaNameResolver;
2727
import org.hibernate.engine.spi.SessionFactoryImplementor;
28-
import org.hibernate.event.spi.EventSource;
28+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2929
import org.hibernate.internal.util.collections.ArrayHelper;
3030
import org.hibernate.mapping.Column;
3131
import org.hibernate.mapping.ForeignKey;
@@ -891,7 +891,7 @@ static class DoNothingLockingStrategy implements LockingStrategy {
891891

892892
@Override
893893
public void lock(
894-
Object id, Object version, Object object, int timeout, EventSource session)
894+
Object id, Object version, Object object, int timeout, SharedSessionContractImplementor session)
895895
throws StaleObjectStateException, LockingStrategyException {
896896
// Do nothing. Cloud Spanner doesn't have have locking strategies.
897897
}

hibernate-core/src/main/java/org/hibernate/dialect/lock/AbstractSelectLockingStrategy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.hibernate.StaleObjectStateException;
1212
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
1313
import org.hibernate.engine.spi.SessionFactoryImplementor;
14-
import org.hibernate.event.spi.EventSource;
14+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1515
import org.hibernate.persister.entity.EntityPersister;
1616
import org.hibernate.sql.SimpleSelect;
1717
import org.hibernate.stat.spi.StatisticsImplementor;
@@ -69,7 +69,7 @@ protected String generateLockString(int lockTimeout) {
6969
}
7070

7171
@Override
72-
public void lock(Object id, Object version, Object object, int timeout, EventSource session)
72+
public void lock(Object id, Object version, Object object, int timeout, SharedSessionContractImplementor session)
7373
throws StaleObjectStateException, JDBCException {
7474
final String sql = determineSql( timeout );
7575
final SessionFactoryImplementor factory = session.getFactory();
@@ -112,7 +112,7 @@ public void lock(Object id, Object version, Object object, int timeout, EventSou
112112
}
113113
}
114114

115-
private JDBCException jdbcException(Object id, EventSource session, SQLException sqle, String sql) {
115+
private JDBCException jdbcException(Object id, SharedSessionContractImplementor session, SQLException sqle, String sql) {
116116
return session.getJdbcServices().getSqlExceptionHelper()
117117
.convert( sqle, "could not lock: " + infoString( lockable, id, session.getFactory() ), sql );
118118
}

hibernate-core/src/main/java/org/hibernate/dialect/lock/LockingStrategy.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package org.hibernate.dialect.lock;
66

77
import org.hibernate.StaleObjectStateException;
8+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
89
import org.hibernate.event.spi.EventSource;
910

1011
/**
@@ -22,6 +23,28 @@
2223
* @author Steve Ebersole
2324
*/
2425
public interface LockingStrategy {
26+
/**
27+
* Acquire an appropriate type of lock on the underlying data that will
28+
* endure until the end of the current transaction.
29+
*
30+
* @param id The id of the row to be locked
31+
* @param version The current version (or null if not versioned)
32+
* @param object The object logically being locked (currently not used)
33+
* @param timeout timeout in milliseconds, 0 = no wait, -1 = wait indefinitely
34+
* @param session The session from which the lock request originated
35+
*
36+
* @throws StaleObjectStateException Indicates an inability to locate the database row as part of acquiring
37+
* the requested lock.
38+
* @throws LockingStrategyException Indicates a failure in the lock attempt
39+
*
40+
* @deprecated Use {@link #lock(Object, Object, Object, int, SharedSessionContractImplementor)}
41+
*/
42+
@Deprecated(since = "7")
43+
default void lock(Object id, Object version, Object object, int timeout, EventSource session)
44+
throws StaleObjectStateException, LockingStrategyException {
45+
lock( id, version, object, timeout, (SharedSessionContractImplementor) session );
46+
}
47+
2548
/**
2649
* Acquire an appropriate type of lock on the underlying data that will
2750
* endure until the end of the current transaction.
@@ -36,6 +59,13 @@ public interface LockingStrategy {
3659
* the requested lock.
3760
* @throws LockingStrategyException Indicates a failure in the lock attempt
3861
*/
39-
void lock(Object id, Object version, Object object, int timeout, EventSource session)
40-
throws StaleObjectStateException, LockingStrategyException;
62+
default void lock(Object id, Object version, Object object, int timeout, SharedSessionContractImplementor session)
63+
throws StaleObjectStateException, LockingStrategyException {
64+
if ( session instanceof EventSource eventSource ) {
65+
lock( id, version, object, timeout, eventSource );
66+
}
67+
else {
68+
throw new UnsupportedOperationException( "Optimistic locking strategies not supported in stateless session" );
69+
}
70+
}
4171
}

hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticForceIncrementLockingStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.hibernate.HibernateException;
88
import org.hibernate.LockMode;
99
import org.hibernate.engine.spi.EntityEntry;
10-
import org.hibernate.event.spi.EventSource;
10+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1111
import org.hibernate.persister.entity.EntityPersister;
1212

1313
/**
@@ -39,7 +39,7 @@ public PessimisticForceIncrementLockingStrategy(EntityPersister lockable, LockMo
3939
}
4040

4141
@Override
42-
public void lock(Object id, Object version, Object object, int timeout, EventSource session) {
42+
public void lock(Object id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
4343
if ( !lockable.isVersioned() ) {
4444
throw new HibernateException( "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
4545
}

hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticReadUpdateLockingStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.hibernate.StaleObjectStateException;
1515
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
1616
import org.hibernate.engine.spi.SessionFactoryImplementor;
17-
import org.hibernate.event.spi.EventSource;
17+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1818
import org.hibernate.internal.CoreMessageLogger;
1919
import org.hibernate.persister.entity.EntityPersister;
2020
import org.hibernate.pretty.MessageHelper;
@@ -69,7 +69,7 @@ public PessimisticReadUpdateLockingStrategy(EntityPersister lockable, LockMode l
6969
}
7070

7171
@Override
72-
public void lock(Object id, Object version, Object object, int timeout, EventSource session) {
72+
public void lock(Object id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
7373
if ( !lockable.isVersioned() ) {
7474
throw new HibernateException( "write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
7575
}

hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteUpdateLockingStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.hibernate.StaleObjectStateException;
1515
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
1616
import org.hibernate.engine.spi.SessionFactoryImplementor;
17-
import org.hibernate.event.spi.EventSource;
17+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1818
import org.hibernate.internal.CoreMessageLogger;
1919
import org.hibernate.persister.entity.EntityPersister;
2020
import org.hibernate.pretty.MessageHelper;
@@ -68,7 +68,7 @@ public PessimisticWriteUpdateLockingStrategy(EntityPersister lockable, LockMode
6868
}
6969

7070
@Override
71-
public void lock(Object id, Object version, Object object, int timeout, EventSource session) {
71+
public void lock(Object id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
7272
if ( !lockable.isVersioned() ) {
7373
throw new HibernateException( "write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
7474
}

hibernate-core/src/main/java/org/hibernate/dialect/lock/UpdateLockingStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.hibernate.StaleObjectStateException;
1515
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
1616
import org.hibernate.engine.spi.SessionFactoryImplementor;
17-
import org.hibernate.event.spi.EventSource;
17+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1818
import org.hibernate.internal.CoreMessageLogger;
1919
import org.hibernate.persister.entity.EntityPersister;
2020
import org.hibernate.pretty.MessageHelper;
@@ -72,7 +72,7 @@ public void lock(
7272
Object version,
7373
Object object,
7474
int timeout,
75-
EventSource session) throws StaleObjectStateException, JDBCException {
75+
SharedSessionContractImplementor session) throws StaleObjectStateException, JDBCException {
7676
final String lockableEntityName = lockable.getEntityName();
7777
if ( !lockable.isVersioned() ) {
7878
throw new HibernateException( "write locks via update not supported for non-versioned entities [" + lockableEntityName + "]" );

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,4 +1236,9 @@ public FormatMapper getJsonFormatMapper() {
12361236
public FormatMapper getXmlFormatMapper() {
12371237
return delegate.getXmlFormatMapper();
12381238
}
1239+
1240+
@Override
1241+
public Object loadFromSecondLevelCache(EntityPersister persister, EntityKey entityKey, Object instanceToLoad, LockMode lockMode) {
1242+
return delegate.loadFromSecondLevelCache( persister, entityKey, instanceToLoad, lockMode );
1243+
}
12391244
}

hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
import org.hibernate.FlushMode;
1414
import org.hibernate.HibernateException;
15+
import org.hibernate.Incubating;
1516
import org.hibernate.Interceptor;
17+
import org.hibernate.LockMode;
1618
import org.hibernate.StatelessSession;
1719
import org.hibernate.boot.spi.SessionFactoryOptions;
1820
import org.hibernate.dialect.Dialect;
@@ -584,4 +586,18 @@ default boolean isStatelessSession() {
584586
return false;
585587
}
586588

589+
/**
590+
* Attempts to load the entity from the second-level cache.
591+
*
592+
* @param persister The persister for the entity being requested for load
593+
* @param entityKey The entity key
594+
* @param instanceToLoad The instance that is being initialized, or null
595+
* @param lockMode The lock mode
596+
*
597+
* @return The entity from the second-level cache, or null.
598+
*
599+
* @since 7.0
600+
*/
601+
@Incubating
602+
Object loadFromSecondLevelCache(EntityPersister persister, EntityKey entityKey, Object instanceToLoad, LockMode lockMode);
587603
}

0 commit comments

Comments
 (0)