10
10
import jakarta .persistence .FetchType ;
11
11
import jakarta .persistence .GeneratedValue ;
12
12
import jakarta .persistence .Id ;
13
+ import org .hibernate .dialect .Dialect ;
13
14
import org .hibernate .stat .spi .StatisticsImplementor ;
14
15
import org .hibernate .testing .orm .junit .DomainModel ;
15
16
import org .hibernate .testing .orm .junit .ServiceRegistry ;
20
21
21
22
import java .util .ArrayList ;
22
23
import java .util .List ;
24
+ import java .util .Locale ;
23
25
24
26
import static org .hibernate .cfg .StatisticsSettings .GENERATE_STATISTICS ;
25
27
import static org .hibernate .graph .GraphSemantic .FETCH ;
32
34
public class StatelessSessionStatisticsTest {
33
35
@ Test
34
36
void test (SessionFactoryScope scope ) {
35
- StatisticsImplementor statistics = scope .getSessionFactory ().getStatistics ();
37
+ final StatisticsImplementor statistics = scope .getSessionFactory ().getStatistics ();
38
+ final Dialect dialect = scope .fromSession ( session -> session .getDialect () );
39
+ final boolean isSybaseOrMysql = dialect .getClass ().getName ().toLowerCase ( Locale .ROOT ).split ( "sybase|mysql" ).length == 2 ;
40
+ int stmtCount = isSybaseOrMysql ? 4 : 3 ;
41
+
36
42
assertEquals (0 , statistics .getEntityInsertCount ());
37
43
assertEquals (0 , statistics .getEntityUpdateCount ());
38
44
assertEquals (0 , statistics .getEntityDeleteCount ());
39
45
assertEquals (0 , statistics .getEntityLoadCount ());
46
+ assertEquals (0 , statistics .getPrepareStatementCount ());
40
47
Person person = new Person ();
41
48
person .name = "Gavin" ;
42
49
person .handles .add ("@1ovthafew" );
43
50
scope .inStatelessTransaction (s -> s .insert (person ));
44
51
assertEquals (1 , statistics .getEntityInsertCount ());
45
52
assertEquals (1 , statistics .getCollectionRecreateCount ());
53
+ assertEquals (stmtCount , statistics .getPrepareStatementCount ());
54
+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
46
55
scope .inStatelessSession (s -> s .get (Person .class , person .id ));
47
56
assertEquals (1 , statistics .getEntityLoadCount ());
48
57
assertEquals (0 , statistics .getEntityFetchCount ());
49
58
assertEquals (1 , statistics .getCollectionLoadCount ());
50
59
assertEquals (0 , statistics .getCollectionFetchCount ());
60
+ assertEquals (++stmtCount , statistics .getPrepareStatementCount ());
61
+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
51
62
person .name = "Gavin King" ;
52
63
scope .inStatelessTransaction (s -> s .update (person ));
53
64
assertEquals (1 , statistics .getEntityUpdateCount ());
@@ -56,25 +67,37 @@ void test(SessionFactoryScope scope) {
56
67
assertEquals (2 , statistics .getEntityLoadCount ());
57
68
assertEquals (2 , statistics .getCollectionLoadCount ());
58
69
assertEquals (0 , statistics .getCollectionFetchCount ());
70
+ assertEquals (stmtCount +=4 , statistics .getPrepareStatementCount ());
71
+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
59
72
scope .inStatelessSession (s -> s .get (s .createEntityGraph (Person .class ), FETCH , person .id ));
60
73
assertEquals (3 , statistics .getEntityLoadCount ());
61
74
assertEquals (2 , statistics .getCollectionLoadCount ());
62
75
assertEquals (0 , statistics .getCollectionFetchCount ());
76
+ assertEquals (++stmtCount , statistics .getPrepareStatementCount ());
77
+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
63
78
scope .inStatelessSession (s -> s .fetch (s .get (s .createEntityGraph (Person .class ), FETCH , person .id ).handles ));
64
79
assertEquals (4 , statistics .getEntityLoadCount ());
65
80
assertEquals (3 , statistics .getCollectionLoadCount ());
66
81
assertEquals (1 , statistics .getCollectionFetchCount ());
82
+ assertEquals (stmtCount +=2 , statistics .getPrepareStatementCount ());
83
+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
67
84
scope .inStatelessSession (s -> s .createQuery ("from Person" , Person .class ).getSingleResult ());
68
85
assertEquals (5 , statistics .getEntityLoadCount ());
69
86
assertEquals (4 , statistics .getCollectionLoadCount ());
70
87
assertEquals (2 , statistics .getCollectionFetchCount ());
88
+ assertEquals (stmtCount +=2 , statistics .getPrepareStatementCount ());
89
+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
71
90
person .handles .add ("hello world" );
72
91
scope .inStatelessTransaction (s -> s .upsert (person ));
73
92
assertEquals (2 , statistics .getCollectionUpdateCount ());
93
+ assertEquals (stmtCount +=4 , statistics .getPrepareStatementCount ());
94
+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
74
95
scope .inStatelessTransaction (s -> s .delete (person ));
75
96
assertEquals (1 , statistics .getEntityDeleteCount ());
76
97
assertEquals (1 , statistics .getCollectionRemoveCount ());
77
98
assertEquals (4 , statistics .getTransactionCount ());
99
+ assertEquals (stmtCount +=2 , statistics .getPrepareStatementCount ());
100
+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
78
101
}
79
102
80
103
@ Entity (name ="Person" )
0 commit comments