1010import org .hibernate .cfg .Configuration ;
1111import org .hibernate .reactive .testing .SqlStatementTracker ;
1212
13- import java .util .ArrayList ;
14- import java .util .Collection ;
15- import java .util .List ;
16- import java .util .concurrent .TimeUnit ;
17-
1813import org .junit .jupiter .api .BeforeEach ;
19- import org .junit .jupiter .api .Disabled ;
2014import org .junit .jupiter .api .Test ;
2115
2216import io .vertx .junit5 .Timeout ;
2721import jakarta .persistence .LockModeType ;
2822import jakarta .persistence .ManyToOne ;
2923import jakarta .persistence .OneToMany ;
24+ import java .util .ArrayList ;
25+ import java .util .Collection ;
26+ import java .util .List ;
27+ import java .util .concurrent .TimeUnit ;
3028
3129import static org .assertj .core .api .Assertions .assertThat ;
3230import static org .hibernate .reactive .containers .DatabaseConfiguration .dbType ;
@@ -84,7 +82,6 @@ context, getMutinySessionFactory()
8482 );
8583 }
8684
87- @ Disabled
8885 @ Test
8986 public void testFindUpgradeNoWait (VertxTestContext context ) {
9087 Child child = new Child ( CHILD_ID , "And" );
@@ -97,30 +94,51 @@ context, getMutinySessionFactory()
9794 .invoke ( c -> {
9895 assertThat ( c ).isNotNull ();
9996 assertThat ( c .getId () ).isEqualTo ( CHILD_ID );
100- String selectQuery = sqlTracker .getLoggedQueries ().get ( 0 );
101- assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
102- assertThat ( selectQuery )
103- .matches ( this ::noWaitLockingPredicate , "SQL query with nowait lock for " + dbType ().name () );
97+
98+ assertThatExecutedQueriesContainLock ();
10499 }
105100 ) ) )
106101 );
107102 }
108103
109- /**
110- * @return true if the query contains the expected nowait keyword for the selected database
111- */
112- private boolean noWaitLockingPredicate (String selectQuery ) {
113- return switch ( dbType () ) {
114- case POSTGRESQL -> selectQuery .endsWith ( "for no key update of c1_0 nowait" );
115- case COCKROACHDB -> selectQuery .endsWith ( "for update of c1_0 nowait" );
116- case SQLSERVER -> selectQuery .contains ( "with (updlock,holdlock,rowlock,nowait)" );
117- case ORACLE -> selectQuery .contains ( "for update of c1_0.id nowait" );
104+ private void assertThatExecutedQueriesContainLock () {
105+ switch ( dbType () ) {
106+ case SQLSERVER -> {
107+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
108+ assertThat ( sqlTracker .getLoggedQueries ()
109+ .get ( 0 ) ).contains ( "with (updlock,holdlock,rowlock,nowait)" );
110+ }
118111 // DB2 does not support nowait
119- case DB2 -> selectQuery .contains ( "for read only with rs use and keep update locks" );
120- case MARIA -> selectQuery .contains ( "for update nowait" );
121- case MYSQL -> selectQuery .contains ( "for update of c1_0 nowait" );
112+ case DB2 -> {
113+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
114+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) ).contains (
115+ "for read only with rs use and keep update locks" );
116+ }
117+ case ORACLE -> {
118+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
119+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) ).contains ( "for update of c1_0.id nowait" );
120+ }
121+ case MARIA -> {
122+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
123+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) ).contains ( "for update nowait" );
124+ }
125+ case MYSQL -> {
126+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
127+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) ).contains ( "for update of c1_0 nowait" );
128+ }
129+ // For PostgresSql and CockroachDb LockStrategy.FOLLOW_ON is applied
130+ // (dialects do not support outer join for update, see org.hibernate.sql.ast.spiAbstractSqlAst#determineLockingStrategy(QuerySpec,Locking.FollowOn))
131+ // so 2 queries are executed, the first one select the entity and contains the join the second one does not contain the join but contains the lock clause.
132+ case POSTGRESQL -> {
133+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 2 );
134+ assertThat ( sqlTracker .getLoggedQueries ().get ( 1 ) ).endsWith ( "for no key update of tbl nowait" );
135+ }
136+ case COCKROACHDB -> {
137+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 2 );
138+ assertThat ( sqlTracker .getLoggedQueries ().get ( 1 ) ).endsWith ( "for update of tbl nowait" );
139+ }
122140 default -> throw new IllegalArgumentException ( "Database not recognized: " + dbType ().name () );
123- };
141+ }
124142 }
125143
126144 @ Entity (name = "Parent" )
0 commit comments