5
5
*/
6
6
package org .hibernate .reactive ;
7
7
8
+ import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
8
9
import org .hibernate .cfg .AvailableSettings ;
9
10
import org .hibernate .cfg .Configuration ;
10
11
import org .hibernate .reactive .mutiny .impl .MutinySessionImpl ;
13
14
import org .hibernate .reactive .pool .impl .SqlClientConnection ;
14
15
import org .hibernate .reactive .stage .impl .StageSessionImpl ;
15
16
import org .hibernate .reactive .stage .impl .StageStatelessSessionImpl ;
17
+ import org .hibernate .reactive .testing .SqlStatementTracker ;
16
18
17
19
import org .junit .Test ;
18
20
24
26
25
27
public class BatchingConnectionTest extends ReactiveSessionTest {
26
28
29
+ private static SqlStatementTracker sqlTracker ;
30
+
27
31
@ Override
28
32
protected Configuration constructConfiguration () {
29
33
Configuration configuration = super .constructConfiguration ();
30
34
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 () );
31
39
return configuration ;
32
40
}
33
41
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
+
34
82
@ Test
35
83
public void testBatching (TestContext context ) {
36
84
test (
@@ -40,9 +88,17 @@ public void testBatching(TestContext context) {
40
88
.thenCompose ( v -> s .persist ( new GuineaPig (11 , "One" ) ) )
41
89
.thenCompose ( v -> s .persist ( new GuineaPig (22 , "Two" ) ) )
42
90
.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
+ } )
46
102
)
47
103
)
48
104
.thenCompose ( v -> openSession () )
@@ -51,15 +107,27 @@ public void testBatching(TestContext context) {
51
107
.thenAccept ( list -> list .forEach ( pig -> pig .setName ("Zero" ) ) )
52
108
.thenCompose ( v -> s .<Long >createQuery ("select count(*) from GuineaPig where name='Zero'" )
53
109
.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
+ } )
55
117
) )
56
118
.thenCompose ( v -> openSession () )
57
119
.thenCompose ( s -> s .<GuineaPig >createQuery ("from GuineaPig" )
58
120
.getResultList ()
59
121
.thenCompose ( list -> loop ( list , s ::remove ) )
60
122
.thenCompose ( v -> s .<Long >createQuery ("select count(*) from GuineaPig" )
61
123
.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
+ } )
63
131
)
64
132
)
65
133
);
0 commit comments