Skip to content

Commit c5893e7

Browse files
dreab8beikov
authored andcommitted
HHH-16461 @Version + session.refresh(entity, LockMode.PESSIMISTIC_WRITE) leads to StaleObjectStateException
1 parent f80b907 commit c5893e7

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmJdbcExecutionContextAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@ public boolean hasQueryExecutionToBeAddedToStatistics() {
7575
return true;
7676
}
7777

78+
@Override
79+
public boolean upgradeLocks() {
80+
return true;
81+
}
7882
}

hibernate-core/src/main/java/org/hibernate/sql/exec/spi/ExecutionContext.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,12 @@ default boolean hasQueryExecutionToBeAddedToStatistics() {
9090
return false;
9191
}
9292

93+
/**
94+
* Does this query return objects that might be already cached
95+
* by the session, whose lock mode may need upgrading
96+
*/
97+
default boolean upgradeLocks(){
98+
return false;
99+
}
100+
93101
}

hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ && getEntityKey().getIdentifier().equals( executionContext.getEntityId() ) ) {
571571
}
572572

573573
private void upgradeLockMode(RowProcessingState rowProcessingState) {
574-
if ( lockMode != LockMode.NONE ) {
574+
if ( lockMode != LockMode.NONE && rowProcessingState.upgradeLocks() ) {
575575
final EntityEntry entry =
576576
rowProcessingState.getSession().getPersistenceContextInternal()
577577
.getEntry( entityInstance );

hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowProcessingStateStandardImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,9 @@ public Initializer resolveInitializer(NavigablePath path) {
201201
public boolean hasCollectionInitializers() {
202202
return this.initializers.hasCollectionInitializers();
203203
}
204+
205+
@Override
206+
public boolean upgradeLocks() {
207+
return executionContext.upgradeLocks();
208+
}
204209
}

0 commit comments

Comments
 (0)