HHH-14901 Introduce a new BatchBuilder implementation, suitable for immutable code deployments

This commit is contained in:
Sanne Grinovero 2021-10-26 16:58:32 +01:00 committed by Sanne Grinovero
parent 7f71dca1ae
commit cc7498cf3c
2 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,39 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.engine.jdbc.batch.internal;
import org.hibernate.engine.jdbc.batch.spi.Batch;
import org.hibernate.engine.jdbc.batch.spi.BatchBuilder;
import org.hibernate.engine.jdbc.batch.spi.BatchKey;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
/**
* Simplified version of BatchBuilderImpl which does not support
* changing the configured jdbc Batch size at runtime and is
* not exposed via JMX.
* @author Sanne Grinovero
*/
final class UnmodifiableBatchBuilderImpl implements BatchBuilder {
private final int jdbcBatchSize;
public UnmodifiableBatchBuilderImpl(int jdbcBatchSize) {
this.jdbcBatchSize = jdbcBatchSize;
}
@Override
public Batch buildBatch(BatchKey key, JdbcCoordinator jdbcCoordinator) {
final Integer sessionJdbcBatchSize = jdbcCoordinator.getJdbcSessionOwner()
.getJdbcBatchSize();
final int jdbcBatchSizeToUse = sessionJdbcBatchSize == null ?
this.jdbcBatchSize :
sessionJdbcBatchSize;
return jdbcBatchSizeToUse > 1
? new BatchingBatch( key, jdbcCoordinator, jdbcBatchSizeToUse )
: new NonBatchingBatch( key, jdbcCoordinator );
}
}

View File

@ -0,0 +1,52 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.engine.jdbc.batch.internal;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.batch.spi.BatchBuilder;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* Initiator for the {@link UnmodifiableBatchBuilderImpl} service using
* {@link UnmodifiableBatchBuilderImpl}.
* This is not the default implementation, but it's a useful alternative to have
* in some environments.
*
* @author Sanne Grinovero
*/
public final class UnmodifiableBatchBuilderInitiator implements StandardServiceInitiator<BatchBuilder> {
/**
* Singleton access
*/
public static final UnmodifiableBatchBuilderInitiator INSTANCE = new UnmodifiableBatchBuilderInitiator();
@Override
public Class<BatchBuilder> getServiceInitiated() {
return BatchBuilder.class;
}
@Override
public BatchBuilder initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object builder = configurationValues.get( BatchBuilderInitiator.BUILDER );
if ( builder == null ) {
return new UnmodifiableBatchBuilderImpl(
ConfigurationHelper.getInt( Environment.STATEMENT_BATCH_SIZE, configurationValues, 1 )
);
}
else {
throw new ServiceException( "This Hibernate ORM serviceregistry has been configured explicitly to use " + this.getClass() +
" to create BatchBuilder instances; the property '" + BatchBuilderInitiator.BUILDER
+ "' is not supported." );
}
}
}