HHH-6984 only increment statistics if statistics are enabled

This commit is contained in:
Scott Marlow 2012-01-20 18:22:52 -05:00
parent 537481adbd
commit aef0e25405
6 changed files with 31 additions and 14 deletions

View File

@ -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 );
}

View File

@ -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 );
}

View File

@ -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 );
}

View File

@ -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();

View File

@ -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();
}
}
}

View File

@ -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