55 */
66package org .hibernate .reactive ;
77
8+ import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
89import org .hibernate .cfg .AvailableSettings ;
910import org .hibernate .cfg .Configuration ;
1011import org .hibernate .reactive .mutiny .impl .MutinySessionImpl ;
1314import org .hibernate .reactive .pool .impl .SqlClientConnection ;
1415import org .hibernate .reactive .stage .impl .StageSessionImpl ;
1516import org .hibernate .reactive .stage .impl .StageStatelessSessionImpl ;
17+ import org .hibernate .reactive .testing .SqlStatementTracker ;
1618
1719import org .junit .Test ;
1820
2426
2527public class BatchingConnectionTest extends ReactiveSessionTest {
2628
29+ private static SqlStatementTracker sqlTracker ;
30+
2731 @ Override
2832 protected Configuration constructConfiguration () {
2933 Configuration configuration = super .constructConfiguration ();
3034 configuration .setProperty ( AvailableSettings .STATEMENT_BATCH_SIZE , "5" );
35+
36+ // Construct a tracker that collects query statements via the SqlStatementLogger framework.
37+ // Pass in configuration properties to hand-off any actual logging properties
38+ sqlTracker = new SqlStatementTracker ( BatchingConnectionTest ::filter , configuration .getProperties () );
3139 return configuration ;
3240 }
3341
42+ protected void addServices (StandardServiceRegistryBuilder builder ) {
43+ sqlTracker .registerService ( builder );
44+ }
45+
46+ private static boolean filter (String s ) {
47+ String [] accepted = { "insert " , "update " , "delete " };
48+ for ( String valid : accepted ) {
49+ if ( s .toLowerCase ().startsWith ( valid ) ) {
50+ return true ;
51+ }
52+ }
53+ return false ;
54+ }
55+
56+ @ Test
57+ public void testBatchingWithPersistAll (TestContext context ) {
58+ test ( context , openSession ()
59+ .thenCompose ( s -> s
60+ .persist (
61+ new GuineaPig ( 11 , "One" ),
62+ new GuineaPig ( 22 , "Two" ),
63+ new GuineaPig ( 33 , "Three" )
64+ )
65+ // Auto-flush
66+ .thenCompose ( v -> s
67+ .createQuery ( "select name from GuineaPig" )
68+ .getResultList ()
69+ .thenAccept ( names -> {
70+ assertThat ( names ).containsExactlyInAnyOrder ( "One" , "Two" , "Three" );
71+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
72+ // Parameters are different for different dbs, so we cannot do an exact match
73+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) )
74+ .startsWith ( "insert into pig (name, version, id) values " );
75+ sqlTracker .clear ();
76+ } )
77+ )
78+ )
79+ );
80+ }
81+
3482 @ Test
3583 public void testBatching (TestContext context ) {
3684 test (
@@ -40,9 +88,17 @@ public void testBatching(TestContext context) {
4088 .thenCompose ( v -> s .persist ( new GuineaPig (11 , "One" ) ) )
4189 .thenCompose ( v -> s .persist ( new GuineaPig (22 , "Two" ) ) )
4290 .thenCompose ( v -> s .persist ( new GuineaPig (33 , "Three" ) ) )
43- .thenCompose ( v -> s .createQuery ("select count(*) from GuineaPig" )
44- .getSingleResult ()
45- .thenAccept ( count -> context .assertEquals ( 3L , count ) )
91+ // Auto-flush
92+ .thenCompose ( v -> s .createQuery ("select name from GuineaPig" )
93+ .getResultList ()
94+ .thenAccept ( names -> {
95+ assertThat ( names ).containsExactlyInAnyOrder ( "One" , "Two" , "Three" );
96+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
97+ // Parameters are different for different dbs, so we cannot do an exact match
98+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) )
99+ .startsWith ( "insert into pig (name, version, id) values " );
100+ sqlTracker .clear ();
101+ } )
46102 )
47103 )
48104 .thenCompose ( v -> openSession () )
@@ -51,15 +107,27 @@ public void testBatching(TestContext context) {
51107 .thenAccept ( list -> list .forEach ( pig -> pig .setName ("Zero" ) ) )
52108 .thenCompose ( v -> s .<Long >createQuery ("select count(*) from GuineaPig where name='Zero'" )
53109 .getSingleResult ()
54- .thenAccept ( count -> context .assertEquals ( 3L , count ) )
110+ .thenAccept ( count -> {
111+ context .assertEquals ( 3L , count );
112+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
113+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) )
114+ .matches ( "update pig set name=.+, version=.+ where id=.+ and version=.+" );
115+ sqlTracker .clear ();
116+ } )
55117 ) )
56118 .thenCompose ( v -> openSession () )
57119 .thenCompose ( s -> s .<GuineaPig >createQuery ("from GuineaPig" )
58120 .getResultList ()
59121 .thenCompose ( list -> loop ( list , s ::remove ) )
60122 .thenCompose ( v -> s .<Long >createQuery ("select count(*) from GuineaPig" )
61123 .getSingleResult ()
62- .thenAccept ( count -> context .assertEquals ( 0L , count ) )
124+ .thenAccept ( count -> {
125+ context .assertEquals ( 0L , count );
126+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
127+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) )
128+ .matches ( "delete from pig where id=.+ and version=.+" );
129+ sqlTracker .clear ();
130+ } )
63131 )
64132 )
65133 );
0 commit comments