From 0c8e3056dcb2beab31e62839667ddf6584e8c33a Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Sun, 15 Dec 2019 16:16:40 -0500 Subject: [PATCH] HHH-13783 avoid SequenceMismatchStrategy checking for non-pooled optimizer --- .../id/enhanced/SequenceStyleGenerator.java | 9 +- .../HiLoSequenceMismatchStrategyTest.java | 105 ++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/idgen/enhanced/sequence/HiLoSequenceMismatchStrategyTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java index f07a7cc9c2..2996ad104e 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java @@ -238,7 +238,11 @@ public class SequenceStyleGenerator final int initialValue = determineInitialValue( params ); int incrementSize = determineIncrementSize( params ); - if ( isPhysicalSequence( jdbcEnvironment, forceTableUse ) ) { + final String optimizationStrategy = determineOptimizationStrategy( params, incrementSize ); + + final boolean isPooledOptimizer = OptimizerFactory.isPooledOptimizer( optimizationStrategy ); + + if ( isPooledOptimizer && isPhysicalSequence( jdbcEnvironment, forceTableUse ) ) { String databaseSequenceName = sequenceName.getObjectName().getText(); Long databaseIncrementValue = getSequenceIncrementValue( jdbcEnvironment, databaseSequenceName ); @@ -269,11 +273,10 @@ public class SequenceStyleGenerator } } - final String optimizationStrategy = determineOptimizationStrategy( params, incrementSize ); incrementSize = determineAdjustedIncrementSize( optimizationStrategy, incrementSize ); if ( dialect.supportsSequences() && !forceTableUse ) { - if ( !dialect.supportsPooledSequences() && OptimizerFactory.isPooledOptimizer( optimizationStrategy ) ) { + if ( !dialect.supportsPooledSequences() && isPooledOptimizer ) { forceTableUse = true; LOG.forcingTableUse(); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/idgen/enhanced/sequence/HiLoSequenceMismatchStrategyTest.java b/hibernate-core/src/test/java/org/hibernate/test/idgen/enhanced/sequence/HiLoSequenceMismatchStrategyTest.java new file mode 100644 index 0000000000..e2e723c9fa --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/idgen/enhanced/sequence/HiLoSequenceMismatchStrategyTest.java @@ -0,0 +1,105 @@ +/* + * 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 . + */ +package org.hibernate.test.idgen.enhanced.sequence; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; +import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; +import org.hibernate.id.SequenceMismatchStrategy; +import org.hibernate.id.enhanced.HiLoOptimizer; +import org.hibernate.id.enhanced.SequenceStyleGenerator; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.AfterClassOnce; +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Assert; +import org.junit.Test; +import static org.hibernate.testing.junit4.ExtraAssertions.assertClassAssignability; +import static org.junit.Assert.fail; + +/** + * @author Nathan Xu + */ +@TestForIssue( jiraKey = "HHH-13783" ) +@RequiresDialectFeature( DialectChecks.SupportsSequences.class ) +public class HiLoSequenceMismatchStrategyTest extends BaseCoreFunctionalTestCase { + + private String sequenceName = "ID_SEQ_HILO_SEQ"; + + @Override + protected String[] getMappings() { + return new String[] { "idgen/enhanced/sequence/HiLo.hbm.xml" }; + } + + @Override + protected void configure(Configuration configuration) { + configuration.setProperty(AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY, SequenceMismatchStrategy.EXCEPTION.toString()); + } + + @Override + protected void afterConfigurationBuilt(Configuration configuration) { + DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl(); + connectionProvider.configure( Environment.getProperties() ); + + String[] dropSequenceStatements = getDialect().getDropSequenceStrings( sequenceName ); + String[] createSequenceStatements = getDialect().getCreateSequenceStrings( sequenceName, 1, 1 ); + + try ( Connection connection = connectionProvider.getConnection(); + Statement statement = connection.createStatement() ) { + for ( String dropSequenceStatement : dropSequenceStatements ) { + statement.execute( dropSequenceStatement ); + } + for ( String createSequenceStatement : createSequenceStatements ) { + statement.execute( createSequenceStatement ); + } + } + catch (SQLException e) { + fail( e.getMessage() ); + } + } + + @AfterClassOnce + @SuppressWarnings( {"UnusedDeclaration"}) + protected void releaseSessionFactory() { + DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl(); + connectionProvider.configure( Environment.getProperties() ); + + String[] dropSequenceStatements = getDialect().getDropSequenceStrings( sequenceName ); + try ( Connection connection = connectionProvider.getConnection(); + Statement statement = connection.createStatement() ) { + for ( String dropSequenceStatement : dropSequenceStatements ) { + statement.execute( dropSequenceStatement ); + } + } + catch (SQLException e) { + fail( e.getMessage() ); + } + super.releaseSessionFactory(); + } + + @Test + public void testSequenceMismatchStrategyNotApplied() { + EntityPersister persister = sessionFactory().getEntityPersister( Entity.class.getName() ); + assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); + + SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); + assertClassAssignability( HiLoOptimizer.class, generator.getOptimizer().getClass() ); + + String sequenceName = generator.getDatabaseStructure().getName(); + Assert.assertEquals(this.sequenceName, sequenceName); + + int incrementSize = generator.getOptimizer().getIncrementSize(); + Assert.assertNotEquals(1, incrementSize); + } + +}