|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.locking.options; |
| 6 | + |
| 7 | +import jakarta.persistence.LockModeType; |
| 8 | +import org.hibernate.Locking; |
| 9 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 10 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Steve Ebersole |
| 21 | + */ |
| 22 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 23 | +@DomainModel(annotatedClasses = {Book.class, Person.class, Publisher.class, Report.class}) |
| 24 | +@SessionFactory(useCollectingStatementInspector = true) |
| 25 | +public class FollowOnLockingTests { |
| 26 | + @BeforeEach |
| 27 | + void createTestData(SessionFactoryScope factoryScope) { |
| 28 | + Helper.createTestData( factoryScope ); |
| 29 | + } |
| 30 | + |
| 31 | + @AfterEach |
| 32 | + void dropTestData(SessionFactoryScope factoryScope) { |
| 33 | + factoryScope.dropData(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testFindBaseline(SessionFactoryScope factoryScope) { |
| 38 | + final SQLStatementInspector sqlCollector = factoryScope.getCollectingStatementInspector(); |
| 39 | + |
| 40 | + factoryScope.inTransaction( (session) -> { |
| 41 | + sqlCollector.clear(); |
| 42 | + session.find( Book.class, 1, LockModeType.PESSIMISTIC_WRITE ); |
| 43 | + |
| 44 | + assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 ); |
| 45 | + Helper.checkSql( sqlCollector.getSqlQueries().get( 0 ), session.getDialect(), Helper.Table.BOOKS ); |
| 46 | + } ); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testFindWithForced(SessionFactoryScope factoryScope) { |
| 51 | + final SQLStatementInspector sqlCollector = factoryScope.getCollectingStatementInspector(); |
| 52 | + |
| 53 | + factoryScope.inTransaction( (session) -> { |
| 54 | + sqlCollector.clear(); |
| 55 | + session.find( Book.class, 1, LockModeType.PESSIMISTIC_WRITE, Locking.FollowOn.FORCE ); |
| 56 | + |
| 57 | + assertThat( sqlCollector.getSqlQueries() ).hasSize( 2 ); |
| 58 | + Helper.checkSql( sqlCollector.getSqlQueries().get( 1 ), session.getDialect(), Helper.Table.BOOKS ); |
| 59 | + } ); |
| 60 | + } |
| 61 | +} |
0 commit comments