|
8 | 8 |
|
9 | 9 | import java.util.Properties;
|
10 | 10 |
|
11 |
| -import org.hibernate.Session; |
12 | 11 | import org.hibernate.Transaction;
|
13 | 12 | import org.hibernate.boot.Metadata;
|
14 | 13 | import org.hibernate.boot.MetadataSources;
|
@@ -88,45 +87,48 @@ public void tearDown() throws Exception {
|
88 | 87 | @Test
|
89 | 88 | public void testHiLoAlgorithm() {
|
90 | 89 | 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 | + } |
119 | 119 | }
|
120 | 120 |
|
121 | 121 | private long extractSequenceValue() {
|
122 | 122 | return sequenceValueExtractor.extractSequenceValue( sessionImpl );
|
123 | 123 | }
|
124 | 124 |
|
125 | 125 | 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 | + } |
131 | 133 | }
|
132 | 134 | }
|
0 commit comments