1010import jakarta .persistence .FetchType ;
1111import jakarta .persistence .GeneratedValue ;
1212import jakarta .persistence .Id ;
13+ import org .hibernate .dialect .Dialect ;
1314import org .hibernate .stat .spi .StatisticsImplementor ;
1415import org .hibernate .testing .orm .junit .DomainModel ;
1516import org .hibernate .testing .orm .junit .ServiceRegistry ;
2021
2122import java .util .ArrayList ;
2223import java .util .List ;
24+ import java .util .Locale ;
2325
2426import static org .hibernate .cfg .StatisticsSettings .GENERATE_STATISTICS ;
2527import static org .hibernate .graph .GraphSemantic .FETCH ;
3234public class StatelessSessionStatisticsTest {
3335 @ Test
3436 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+
3642 assertEquals (0 , statistics .getEntityInsertCount ());
3743 assertEquals (0 , statistics .getEntityUpdateCount ());
3844 assertEquals (0 , statistics .getEntityDeleteCount ());
3945 assertEquals (0 , statistics .getEntityLoadCount ());
46+ assertEquals (0 , statistics .getPrepareStatementCount ());
4047 Person person = new Person ();
4148 person .name = "Gavin" ;
4249 person .handles .add ("@1ovthafew" );
4350 scope .inStatelessTransaction (s -> s .insert (person ));
4451 assertEquals (1 , statistics .getEntityInsertCount ());
4552 assertEquals (1 , statistics .getCollectionRecreateCount ());
53+ assertEquals (stmtCount , statistics .getPrepareStatementCount ());
54+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
4655 scope .inStatelessSession (s -> s .get (Person .class , person .id ));
4756 assertEquals (1 , statistics .getEntityLoadCount ());
4857 assertEquals (0 , statistics .getEntityFetchCount ());
4958 assertEquals (1 , statistics .getCollectionLoadCount ());
5059 assertEquals (0 , statistics .getCollectionFetchCount ());
60+ assertEquals (++stmtCount , statistics .getPrepareStatementCount ());
61+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
5162 person .name = "Gavin King" ;
5263 scope .inStatelessTransaction (s -> s .update (person ));
5364 assertEquals (1 , statistics .getEntityUpdateCount ());
@@ -56,25 +67,37 @@ void test(SessionFactoryScope scope) {
5667 assertEquals (2 , statistics .getEntityLoadCount ());
5768 assertEquals (2 , statistics .getCollectionLoadCount ());
5869 assertEquals (0 , statistics .getCollectionFetchCount ());
70+ assertEquals (stmtCount +=4 , statistics .getPrepareStatementCount ());
71+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
5972 scope .inStatelessSession (s -> s .get (s .createEntityGraph (Person .class ), FETCH , person .id ));
6073 assertEquals (3 , statistics .getEntityLoadCount ());
6174 assertEquals (2 , statistics .getCollectionLoadCount ());
6275 assertEquals (0 , statistics .getCollectionFetchCount ());
76+ assertEquals (++stmtCount , statistics .getPrepareStatementCount ());
77+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
6378 scope .inStatelessSession (s -> s .fetch (s .get (s .createEntityGraph (Person .class ), FETCH , person .id ).handles ));
6479 assertEquals (4 , statistics .getEntityLoadCount ());
6580 assertEquals (3 , statistics .getCollectionLoadCount ());
6681 assertEquals (1 , statistics .getCollectionFetchCount ());
82+ assertEquals (stmtCount +=2 , statistics .getPrepareStatementCount ());
83+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
6784 scope .inStatelessSession (s -> s .createQuery ("from Person" , Person .class ).getSingleResult ());
6885 assertEquals (5 , statistics .getEntityLoadCount ());
6986 assertEquals (4 , statistics .getCollectionLoadCount ());
7087 assertEquals (2 , statistics .getCollectionFetchCount ());
88+ assertEquals (stmtCount +=2 , statistics .getPrepareStatementCount ());
89+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
7190 person .handles .add ("hello world" );
7291 scope .inStatelessTransaction (s -> s .upsert (person ));
7392 assertEquals (2 , statistics .getCollectionUpdateCount ());
93+ assertEquals (stmtCount +=4 , statistics .getPrepareStatementCount ());
94+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
7495 scope .inStatelessTransaction (s -> s .delete (person ));
7596 assertEquals (1 , statistics .getEntityDeleteCount ());
7697 assertEquals (1 , statistics .getCollectionRemoveCount ());
7798 assertEquals (4 , statistics .getTransactionCount ());
99+ assertEquals (stmtCount +=2 , statistics .getPrepareStatementCount ());
100+ assertEquals (stmtCount , statistics .getCloseStatementCount ());
78101 }
79102
80103 @ Entity (name ="Person" )
0 commit comments