From aef0e2540545559787108a81e5a0fdb59f3351e9 Mon Sep 17 00:00:00 2001 From: Scott Marlow Date: Fri, 20 Jan 2012 18:22:52 -0500 Subject: [PATCH] HHH-6984 only increment statistics if statistics are enabled --- .../lock/PessimisticReadUpdateLockingStrategy.java | 8 ++++---- .../lock/PessimisticWriteUpdateLockingStrategy.java | 8 ++++---- .../dialect/lock/UpdateLockingStrategy.java | 4 +++- .../internal/TransactionCoordinatorImpl.java | 4 +++- .../internal/ConnectionObserverStatsBridge.java | 8 ++++++-- .../test/common/TransactionEnvironmentImpl.java | 13 +++++++++++-- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticReadUpdateLockingStrategy.java b/hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticReadUpdateLockingStrategy.java index 60a96df95b..a1463b0845 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticReadUpdateLockingStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticReadUpdateLockingStrategy.java @@ -27,12 +27,9 @@ import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.SQLException; -import org.jboss.logging.Logger; - import org.hibernate.HibernateException; import org.hibernate.JDBCException; import org.hibernate.LockMode; -import org.hibernate.PessimisticLockException; import org.hibernate.StaleObjectStateException; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; @@ -40,6 +37,7 @@ import org.hibernate.internal.CoreMessageLogger; import org.hibernate.persister.entity.Lockable; import org.hibernate.pretty.MessageHelper; import org.hibernate.sql.Update; +import org.jboss.logging.Logger; /** * A pessimistic locking strategy where the locks are obtained through update statements. @@ -107,7 +105,9 @@ public class PessimisticReadUpdateLockingStrategy implements LockingStrategy { int affected = st.executeUpdate(); if ( affected < 0 ) { // todo: should this instead check for exactly one row modified? - factory.getStatisticsImplementor().optimisticFailure( lockable.getEntityName() ); + if (factory.getStatistics().isStatisticsEnabled()) { + factory.getStatisticsImplementor().optimisticFailure( lockable.getEntityName() ); + } throw new StaleObjectStateException( lockable.getEntityName(), id ); } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteUpdateLockingStrategy.java b/hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteUpdateLockingStrategy.java index c9838fa832..6fc57712d1 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteUpdateLockingStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteUpdateLockingStrategy.java @@ -27,12 +27,9 @@ import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.SQLException; -import org.jboss.logging.Logger; - import org.hibernate.HibernateException; import org.hibernate.JDBCException; import org.hibernate.LockMode; -import org.hibernate.PessimisticLockException; import org.hibernate.StaleObjectStateException; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; @@ -40,6 +37,7 @@ import org.hibernate.internal.CoreMessageLogger; import org.hibernate.persister.entity.Lockable; import org.hibernate.pretty.MessageHelper; import org.hibernate.sql.Update; +import org.jboss.logging.Logger; /** * A pessimistic locking strategy where the locks are obtained through update statements. @@ -106,7 +104,9 @@ public class PessimisticWriteUpdateLockingStrategy implements LockingStrategy { int affected = st.executeUpdate(); if ( affected < 0 ) { // todo: should this instead check for exactly one row modified? - factory.getStatisticsImplementor().optimisticFailure( lockable.getEntityName() ); + if (factory.getStatistics().isStatisticsEnabled()) { + factory.getStatisticsImplementor().optimisticFailure( lockable.getEntityName() ); + } throw new StaleObjectStateException( lockable.getEntityName(), id ); } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/lock/UpdateLockingStrategy.java b/hibernate-core/src/main/java/org/hibernate/dialect/lock/UpdateLockingStrategy.java index b38b96df46..10b7fd2986 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/lock/UpdateLockingStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/lock/UpdateLockingStrategy.java @@ -108,7 +108,9 @@ public class UpdateLockingStrategy implements LockingStrategy { int affected = st.executeUpdate(); if ( affected < 0 ) { - factory.getStatisticsImplementor().optimisticFailure( lockable.getEntityName() ); + if (factory.getStatistics().isStatisticsEnabled()) { + factory.getStatisticsImplementor().optimisticFailure( lockable.getEntityName() ); + } throw new StaleObjectStateException( lockable.getEntityName(), id ); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionCoordinatorImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionCoordinatorImpl.java index e8d896b1fa..0c025c68f5 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionCoordinatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionCoordinatorImpl.java @@ -137,7 +137,9 @@ public class TransactionCoordinatorImpl implements TransactionCoordinator { final boolean success = JtaStatusHelper.isCommitted( status ); - transactionEnvironment.getStatisticsImplementor().endTransaction( success ); + if (sessionFactory().getStatistics().isStatisticsEnabled()) { + transactionEnvironment.getStatisticsImplementor().endTransaction( success ); + } getJdbcCoordinator().afterTransaction(); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/ConnectionObserverStatsBridge.java b/hibernate-core/src/main/java/org/hibernate/internal/ConnectionObserverStatsBridge.java index e1f22af46d..eb6775d2ef 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/ConnectionObserverStatsBridge.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/ConnectionObserverStatsBridge.java @@ -41,7 +41,9 @@ public class ConnectionObserverStatsBridge implements ConnectionObserver, Serial @Override public void physicalConnectionObtained(Connection connection) { - sessionFactory.getStatisticsImplementor().connect(); + if (sessionFactory.getStatistics().isStatisticsEnabled()) { + sessionFactory.getStatisticsImplementor().connect(); + } } @Override @@ -54,6 +56,8 @@ public class ConnectionObserverStatsBridge implements ConnectionObserver, Serial @Override public void statementPrepared() { - sessionFactory.getStatisticsImplementor().prepareStatement(); + if (sessionFactory.getStatistics().isStatisticsEnabled()) { + sessionFactory.getStatisticsImplementor().prepareStatement(); + } } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/common/TransactionEnvironmentImpl.java b/hibernate-core/src/test/java/org/hibernate/test/common/TransactionEnvironmentImpl.java index 453c3885e9..41e8f596a4 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/common/TransactionEnvironmentImpl.java +++ b/hibernate-core/src/test/java/org/hibernate/test/common/TransactionEnvironmentImpl.java @@ -23,7 +23,8 @@ */ package org.hibernate.test.common; -import org.hibernate.cfg.NotYetImplementedException; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Configuration; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.transaction.spi.TransactionEnvironment; @@ -39,14 +40,22 @@ import org.hibernate.stat.spi.StatisticsImplementor; public class TransactionEnvironmentImpl implements TransactionEnvironment { private final ServiceRegistry serviceRegistry; private final ConcurrentStatisticsImpl statistics = new ConcurrentStatisticsImpl(); + private final SessionFactoryImplementor sessionFactory; + + public static final String NAME = "TransactionEnvironmentImpl_testSF"; public TransactionEnvironmentImpl(ServiceRegistry serviceRegistry) { this.serviceRegistry = serviceRegistry; + + Configuration cfg = new Configuration() + .setProperty( AvailableSettings.SESSION_FACTORY_NAME, NAME ) + .setProperty( AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, "false" ); // default is true + sessionFactory = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry); } @Override public SessionFactoryImplementor getSessionFactory() { - throw new NotYetImplementedException( "Not available in this context" ); + return sessionFactory; } @Override