From bb60d170700178f783b79846f329de80bced34aa Mon Sep 17 00:00:00 2001 From: gtoison Date: Sun, 27 Oct 2024 08:25:23 +0100 Subject: [PATCH 1/3] HHH-18770 test with the jfr module on the classpath but jfr disabled --- .../jfr/flush/AutoFlushJfrDisabledTests.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 hibernate-jfr/src/test/java/org/hibernate/event/jfr/flush/AutoFlushJfrDisabledTests.java diff --git a/hibernate-jfr/src/test/java/org/hibernate/event/jfr/flush/AutoFlushJfrDisabledTests.java b/hibernate-jfr/src/test/java/org/hibernate/event/jfr/flush/AutoFlushJfrDisabledTests.java new file mode 100644 index 000000000000..6ccc5bcd01f2 --- /dev/null +++ b/hibernate-jfr/src/test/java/org/hibernate/event/jfr/flush/AutoFlushJfrDisabledTests.java @@ -0,0 +1,60 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.event.jfr.flush; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + + +@DomainModel(annotatedClasses = { + AutoFlushJfrDisabledTests.TestEntity.class, +}) +@SessionFactory +@JiraKey(value = "HHH-18770") +public class AutoFlushJfrDisabledTests { + + /** + * Execute a query (and a flush) with the jfr module on the classpath but jfr disabled + */ + @Test + public void testFlushEventWithPartialFlushEventDisabled(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + TestEntity entity = new TestEntity( 1, "name_1" ); + session.persist( entity ); + session.createQuery( "select t from TestEntity t" ).list(); + + session.remove( entity ); + } + ); + } + + @Entity(name = "TestEntity") + public static class TestEntity { + @Id + private Integer id; + + private String name; + + public TestEntity() { + } + + public TestEntity(Integer id, String name) { + this.id = id; + this.name = name; + } + + public void setName(String name) { + this.name = name; + } + } + +} From ee786bd31a0b3058e4175093be5b7ef94259dd8f Mon Sep 17 00:00:00 2001 From: gtoison Date: Sun, 27 Oct 2024 08:27:25 +0100 Subject: [PATCH 2/3] HHH-18770 Check if the monitoring event was null When the JFR partial flush event is disabled (for instance when JFR is entirely disabled) beginPartialFlushEvent() will return a null event. --- .../java/org/hibernate/event/jfr/internal/JfrEventManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java b/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java index e4586ba26818..3d3f7f30fc5b 100644 --- a/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java +++ b/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java @@ -479,7 +479,7 @@ public PartialFlushEvent beginPartialFlushEvent() { public void completePartialFlushEvent( HibernateMonitoringEvent hibernateMonitoringEvent, AutoFlushEvent event) { - if ( event != null ) { + if ( event != null && hibernateMonitoringEvent != null) { final PartialFlushEvent flushEvent = (PartialFlushEvent) hibernateMonitoringEvent; flushEvent.end(); if ( flushEvent.shouldCommit() ) { From a414bf3da47a1a617526a79aea204309cf5c79d6 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 29 Oct 2024 10:17:04 +0100 Subject: [PATCH 3/3] HHH-18770 Check if the monitoring event was null --- .../event/jfr/internal/JfrEventManager.java | 109 +++++++++--------- 1 file changed, 54 insertions(+), 55 deletions(-) diff --git a/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java b/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java index 3d3f7f30fc5b..950f96367925 100644 --- a/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java +++ b/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java @@ -58,10 +58,10 @@ public SessionOpenEvent beginSessionOpenEvent() { @Override public void completeSessionOpenEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session) { - if ( event != null ) { - final SessionOpenEvent sessionOpenEvent = (SessionOpenEvent) event; + if ( monitoringEvent != null ) { + final SessionOpenEvent sessionOpenEvent = (SessionOpenEvent) monitoringEvent; sessionOpenEvent.end(); if ( sessionOpenEvent.shouldCommit() ) { sessionOpenEvent.sessionIdentifier = getSessionIdentifier( session ); @@ -84,10 +84,10 @@ public SessionClosedEvent beginSessionClosedEvent() { @Override public void completeSessionClosedEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session) { - if ( event != null ) { - final SessionClosedEvent sessionClosedEvent = (SessionClosedEvent) event; + if ( monitoringEvent != null ) { + final SessionClosedEvent sessionClosedEvent = (SessionClosedEvent) monitoringEvent; sessionClosedEvent.end(); if ( sessionClosedEvent.shouldCommit() ) { sessionClosedEvent.sessionIdentifier = getSessionIdentifier( session ); @@ -111,11 +111,11 @@ public JdbcConnectionAcquisitionEvent beginJdbcConnectionAcquisitionEvent() { @Override public void completeJdbcConnectionAcquisitionEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, Object tenantId) { - if ( event != null ) { - final JdbcConnectionAcquisitionEvent jdbcConnectionAcquisitionEvent = (JdbcConnectionAcquisitionEvent) event; + if ( monitoringEvent != null ) { + final JdbcConnectionAcquisitionEvent jdbcConnectionAcquisitionEvent = (JdbcConnectionAcquisitionEvent) monitoringEvent; jdbcConnectionAcquisitionEvent.end(); if ( jdbcConnectionAcquisitionEvent.shouldCommit() ) { jdbcConnectionAcquisitionEvent.executionTime = getExecutionTime( jdbcConnectionAcquisitionEvent.startedAt ); @@ -143,11 +143,11 @@ public JdbcConnectionReleaseEvent beginJdbcConnectionReleaseEvent() { @Override public void completeJdbcConnectionReleaseEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, Object tenantId) { - if ( event != null ) { - final JdbcConnectionReleaseEvent jdbcConnectionReleaseEvent = (JdbcConnectionReleaseEvent) event; + if ( monitoringEvent != null ) { + final JdbcConnectionReleaseEvent jdbcConnectionReleaseEvent = (JdbcConnectionReleaseEvent) monitoringEvent; jdbcConnectionReleaseEvent.end(); if ( jdbcConnectionReleaseEvent.shouldCommit() ) { jdbcConnectionReleaseEvent.executionTime = getExecutionTime( jdbcConnectionReleaseEvent.startedAt ); @@ -175,10 +175,10 @@ public JdbcPreparedStatementCreationEvent beginJdbcPreparedStatementCreationEven @Override public void completeJdbcPreparedStatementCreationEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, String preparedStatementSql) { - if ( event != null ) { - final JdbcPreparedStatementCreationEvent jdbcPreparedStatementCreation = (JdbcPreparedStatementCreationEvent) event; + if ( monitoringEvent != null ) { + final JdbcPreparedStatementCreationEvent jdbcPreparedStatementCreation = (JdbcPreparedStatementCreationEvent) monitoringEvent; jdbcPreparedStatementCreation.end(); if ( jdbcPreparedStatementCreation.shouldCommit() ) { jdbcPreparedStatementCreation.executionTime = getExecutionTime( jdbcPreparedStatementCreation.startedAt ); @@ -203,10 +203,10 @@ public JdbcPreparedStatementExecutionEvent beginJdbcPreparedStatementExecutionEv @Override public void completeJdbcPreparedStatementExecutionEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, String preparedStatementSql) { - if ( event != null ) { - final JdbcPreparedStatementExecutionEvent jdbcPreparedStatementExecutionEvent = (JdbcPreparedStatementExecutionEvent) event; + if ( monitoringEvent != null ) { + final JdbcPreparedStatementExecutionEvent jdbcPreparedStatementExecutionEvent = (JdbcPreparedStatementExecutionEvent) monitoringEvent; jdbcPreparedStatementExecutionEvent.end(); if ( jdbcPreparedStatementExecutionEvent.shouldCommit() ) { jdbcPreparedStatementExecutionEvent.executionTime = getExecutionTime( @@ -232,10 +232,10 @@ public JdbcBatchExecutionEvent beginJdbcBatchExecutionEvent() { @Override public void completeJdbcBatchExecutionEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, String statementSql) { - if ( event != null ) { - final JdbcBatchExecutionEvent jdbcBatchExecutionEvent = (JdbcBatchExecutionEvent) event; + if ( monitoringEvent != null ) { + final JdbcBatchExecutionEvent jdbcBatchExecutionEvent = (JdbcBatchExecutionEvent) monitoringEvent; jdbcBatchExecutionEvent.end(); if ( jdbcBatchExecutionEvent.shouldCommit() ) { jdbcBatchExecutionEvent.executionTime = getExecutionTime( jdbcBatchExecutionEvent.startedAt ); @@ -260,13 +260,13 @@ public HibernateMonitoringEvent beginCachePutEvent() { @Override public void completeCachePutEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, Region region, boolean cacheContentChanged, CacheActionDescription description) { - if ( event != null ) { - final CachePutEvent cachePutEvent = (CachePutEvent) event; + if ( monitoringEvent != null ) { + final CachePutEvent cachePutEvent = (CachePutEvent) monitoringEvent; cachePutEvent.end(); if ( cachePutEvent.shouldCommit() ) { cachePutEvent.executionTime = getExecutionTime( cachePutEvent.startedAt ); @@ -281,14 +281,14 @@ public void completeCachePutEvent( @Override public void completeCachePutEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, CachedDomainDataAccess cachedDomainDataAccess, EntityPersister persister, boolean cacheContentChanged, CacheActionDescription description) { completeCachePutEvent( - event, + monitoringEvent, session, cachedDomainDataAccess, persister, @@ -300,15 +300,15 @@ public void completeCachePutEvent( @Override public void completeCachePutEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, CachedDomainDataAccess cachedDomainDataAccess, EntityPersister persister, boolean cacheContentChanged, boolean isNatualId, CacheActionDescription description) { - if ( event != null ) { - final CachePutEvent cachePutEvent = (CachePutEvent) event; + if ( monitoringEvent != null ) { + final CachePutEvent cachePutEvent = (CachePutEvent) monitoringEvent; cachePutEvent.end(); if ( cachePutEvent.shouldCommit() ) { cachePutEvent.executionTime = getExecutionTime( cachePutEvent.startedAt ); @@ -325,14 +325,14 @@ public void completeCachePutEvent( @Override public void completeCachePutEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, CachedDomainDataAccess cachedDomainDataAccess, CollectionPersister persister, boolean cacheContentChanged, CacheActionDescription description) { - if ( event != null ) { - final CachePutEvent cachePutEvent = (CachePutEvent) event; + if ( monitoringEvent != null ) { + final CachePutEvent cachePutEvent = (CachePutEvent) monitoringEvent; cachePutEvent.end(); if ( cachePutEvent.shouldCommit() ) { cachePutEvent.executionTime = getExecutionTime( cachePutEvent.startedAt ); @@ -361,12 +361,12 @@ public HibernateMonitoringEvent beginCacheGetEvent() { @Override public void completeCacheGetEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, Region region, boolean hit) { - if ( event != null ) { - final CacheGetEvent cacheGetEvent = (CacheGetEvent) event; + if ( monitoringEvent != null ) { + final CacheGetEvent cacheGetEvent = (CacheGetEvent) monitoringEvent; cacheGetEvent.end(); if ( cacheGetEvent.shouldCommit() ) { cacheGetEvent.executionTime = getExecutionTime( cacheGetEvent.startedAt ); @@ -380,14 +380,14 @@ public void completeCacheGetEvent( @Override public void completeCacheGetEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, Region region, EntityPersister persister, boolean isNaturalKey, boolean hit) { - if ( event != null ) { - final CacheGetEvent cacheGetEvent = (CacheGetEvent) event; + if ( monitoringEvent != null ) { + final CacheGetEvent cacheGetEvent = (CacheGetEvent) monitoringEvent; cacheGetEvent.end(); if ( cacheGetEvent.shouldCommit() ) { cacheGetEvent.executionTime = getExecutionTime( cacheGetEvent.startedAt ); @@ -403,13 +403,13 @@ public void completeCacheGetEvent( @Override public void completeCacheGetEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, Region region, CollectionPersister persister, boolean hit) { - if ( event != null ) { - final CacheGetEvent cacheGetEvent = (CacheGetEvent) event; + if ( monitoringEvent != null ) { + final CacheGetEvent cacheGetEvent = (CacheGetEvent) monitoringEvent; cacheGetEvent.end(); if ( cacheGetEvent.shouldCommit() ) { cacheGetEvent.executionTime = getExecutionTime( cacheGetEvent.startedAt ); @@ -444,18 +444,18 @@ public void completeFlushEvent( @Override public void completeFlushEvent( - HibernateMonitoringEvent hibernateMonitoringEvent, - org.hibernate.event.spi.FlushEvent event, + HibernateMonitoringEvent monitoringEvent, + org.hibernate.event.spi.FlushEvent hibernateFlushEvent, boolean autoFlush) { - if ( hibernateMonitoringEvent != null ) { - final FlushEvent flushEvent = (FlushEvent) hibernateMonitoringEvent; + if ( monitoringEvent != null ) { + final FlushEvent flushEvent = (FlushEvent) monitoringEvent; flushEvent.end(); if ( flushEvent.shouldCommit() ) { flushEvent.executionTime = getExecutionTime( flushEvent.startedAt ); - EventSource session = event.getSession(); + EventSource session = hibernateFlushEvent.getSession(); flushEvent.sessionIdentifier = getSessionIdentifier( session ); - flushEvent.numberOfEntitiesProcessed = event.getNumberOfEntitiesProcessed(); - flushEvent.numberOfCollectionsProcessed = event.getNumberOfCollectionsProcessed(); + flushEvent.numberOfEntitiesProcessed = hibernateFlushEvent.getNumberOfEntitiesProcessed(); + flushEvent.numberOfCollectionsProcessed = hibernateFlushEvent.getNumberOfCollectionsProcessed(); flushEvent.isAutoFlush = autoFlush; flushEvent.commit(); } @@ -466,7 +466,6 @@ public void completeFlushEvent( public PartialFlushEvent beginPartialFlushEvent() { if ( partialFlushEventType.isEnabled() ) { final PartialFlushEvent partialFlushEvent = new PartialFlushEvent(); - partialFlushEvent.startedAt = System.nanoTime(); partialFlushEvent.begin(); return partialFlushEvent; } @@ -477,10 +476,10 @@ public PartialFlushEvent beginPartialFlushEvent() { @Override public void completePartialFlushEvent( - HibernateMonitoringEvent hibernateMonitoringEvent, + HibernateMonitoringEvent monitoringEvent, AutoFlushEvent event) { - if ( event != null && hibernateMonitoringEvent != null) { - final PartialFlushEvent flushEvent = (PartialFlushEvent) hibernateMonitoringEvent; + if ( monitoringEvent != null) { + final PartialFlushEvent flushEvent = (PartialFlushEvent) monitoringEvent; flushEvent.end(); if ( flushEvent.shouldCommit() ) { flushEvent.executionTime = getExecutionTime( flushEvent.startedAt ); @@ -509,13 +508,13 @@ public DirtyCalculationEvent beginDirtyCalculationEvent() { @Override public void completeDirtyCalculationEvent( - HibernateMonitoringEvent event, + HibernateMonitoringEvent monitoringEvent, SharedSessionContractImplementor session, EntityPersister persister, EntityEntry entry, int[] dirtyProperties) { - if ( event != null ) { - final DirtyCalculationEvent dirtyCalculationEvent = (DirtyCalculationEvent) event; + if ( monitoringEvent != null ) { + final DirtyCalculationEvent dirtyCalculationEvent = (DirtyCalculationEvent) monitoringEvent; dirtyCalculationEvent.end(); if ( dirtyCalculationEvent.shouldCommit() ) { dirtyCalculationEvent.executionTime = getExecutionTime( dirtyCalculationEvent.startedAt );