Skip to content

Commit 230c45b

Browse files
committed
HHH-19551 - Address deficiencies in pessimistic locking
1 parent 9852e1e commit 230c45b

File tree

3 files changed

+14
-30
lines changed

3 files changed

+14
-30
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/locking/jpa/FollowOnLockingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void testQueryLocking(SessionFactoryScope scope, boolean followOnLocking)
9797
statementInspector.assertExecutedCount( 1 );
9898
}
9999

100-
TransactionUtil.deleteFromTable( scope, "employees", true);
100+
TransactionUtil.updateTable( scope, "employees", "salary", true );
101101
}
102102
);
103103
} );

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ void testFindWithExtendedAndFetch(SessionFactoryScope factoryScope) {
137137
if ( session.getDialect().supportsOuterJoinForUpdate() ) {
138138
assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 );
139139
Helper.checkSql( sqlCollector.getSqlQueries().get( 0 ), session.getDialect(), BOOKS, BOOK_GENRES );
140-
TransactionUtil.deleteFromTable( factoryScope, BOOKS.getTableName(), true );
141-
TransactionUtil.deleteFromTable( factoryScope, BOOK_GENRES.getTableName(), true );
140+
141+
TransactionUtil.updateTable( factoryScope, BOOKS.getTableName(), "title", true );
142+
TransactionUtil.updateTable( factoryScope, BOOK_GENRES.getTableName(), "genre", true );
142143
}
143144
else {
144145
// should be 3, but follow-on locking is not locking collection tables...

hibernate-testing/src/main/java/org/hibernate/testing/orm/transaction/TransactionUtil.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -166,36 +166,19 @@ public static void updateTable(SessionFactoryScope factoryScope, String tableNam
166166
}
167167
}
168168
catch (RuntimeException re) {
169-
if ( re.getCause() instanceof ConstraintViolationException cve ) {
170-
throw cve;
169+
if ( re.getCause() instanceof jakarta.persistence.LockTimeoutException
170+
|| re.getCause() instanceof org.hibernate.exception.LockTimeoutException ) {
171+
if ( !expectingToBlock ) {
172+
fail( "Expecting update to " + tableName + " to succeed, but failed due to async timeout (presumably due to locks)", re.getCause() );
173+
}
171174
}
172-
throw re;
173-
}
174-
}
175-
176-
public static void deleteFromTable(SessionFactoryScope factoryScope, String tableName, boolean expectingToBlock) {
177-
try {
178-
AsyncExecutor.executeAsync( 2, TimeUnit.SECONDS, () -> {
179-
factoryScope.inTransaction( (session) -> {
180-
//noinspection deprecation
181-
session.createNativeQuery( "delete from " + tableName ).executeUpdate();
182-
if ( expectingToBlock ) {
183-
fail( "Expecting delete from " + tableName + " to block dues to locks" );
184-
}
185-
} );
186-
} );
187-
}
188-
catch (AsyncExecutor.TimeoutException expected) {
189-
if ( !expectingToBlock ) {
190-
fail( "Expecting delete from " + tableName + " to succeed, but failed due to async timeout (presumably due to locks)", expected );
175+
else if ( re.getCause() instanceof ConstraintViolationException cve ) {
176+
throw cve;
191177
}
192-
}
193-
catch (RuntimeException re) {
194-
if ( re.getCause() instanceof ConstraintViolationException cve ) {
195-
log.debug( "deleting from table did not block, but did lead to ConstraintViolationException", cve );
196-
return;
178+
else {
179+
throw re;
197180
}
198-
throw re;
199181
}
200182
}
183+
201184
}

0 commit comments

Comments
 (0)