Skip to content

Commit d6ae560

Browse files
committed
HHH-19336 - Proper implementation for JPA extended locking scope
HHH-19459 - LockScope, FollowOnLocking HHH-19501 - Session#lock w/ pessimistic locks for scopes HHH-19502 - Disallow SKIP_LOCKED with Session#lock HHH-19503 - Track a Dialect's level of support for locking joined tables
1 parent 321fd2e commit d6ae560

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,9 @@ else if ( option instanceof Locking.Scope lockScope ) {
25362536
else if ( option instanceof PessimisticLockScope pessimisticLockScope ) {
25372537
lockOptions.setScope( Locking.Scope.fromJpaScope( pessimisticLockScope ) );
25382538
}
2539+
else if ( option instanceof Locking.FollowOn followOn ) {
2540+
lockOptions.setFollowOnStrategy( followOn );
2541+
}
25392542
else if ( option instanceof Timeout timeout ) {
25402543
lockOptions.setTimeout( timeout );
25412544
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.locking.options;
6+
7+
import jakarta.persistence.LockModeType;
8+
import org.hibernate.Locking;
9+
import org.hibernate.testing.jdbc.SQLStatementInspector;
10+
import org.hibernate.testing.orm.junit.DomainModel;
11+
import org.hibernate.testing.orm.junit.SessionFactory;
12+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
/**
20+
* @author Steve Ebersole
21+
*/
22+
@SuppressWarnings("JUnitMalformedDeclaration")
23+
@DomainModel(annotatedClasses = {Book.class, Person.class, Publisher.class, Report.class})
24+
@SessionFactory(useCollectingStatementInspector = true)
25+
public class FollowOnLockingTests {
26+
@BeforeEach
27+
void createTestData(SessionFactoryScope factoryScope) {
28+
Helper.createTestData( factoryScope );
29+
}
30+
31+
@AfterEach
32+
void dropTestData(SessionFactoryScope factoryScope) {
33+
factoryScope.dropData();
34+
}
35+
36+
@Test
37+
void testFindBaseline(SessionFactoryScope factoryScope) {
38+
final SQLStatementInspector sqlCollector = factoryScope.getCollectingStatementInspector();
39+
40+
factoryScope.inTransaction( (session) -> {
41+
sqlCollector.clear();
42+
session.find( Book.class, 1, LockModeType.PESSIMISTIC_WRITE );
43+
44+
assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 );
45+
Helper.checkSql( sqlCollector.getSqlQueries().get( 0 ), session.getDialect(), Helper.Table.BOOKS );
46+
} );
47+
}
48+
49+
@Test
50+
void testFindWithForced(SessionFactoryScope factoryScope) {
51+
final SQLStatementInspector sqlCollector = factoryScope.getCollectingStatementInspector();
52+
53+
factoryScope.inTransaction( (session) -> {
54+
sqlCollector.clear();
55+
session.find( Book.class, 1, LockModeType.PESSIMISTIC_WRITE, Locking.FollowOn.FORCE );
56+
57+
assertThat( sqlCollector.getSqlQueries() ).hasSize( 2 );
58+
Helper.checkSql( sqlCollector.getSqlQueries().get( 1 ), session.getDialect(), Helper.Table.BOOKS );
59+
} );
60+
}
61+
}

hibernate-core/src/test/java/org/hibernate/orm/test/locking/options/ScopeTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void testRefreshWithExtended(SessionFactoryScope factoryScope) {
225225
assertThat( Hibernate.isInitialized( theTalisman ) ).isTrue();
226226

227227
sqlCollector.clear();
228-
session.lock( theTalisman, PESSIMISTIC_WRITE, EXTENDED );
228+
session.refresh( theTalisman, PESSIMISTIC_WRITE, EXTENDED );
229229
assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 );
230230
Helper.checkSql( sqlCollector.getSqlQueries().get( 0 ), session.getDialect(), BOOKS );
231231
TransactionUtil.updateTable( factoryScope, BOOKS.getTableName(), "title", true );

0 commit comments

Comments
 (0)