Skip to content

Commit ad1be82

Browse files
committed
HHH-19939, HHH-19937 fix tests on MySQL/Informix
1 parent 602d47f commit ad1be82

File tree

2 files changed

+158
-102
lines changed

2 files changed

+158
-102
lines changed

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

Lines changed: 7 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,42 @@
44
*/
55
package org.hibernate.orm.test.locking;
66

7-
import java.sql.Connection;
8-
import java.util.Collections;
9-
107
import jakarta.persistence.LockModeType;
118
import jakarta.persistence.Timeout;
129
import jakarta.persistence.criteria.CriteriaBuilder;
1310
import jakarta.persistence.criteria.CriteriaQuery;
14-
1511
import org.hibernate.LockMode;
1612
import org.hibernate.Session;
17-
import org.hibernate.StaleObjectStateException;
1813
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
19-
import org.hibernate.cfg.AvailableSettings;
2014
import org.hibernate.community.dialect.AltibaseDialect;
2115
import org.hibernate.community.dialect.InformixDialect;
2216
import org.hibernate.dialect.CockroachDialect;
2317
import org.hibernate.dialect.SQLServerDialect;
2418
import org.hibernate.dialect.SybaseASEDialect;
2519
import org.hibernate.dialect.SybaseDialect;
26-
import org.hibernate.dialect.lock.OptimisticEntityLockException;
2720
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2821
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
2922
import org.hibernate.query.criteria.JpaCriteriaQuery;
3023
import org.hibernate.query.sqm.tree.domain.SqmPath;
31-
3224
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
3325
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
26+
import org.hibernate.testing.orm.junit.JiraKey;
3427
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
3528
import org.hibernate.testing.orm.junit.SkipForDialect;
36-
import org.hibernate.testing.orm.junit.JiraKey;
3729
import org.hibernate.testing.transaction.TransactionUtil;
3830
import org.hibernate.testing.util.ExceptionUtil;
39-
4031
import org.junit.jupiter.api.BeforeEach;
4132
import org.junit.jupiter.api.Test;
4233

34+
import java.util.Collections;
35+
4336
import static jakarta.persistence.LockModeType.NONE;
4437
import static jakarta.persistence.LockModeType.PESSIMISTIC_WRITE;
38+
import static java.sql.Connection.TRANSACTION_REPEATABLE_READ;
39+
import static org.hibernate.cfg.JdbcSettings.ISOLATION;
4540
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
4641
import static org.junit.jupiter.api.Assertions.assertEquals;
4742
import static org.junit.jupiter.api.Assertions.assertNotNull;
48-
import static org.junit.jupiter.api.Assertions.assertThrows;
4943
import static org.junit.jupiter.api.Assertions.fail;
5044

