44 */
55package org .hibernate .orm .test .locking ;
66
7- import java .sql .Connection ;
8- import java .util .Collections ;
9-
107import jakarta .persistence .LockModeType ;
118import jakarta .persistence .Timeout ;
129import jakarta .persistence .criteria .CriteriaBuilder ;
1310import jakarta .persistence .criteria .CriteriaQuery ;
14-
1511import org .hibernate .LockMode ;
1612import org .hibernate .Session ;
17- import org .hibernate .StaleObjectStateException ;
1813import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
19- import org .hibernate .cfg .AvailableSettings ;
2014import org .hibernate .community .dialect .AltibaseDialect ;
2115import org .hibernate .community .dialect .InformixDialect ;
2216import org .hibernate .dialect .CockroachDialect ;
2317import org .hibernate .dialect .SQLServerDialect ;
2418import org .hibernate .dialect .SybaseASEDialect ;
2519import org .hibernate .dialect .SybaseDialect ;
26- import org .hibernate .dialect .lock .OptimisticEntityLockException ;
2720import org .hibernate .engine .spi .SharedSessionContractImplementor ;
2821import org .hibernate .query .criteria .HibernateCriteriaBuilder ;
2922import org .hibernate .query .criteria .JpaCriteriaQuery ;
3023import org .hibernate .query .sqm .tree .domain .SqmPath ;
31-
3224import org .hibernate .testing .orm .junit .BaseSessionFactoryFunctionalTest ;
3325import org .hibernate .testing .orm .junit .DialectFeatureChecks ;
26+ import org .hibernate .testing .orm .junit .JiraKey ;
3427import org .hibernate .testing .orm .junit .RequiresDialectFeature ;
3528import org .hibernate .testing .orm .junit .SkipForDialect ;
36- import org .hibernate .testing .orm .junit .JiraKey ;
3729import org .hibernate .testing .transaction .TransactionUtil ;
3830import org .hibernate .testing .util .ExceptionUtil ;
39-
4031import org .junit .jupiter .api .BeforeEach ;
4132import org .junit .jupiter .api .Test ;
4233
34+ import java .util .Collections ;
35+
4336import static jakarta .persistence .LockModeType .NONE ;
4437import static jakarta .persistence .LockModeType .PESSIMISTIC_WRITE ;
38+ import static java .sql .Connection .TRANSACTION_REPEATABLE_READ ;
39+ import static org .hibernate .cfg .JdbcSettings .ISOLATION ;
4540import static org .hibernate .testing .transaction .TransactionUtil .doInHibernate ;
4641import static org .junit .jupiter .api .Assertions .assertEquals ;
4742import static org .junit .jupiter .api .Assertions .assertNotNull ;
48- import static org .junit .jupiter .api .Assertions .assertThrows ;
4943import static org .junit .jupiter .api .Assertions .fail ;
5044
5145/**
5953public 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 () {
0 commit comments