Skip to content

Commit e46956b

Browse files
istrakavladmihalcea
authored andcommitted
HHH-11456 - SequenceHiLoGeneratorTest fails due to wrong select string on SQL Server
1 parent f82dd46 commit e46956b

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

hibernate-core/src/test/java/org/hibernate/id/SequenceHiLoGeneratorTest.java

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import java.util.Properties;
1010

11-
import org.hibernate.Session;
1211
import org.hibernate.Transaction;
1312
import org.hibernate.boot.Metadata;
1413
import org.hibernate.boot.MetadataSources;
@@ -88,45 +87,48 @@ public void tearDown() throws Exception {
8887
@Test
8988
public void testHiLoAlgorithm() {
9089
sessionImpl = (SessionImpl) sessionFactory.openSession();
91-
92-
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93-
// historically the hilo generators skipped the initial block of values;
94-
// so the first generated id value is maxlo + 1, here be 4
95-
assertEquals( 4L, generateValue() );
96-
// which should also perform the first read on the sequence which should set it to its "start with" value (1)
97-
assertEquals( 1L, extractSequenceValue() );
98-
99-
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100-
assertEquals( 5L, generateValue() );
101-
assertEquals( 1L, extractSequenceValue() );
102-
103-
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104-
assertEquals( 6L, generateValue() );
105-
assertEquals( 1L, extractSequenceValue() );
106-
107-
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108-
assertEquals( 7L, generateValue() );
109-
// unlike the newer strategies, the db value will not get update here. It gets updated on the next invocation
110-
// afterQuery a clock over
111-
assertEquals( 1L, extractSequenceValue() );
112-
113-
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114-
assertEquals( 8L, generateValue() );
115-
// this should force an increment in the sequence value
116-
assertEquals( 2L, extractSequenceValue() );
117-
118-
((Session) sessionImpl).close();
90+
try {
91+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92+
// historically the hilo generators skipped the initial block of values;
93+
// so the first generated id value is maxlo + 1, here be 4
94+
assertEquals(4L, generateValue());
95+
// which should also perform the first read on the sequence which should set it to its "start with" value (1)
96+
assertEquals(1L, extractSequenceValue());
97+
98+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99+
assertEquals(5L, generateValue());
100+
assertEquals(1L, extractSequenceValue());
101+
102+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103+
assertEquals(6L, generateValue());
104+
assertEquals(1L, extractSequenceValue());
105+
106+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107+
assertEquals(7L, generateValue());
108+
// unlike the newer strategies, the db value will not get update here. It gets updated on the next invocation
109+
// afterQuery a clock over
110+
assertEquals(1L, extractSequenceValue());
111+
112+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113+
assertEquals(8L, generateValue());
114+
// this should force an increment in the sequence value
115+
assertEquals(2L, extractSequenceValue());
116+
} finally {
117+
sessionImpl.close();
118+
}
119119
}
120120

121121
private long extractSequenceValue() {
122122
return sequenceValueExtractor.extractSequenceValue( sessionImpl );
123123
}
124124

125125
private long generateValue() {
126-
Long generatedValue;
127-
Transaction transaction = ((Session) sessionImpl).beginTransaction();
128-
generatedValue = (Long) generator.generate( sessionImpl, null );
129-
transaction.commit();
130-
return generatedValue.longValue();
126+
Transaction transaction = sessionImpl.beginTransaction();
127+
try {
128+
return (Long) generator.generate( sessionImpl, null );
129+
}
130+
finally {
131+
transaction.commit();
132+
}
131133
}
132134
}

hibernate-core/src/test/java/org/hibernate/id/SequenceValueExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ else if ( dialect instanceof Oracle8iDialect ) {
4343
queryString = "select " + sequenceName + ".currval from dual";
4444
}
4545
else if ( dialect instanceof SQLServer2012Dialect ) {
46-
queryString = "SELECT CONVERT(varchar(200), Current_value) FROM SYS.Sequences WHERE name = '" + sequenceName + "'";
46+
queryString = "SELECT CONVERT(varchar(200), Current_value) FROM sys.sequences WHERE name = '" + sequenceName + "'";
4747
}
4848
else if ( dialect instanceof HSQLDialect ) {
4949

0 commit comments

Comments
 (0)