Skip to content

Commit cb0d703

Browse files
committed
HHH-17989 - Fix for StatisticsImplementor.closeStatement() never called
Signed-off-by: Jan Schatteman <[email protected]>
1 parent ddf9362 commit cb0d703

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.hibernate.mapping.PrimaryKey;
4848
import org.hibernate.mapping.Table;
4949
import org.hibernate.service.ServiceRegistry;
50+
import org.hibernate.stat.spi.StatisticsImplementor;
5051
import org.hibernate.type.BasicTypeRegistry;
5152
import org.hibernate.type.StandardBasicTypes;
5253
import org.hibernate.type.Type;
@@ -624,13 +625,20 @@ private PreparedStatement prepareStatement(
624625
logger.logStatement( sql, FormatStyle.BASIC.getFormatter() );
625626
final EventManager eventManager = session.getEventManager();
626627
final HibernateMonitoringEvent creationEvent = eventManager.beginJdbcPreparedStatementCreationEvent();
628+
final StatisticsImplementor stats = session.getFactory().getStatistics();
627629
try {
628630
listener.jdbcPrepareStatementStart();
631+
if ( stats != null && stats.isStatisticsEnabled() ) {
632+
stats.prepareStatement();
633+
}
629634
return connection.prepareStatement( sql );
630635
}
631636
finally {
632637
eventManager.completeJdbcPreparedStatementCreationEvent( creationEvent, sql );
633638
listener.jdbcPrepareStatementEnd();
639+
if ( stats != null && stats.isStatisticsEnabled() ) {
640+
stats.closeStatement();
641+
}
634642
}
635643
}
636644

hibernate-core/src/main/java/org/hibernate/id/enhanced/TableStructure.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.hibernate.internal.CoreMessageLogger;
3232
import org.hibernate.jdbc.AbstractReturningWork;
3333
import org.hibernate.mapping.Table;
34+
import org.hibernate.stat.spi.StatisticsImplementor;
3435
import org.hibernate.type.StandardBasicTypes;
3536

3637
import org.jboss.logging.Logger;
@@ -231,13 +232,20 @@ private PreparedStatement prepareStatement(
231232
logger.logStatement( sql, FormatStyle.BASIC.getFormatter() );
232233
final EventManager eventManager = session.getEventManager();
233234
final HibernateMonitoringEvent creationEvent = eventManager.beginJdbcPreparedStatementCreationEvent();
235+
final StatisticsImplementor stats = session.getFactory().getStatistics();
234236
try {
235237
statsCollector.jdbcPrepareStatementStart();
238+
if ( stats != null && stats.isStatisticsEnabled() ) {
239+
stats.prepareStatement();
240+
}
236241
return connection.prepareStatement( sql );
237242
}
238243
finally {
239244
eventManager.completeJdbcPreparedStatementCreationEvent( creationEvent, sql );
240245
statsCollector.jdbcPrepareStatementEnd();
246+
if ( stats != null && stats.isStatisticsEnabled() ) {
247+
stats.closeStatement();
248+
}
241249
}
242250
}
243251

hibernate-core/src/main/java/org/hibernate/resource/jdbc/spi/JdbcEventHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public void jdbcPrepareStatementStart() {
8080
if ( sessionListener != null ) {
8181
sessionListener.jdbcPrepareStatementStart();
8282
}
83+
84+
if ( statistics != null && statistics.isStatisticsEnabled() ) {
85+
statistics.prepareStatement();
86+
}
8387
}
8488

8589
public void jdbcPrepareStatementEnd() {
@@ -88,7 +92,7 @@ public void jdbcPrepareStatementEnd() {
8892
}
8993

9094
if ( statistics != null && statistics.isStatisticsEnabled() ) {
91-
statistics.prepareStatement();
95+
statistics.closeStatement();
9296
}
9397
}
9498

hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionStatisticsTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import jakarta.persistence.FetchType;
1111
import jakarta.persistence.GeneratedValue;
1212
import jakarta.persistence.Id;
13+
import org.hibernate.dialect.Dialect;
1314
import org.hibernate.stat.spi.StatisticsImplementor;
1415
import org.hibernate.testing.orm.junit.DomainModel;
1516
import org.hibernate.testing.orm.junit.ServiceRegistry;
@@ -20,6 +21,7 @@
2021

2122
import java.util.ArrayList;
2223
import java.util.List;
24+
import java.util.Locale;
2325

2426
import static org.hibernate.cfg.StatisticsSettings.GENERATE_STATISTICS;
2527
import static org.hibernate.graph.GraphSemantic.FETCH;
@@ -32,22 +34,31 @@
3234
public 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")

hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatisticsWithNoCachingTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
@DomainModel
2222
@SessionFactory
2323
@ServiceRegistry(
24-
settingProviders = @SettingProvider(provider = StatisticsWithNoCachingTest.RegionFacrotySettingProvider.class, settingName = AvailableSettings.CACHE_REGION_FACTORY)
24+
settingProviders = @SettingProvider(provider = StatisticsWithNoCachingTest.RegionFactorySettingProvider.class, settingName = AvailableSettings.CACHE_REGION_FACTORY)
2525
)
2626
public class StatisticsWithNoCachingTest {
2727

28-
public static class RegionFacrotySettingProvider implements SettingProvider.Provider<String> {
28+
public static class RegionFactorySettingProvider implements SettingProvider.Provider<String> {
2929

3030
@Override
3131
public String getSetting() {

0 commit comments

Comments
 (0)