Skip to content

Commit 6efd6ba

Browse files
committed
HHH-19939, HHH-19937 add tests
1 parent 3878227 commit 6efd6ba

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.locking;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.Table;
12+
import jakarta.persistence.Version;
13+
import org.hibernate.annotations.GenericGenerator;
14+
15+
/**
16+
* @author Steve Ebersole
17+
*/
18+
@Entity
19+
@Table( name = "T_LOCK_C" )
20+
public class C {
21+
private Long id;
22+
private String value;
23+
private int version;
24+
25+
public C() {
26+
}
27+
28+
public C(String value) {
29+
this.value = value;
30+
}
31+
32+
@Id
33+
@GeneratedValue( generator = "increment" )
34+
@GenericGenerator( name = "increment", strategy = "increment" )
35+
public Long getId() {
36+
return id;
37+
}
38+
39+
public void setId(Long id) {
40+
this.id = id;
41+
}
42+
43+
@Column(name="c_value")
44+
public String getValue() {
45+
return value;
46+
}
47+
48+
public void setValue(String value) {
49+
this.value = value;
50+
}
51+
52+
@Version
53+
public int getVersion() {
54+
return version;
55+
}
56+
57+
public void setVersion(int version) {
58+
this.version = version;
59+
}
60+
}

hibernate-core/src/test/java/org/hibernate/orm/test/locking/LockModeTest.java

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import org.hibernate.LockMode;
1616
import org.hibernate.Session;
17+
import org.hibernate.StaleObjectStateException;
1718
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
1819
import org.hibernate.cfg.AvailableSettings;
1920
import org.hibernate.community.dialect.AltibaseDialect;
@@ -22,6 +23,7 @@
2223
import org.hibernate.dialect.SQLServerDialect;
2324
import org.hibernate.dialect.SybaseASEDialect;
2425
import org.hibernate.dialect.SybaseDialect;
26+
import org.hibernate.dialect.lock.OptimisticEntityLockException;
2527
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2628
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
2729
import org.hibernate.query.criteria.JpaCriteriaQuery;
@@ -43,6 +45,7 @@
4345
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
4446
import static org.junit.jupiter.api.Assertions.assertEquals;
4547
import static org.junit.jupiter.api.Assertions.assertNotNull;
48+
import static org.junit.jupiter.api.Assertions.assertThrows;
4649
import static org.junit.jupiter.api.Assertions.fail;
4750

4851
/**
@@ -56,10 +59,11 @@
5659
public class LockModeTest extends BaseSessionFactoryFunctionalTest {
5760

5861
private Long id;
62+
private Long cid;
5963

6064
@Override
6165
protected Class<?>[] getAnnotatedClasses() {
62-
return new Class[] { A.class };
66+
return new Class[] { A.class, C.class };
6367
}
6468

6569
@Override
@@ -79,6 +83,9 @@ public void prepareTest() throws Exception {
7983
A a = new A( "it" );
8084
session.persist( a );
8185
id = a.getId();
86+
C c = new C( "it" );
87+
session.persist( c );
88+
cid = c.getId();
8289
} );
8390
}
8491

@@ -263,6 +270,88 @@ public void testRefreshWithExplicitHigherLevelLockMode2() {
263270
} );
264271
}
265272

273+
@Test
274+
@JiraKey(value = "HHH-19937")
275+
public void testRefreshWithOptimisticExplicitHigherLevelLockMode() {
276+
doInHibernate( this::sessionFactory, session -> {
277+
C c = session.find( C.class, id );
278+
checkLockMode( c, LockMode.READ, session );
279+
session.refresh( c, LockModeType.OPTIMISTIC );
280+
checkLockMode( c, LockMode.OPTIMISTIC, session );
281+
session.refresh( c );
282+
checkLockMode( c, LockMode.OPTIMISTIC, session );
283+
} );
284+
doInHibernate( this::sessionFactory, session -> {
285+
C c = session.find( C.class, id );
286+
checkLockMode( c, LockMode.READ, session );
287+
session.refresh( c, LockModeType.OPTIMISTIC );
288+
checkLockMode( c, LockMode.OPTIMISTIC, session );
289+
session.refresh( c, LockMode.OPTIMISTIC_FORCE_INCREMENT );
290+
checkLockMode( c, LockMode.OPTIMISTIC_FORCE_INCREMENT, session );
291+
} );
292+
doInHibernate( this::sessionFactory, session -> {
293+
C c = session.find( C.class, id );
294+
checkLockMode( c, LockMode.READ, session );
295+
session.refresh( c, LockModeType.OPTIMISTIC_FORCE_INCREMENT );
296+
checkLockMode( c, LockMode.OPTIMISTIC_FORCE_INCREMENT, session );
297+
session.refresh( c );
298+
checkLockMode( c, LockMode.OPTIMISTIC_FORCE_INCREMENT, session );
299+
} );
300+
}
301+
302+
@Test
303+
@JiraKey(value = "HHH-19937")
304+
public void testRefreshWithOptimisticLockRefresh() {
305+
doInHibernate( this::sessionFactory, session -> {
306+
C c = session.find( C.class, id, LockModeType.OPTIMISTIC );
307+
doInHibernate( this::sessionFactory, s -> {
308+
s.find( C.class, id ).setValue( "new value" );
309+
} );
310+
session.refresh( c );
311+
} );
312+
}
313+
314+
@Test
315+
@JiraKey(value = "HHH-19937")
316+
public void testRefreshWithOptimisticForceIncrementLockRefresh() {
317+
doInHibernate( this::sessionFactory, session -> {
318+
C c = session.find( C.class, id, LockModeType.OPTIMISTIC_FORCE_INCREMENT );
319+
doInHibernate( this::sessionFactory, s -> {
320+
s.find( C.class, id ).setValue( "new value" );
321+
} );
322+
session.refresh( c );
323+
} );
324+
}
325+
326+
@Test
327+
@JiraKey(value = "HHH-19937")
328+
public void testRefreshWithOptimisticLockFailure() {
329+
assertThrows( OptimisticEntityLockException.class, () ->
330+
doInHibernate( this::sessionFactory, session -> {
331+
C c = session.find( C.class, id, LockModeType.OPTIMISTIC );
332+
session.refresh( c );
333+
doInHibernate( this::sessionFactory, s -> {
334+
s.find( C.class, id ).setValue( "new value" );
335+
} );
336+
} )
337+
);
338+
}
339+
340+
@Test
341+
@JiraKey(value = "HHH-19937")
342+
public void testRefreshWithOptimisticForceIncrementLockFailure() {
343+
// TODO: shouldn't this also be an OptimisticEntityLockException
344+
assertThrows( StaleObjectStateException.class, () ->
345+
doInHibernate( this::sessionFactory, session -> {
346+
C c = session.find( C.class, id, LockModeType.OPTIMISTIC_FORCE_INCREMENT );
347+
session.refresh( c );
348+
doInHibernate( this::sessionFactory, s -> {
349+
s.find( C.class, id ).setValue( "new value" );
350+
} );
351+
} )
352+
);
353+
}
354+
266355
@Test
267356
@JiraKey(value = "HHH-12257")
268357
public void testRefreshAfterUpdate() {

0 commit comments

Comments
 (0)