diff --git a/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java index 12560f720d..fc2f77f0b9 100644 --- a/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java @@ -89,34 +89,35 @@ public class SequenceHiLoGeneratorTest extends BaseUnitTestCase { @Test public void testHiLoAlgorithm() { sessionImpl = (SessionImpl) sessionFactory.openSession(); + try { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // historically the hilo generators skipped the initial block of values; + // so the first generated id value is maxlo + 1, here be 4 + assertEquals(4L, generateValue()); + // which should also perform the first read on the sequence which should set it to its "start with" value (1) + assertEquals(1L, extractSequenceValue()); - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // historically the hilo generators skipped the initial block of values; - // so the first generated id value is maxlo + 1, here be 4 - assertEquals( 4L, generateValue() ); - // which should also perform the first read on the sequence which should set it to its "start with" value (1) - assertEquals( 1L, extractSequenceValue() ); + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + assertEquals(5L, generateValue()); + assertEquals(1L, extractSequenceValue()); - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - assertEquals( 5L, generateValue() ); - assertEquals( 1L, extractSequenceValue() ); + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + assertEquals(6L, generateValue()); + assertEquals(1L, extractSequenceValue()); - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - assertEquals( 6L, generateValue() ); - assertEquals( 1L, extractSequenceValue() ); + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + assertEquals(7L, generateValue()); + // unlike the newer strategies, the db value will not get update here. It gets updated on the next invocation + // afterQuery a clock over + assertEquals(1L, extractSequenceValue()); - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - assertEquals( 7L, generateValue() ); - // unlike the newer strategies, the db value will not get update here. It gets updated on the next invocation - // after a clock over - assertEquals( 1L, extractSequenceValue() ); - - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - assertEquals( 8L, generateValue() ); - // this should force an increment in the sequence value - assertEquals( 2L, extractSequenceValue() ); - - ((Session) sessionImpl).close(); + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + assertEquals(8L, generateValue()); + // this should force an increment in the sequence value + assertEquals(2L, extractSequenceValue()); + } finally { + ( (Session) sessionImpl ).close(); + } } private long extractSequenceValue() { @@ -124,10 +125,12 @@ public class SequenceHiLoGeneratorTest extends BaseUnitTestCase { } private long generateValue() { - Long generatedValue; - Transaction transaction = ((Session) sessionImpl).beginTransaction(); - generatedValue = (Long) generator.generate( sessionImpl, null ); - transaction.commit(); - return generatedValue.longValue(); + Transaction transaction = ( (Session) sessionImpl ).beginTransaction(); + try { + return (Long) generator.generate( sessionImpl, null ); + } + finally { + transaction.commit(); + } } } diff --git a/hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java b/hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java index c001134fc5..0192ee21d1 100644 --- a/hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java +++ b/hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java @@ -43,7 +43,7 @@ public class SequenceValueExtractor { queryString = "select " + sequenceName + ".currval from dual"; } else if ( dialect instanceof SQLServer2012Dialect ) { - queryString = "SELECT CONVERT(varchar(200), Current_value) FROM SYS.Sequences WHERE name = '" + sequenceName + "'"; + queryString = "SELECT CONVERT(varchar(200), Current_value) FROM sys.sequences WHERE name = '" + sequenceName + "'"; } else if ( dialect instanceof HSQLDialect ) {