5145
/**
@@ -59,21 +53,17 @@
5953
public class LockModeTest extends BaseSessionFactoryFunctionalTest {
6054

6155
private Long id;
62-
private Long cid;
6356

6457
@Override
6558
protected Class<?>[] getAnnotatedClasses() {
66-
return new Class[] { A.class, C.class };
59+
return new Class[] { A.class };
6760
}
6861

6962
@Override
7063
protected void applySettings(StandardServiceRegistryBuilder ssrBuilder) {
7164
super.applySettings( ssrBuilder );
72-
// We can't use a shared connection provider if we use T ransactionUtil.setJdbcTimeout because that is set on the connection level
73-
// ssrBuilder.getSettings().remove( AvailableSettings.CONNECTION_PROVIDER );
7465
if ( getDialect() instanceof InformixDialect ) {
75-
ssrBuilder.applySetting( AvailableSettings.ISOLATION,
76-
Connection.TRANSACTION_REPEATABLE_READ );
66+
ssrBuilder.applySetting( ISOLATION, TRANSACTION_REPEATABLE_READ );
7767
}
7868
}
7969

@@ -83,9 +73,6 @@ public void prepareTest() throws Exception {
8373
A a = new A( "it" );
8474
session.persist( a );
8575
id = a.getId();
86-
C c = new C( "it" );
87-
session.persist( c );
88-
cid = c.getId();
8976
} );
9077
}
9178

@@ -270,88 +257,6 @@ public void testRefreshWithExplicitHigherLevelLockMode2() {
270257
} );
271258
}
272259

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-
355260
@Test
356261
@JiraKey(value = "HHH-12257")
357262
public void testRefreshAfterUpdate() {
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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.LockModeType;
8+
import org.hibernate.LockMode;
9+
import org.hibernate.Session;
10+
import org.hibernate.StaleObjectStateException;
11+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
12+
import org.hibernate.dialect.MySQLDialect;
13+
import org.hibernate.dialect.lock.OptimisticEntityLockException;
14+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
15+
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
16+
import org.hibernate.testing.orm.junit.JiraKey;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Test;
19+
20+
21+
import static java.sql.Connection.TRANSACTION_READ_COMMITTED;
22+
import static org.hibernate.cfg.JdbcSettings.ISOLATION;
23+
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
26+
27+
/**
28+
* Make sure that directly specifying lock modes, even though deprecated, continues to work until removed.
29+
*
30+
* @author Steve Ebersole
31+
*/
32+
@JiraKey( value = "HHH-5275")
33+
//@SkipForDialect(dialectClass = SybaseASEDialect.class, majorVersion = 15,
34+
// reason = "skip this test on Sybase ASE 15.5, but run it on 15.7, see HHH-6820")
35+
public class OptimisticLockModeTest extends BaseSessionFactoryFunctionalTest {
36+
37+
private Long id;
38+
39+
@Override
40+
protected Class<?>[] getAnnotatedClasses() {
41+
return new Class[] { C.class };
42+
}
43+
44+
@Override
45+
protected void applySettings(StandardServiceRegistryBuilder ssrBuilder) {
46+
super.applySettings( ssrBuilder );
47+
if ( getDialect() instanceof MySQLDialect ) {
48+
ssrBuilder.applySetting( ISOLATION, TRANSACTION_READ_COMMITTED );
49+
}
50+
}
51+
52+
@BeforeEach
53+
public void prepareTest() throws Exception {
54+
doInHibernate( this::sessionFactory, session -> {
55+
C c = new C( "it" );
56+
session.persist( c );
57+
id = c.getId();
58+
} );
59+
}
60+
61+
@Override
62+
protected boolean isCleanupTestDataRequired(){return true;}
63+
64+
@Test
65+
@JiraKey(value = "HHH-19937")
66+
public void testRefreshWithOptimisticLockRefresh() {
67+
doInHibernate( this::sessionFactory, session -> {
68+
C c = session.find( C.class, id, LockModeType.OPTIMISTIC );
69+
doInHibernate( this::sessionFactory, s -> {
70+
s.find( C.class, id ).setValue( "new value" );
71+
} );
72+
session.refresh( c );
73+
} );
74+
}
75+
76+
@Test
77+
@JiraKey(value = "HHH-19937")
78+
public void testRefreshWithOptimisticForceIncrementLockRefresh() {
79+
doInHibernate( this::sessionFactory, session -> {
80+
C c = session.find( C.class, id, LockModeType.OPTIMISTIC_FORCE_INCREMENT );
81+
doInHibernate( this::sessionFactory, s -> {
82+
s.find( C.class, id ).setValue( "new value" );
83+
} );
84+
session.refresh( c );
85+
} );
86+
}
87+
88+
@Test
89+
@JiraKey(value = "HHH-19937")
90+
public void testRefreshWithOptimisticLockFailure() {
91+
assertThrows( OptimisticEntityLockException.class, () ->
92+
doInHibernate( this::sessionFactory, session -> {
93+
C c = session.find( C.class, id, LockModeType.OPTIMISTIC );
94+
session.refresh( c );
95+
doInHibernate( this::sessionFactory, s -> {
96+
s.find( C.class, id ).setValue( "new value" );
97+
} );
98+
} )
99+
);
100+
}
101+
102+
@Test
103+
@JiraKey(value = "HHH-19937")
104+
public void testRefreshWithOptimisticForceIncrementLockFailure() {
105+
// TODO: shouldn't this also be an OptimisticEntityLockException
106+
assertThrows( StaleObjectStateException.class, () ->
107+
doInHibernate( this::sessionFactory, session -> {
108+
C c = session.find( C.class, id, LockModeType.OPTIMISTIC_FORCE_INCREMENT );
109+
session.refresh( c );
110+
doInHibernate( this::sessionFactory, s -> {
111+
s.find( C.class, id ).setValue( "new value" );
112+
} );
113+
} )
114+
);
115+
}
116+
117+
@Test
118+
@JiraKey(value = "HHH-19937")
119+
public void testRefreshWithOptimisticExplicitHigherLevelLockMode() {
120+
doInHibernate( this::sessionFactory, session -> {
121+
C c = session.find( C.class, id );
122+
checkLockMode( c, LockMode.READ, session );
123+
session.refresh( c, LockModeType.OPTIMISTIC );
124+
checkLockMode( c, LockMode.OPTIMISTIC, session );
125+
session.refresh( c );
126+
checkLockMode( c, LockMode.OPTIMISTIC, session );
127+
} );
128+
doInHibernate( this::sessionFactory, session -> {
129+
C c = session.find( C.class, id );
130+
checkLockMode( c, LockMode.READ, session );
131+
session.refresh( c, LockModeType.OPTIMISTIC );
132+
checkLockMode( c, LockMode.OPTIMISTIC, session );
133+
session.refresh( c, LockMode.OPTIMISTIC_FORCE_INCREMENT );
134+
checkLockMode( c, LockMode.OPTIMISTIC_FORCE_INCREMENT, session );
135+
} );
136+
doInHibernate( this::sessionFactory, session -> {
137+
C c = session.find( C.class, id );
138+
checkLockMode( c, LockMode.READ, session );
139+
session.refresh( c, LockModeType.OPTIMISTIC_FORCE_INCREMENT );
140+
checkLockMode( c, LockMode.OPTIMISTIC_FORCE_INCREMENT, session );
141+
session.refresh( c );
142+
checkLockMode( c, LockMode.OPTIMISTIC_FORCE_INCREMENT, session );
143+
} );
144+
}
145+
146+
private void checkLockMode(Object entity, LockMode expectedLockMode, Session session) {
147+
final LockMode lockMode =
148+
( (SharedSessionContractImplementor) session ).getPersistenceContext().getEntry( entity ).getLockMode();
149+
assertEquals( expectedLockMode, lockMode );
150+
}
151+
}

0 commit comments

Comments
 (0)