/
BatchSequenceGenerator.java
422 lines (379 loc) · 15.1 KB
/
BatchSequenceGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package com.vladmihalcea.hibernate.id;
import com.vladmihalcea.hibernate.util.ReflectionUtils;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.QualifiedNameParser;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.BulkInsertionCapableIdentifierGenerator;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerationException;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.enhanced.DatabaseStructure;
import org.hibernate.id.enhanced.SequenceStructure;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* A sequence generator that uses a recursive query to fetch multiple
* values from a sequence in a single database access.
*
* <h2>Parameters</h2>
* The following configuration parameters are supported:
* <dl>
* <dt>{@value #SEQUENCE_PARAM}</dt>
* <dd><strong>mandatory</strong>, name of the sequence to use</dd>
* <dt>{@value #FETCH_SIZE_PARAM}</dt>
* <dd>optional, how many sequence numbers should be fetched at a time,
* default is {@value #DEFAULT_FETCH_SIZE}</dd>
* </dl>
*
* <h2>SQL</h2>
* Per default the generated SELECT will look something like this
* <pre><code>
* WITH RECURSIVE t(n, level_num) AS (
* SELECT nextval(seq_xxx) AS n, 1 AS level_num
* UNION ALL
* SELECT nextval(seq_xxx) AS n, level_num + 1 AS level_num
* FROM t
* WHERE level_num < ?)
* SELECT n
* FROM t;
* </code></pre>
*
* <h3>DB2</h3>
* For DB2 the generated SELECT will look something like this
* <pre><code>
* WITH t(n) AS (
* SELECT 1 AS n
* FROM (VALUES 1)
* UNION ALL
* SELECT n + 1 AS n
* FROM t
* WHERE n < ?)
* SELECT next value for SEQ_CHILD_ID AS n
* FROM t;
* </code></pre>
*
* <h3>HSQLDB</h3>
* For HSQLDB the generated SELECT will look something like this
* <pre><code>
* SELECT next value for seq_parent_id
* FROM UNNEST(SEQUENCE_ARRAY(1, ?, 1));
* </code></pre>
*
* <h3>Oracle</h3>
* For Oracle the generated SELECT will look something like this
* because Oracle does not support using recursive common table
* expressions to fetch multiple values from a sequence.
* <pre><code>
* SELECT seq_xxx.nextval
* FROM dual
* CONNECT BY rownum <= ?
* </code></pre>
*
* <h3>SQL Server</h3>
* For SQL Server the generated SELECT will look something like this
* <pre><code>
* WITH t(n) AS (
* SELECT 1 AS n
* UNION ALL
* SELECT n + 1 AS n
* FROM t
* WHERE n < ?)
* SELECT NEXT VALUE FOR seq_xxx AS n
* FROM t
* </code></pre>
*
* <h3>Firebird</h3>
* For Firebird the generated SELECT will look something like this
* <pre><code>
* WITH RECURSIVE t(n, level_num) AS (
* SELECT NEXT VALUE FOR seq_xxx AS n, 1 AS level_num
* FROM rdb$database
* UNION ALL
* SELECT NEXT VALUE FOR seq_xxx AS n, level_num + 1 AS level_num
* FROM t
* WHERE level_num < ?)
* SELECT n
* FROM t
* </code></pre>
*
* <h2>Database Support</h2>
* The following RDBMS have been verified to work
* <ul>
* <li>DB2</li>
* <li>Firebird</li>
* <li>Oracle</li>
* <li>H2</li>
* <li>HSQLDB</li>
* <li>MariaDB</li>
* <li>Postgres</li>
* <li>SQL Sever</li>
* </ul>
* <p>
* In theory any RDBMS that supports {@code WITH RECURSIVE} and
* sequences is supported.
* <p>
* For more details about how to use it, check out <a href="https://vladmihalcea.com/hibernate-batch-sequence-generator/">this article</a> on <a href="https://vladmihalcea.com/">vladmihalcea.com</a>.
*
* @author Philippe Marschall
* @since 2.14.0
*/
public class BatchSequenceGenerator implements BulkInsertionCapableIdentifierGenerator, PersistentIdentifierGenerator, Configurable {
/**
* Indicates the name of the sequence to use, mandatory.
*/
public static final String SEQUENCE_PARAM = "sequence";
/**
* Indicates how many sequence values to fetch at once. The default value is {@link #DEFAULT_FETCH_SIZE}.
*/
public static final String FETCH_SIZE_PARAM = "fetch_size";
/**
* The default value for {@link #FETCH_SIZE_PARAM}.
*/
public static final int DEFAULT_FETCH_SIZE = 10;
private final Lock lock = new ReentrantLock();
private String select;
private int fetchSize;
private IdentifierPool identifierPool;
private IdentifierExtractor identifierExtractor;
private DatabaseStructure databaseStructure;
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry)
throws MappingException {
JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class);
Dialect dialect = jdbcEnvironment.getDialect();
String sequenceName = determineSequenceName(params);
this.fetchSize = determineFetchSize(params);
this.select = buildSelect(sequenceName, dialect);
this.identifierExtractor = IdentifierExtractor.getIdentifierExtractor(type.getReturnedClass());
this.identifierPool = IdentifierPool.empty();
this.databaseStructure = this.buildDatabaseStructure(type, sequenceName, jdbcEnvironment);
}
private static String buildSelect(String sequenceName, Dialect dialect) {
if (dialect instanceof org.hibernate.dialect.Oracle8iDialect) {
return "SELECT " + dialect.getSelectSequenceNextValString(sequenceName) + " FROM dual CONNECT BY rownum <= ?";
}
if (dialect instanceof org.hibernate.dialect.SQLServerDialect) {
// No RECURSIVE
return "WITH t(n) AS ( "
+ "SELECT 1 AS n "
+ "UNION ALL "
+"SELECT n + 1 AS n FROM t WHERE n < ?) "
// sequence generation outside of WITH
+ "SELECT " + dialect.getSelectSequenceNextValString(sequenceName) + " AS n FROM t";
}
if (dialect instanceof org.hibernate.dialect.DB2Dialect) {
// No RECURSIVE
return "WITH t(n) AS ( "
+ "SELECT 1 AS n "
// difference
+ "FROM (VALUES 1) "
+ "UNION ALL "
+"SELECT n + 1 AS n FROM t WHERE n < ?) "
// sequence generation outside of WITH
+ "SELECT " + dialect.getSelectSequenceNextValString(sequenceName) + " AS n FROM t";
}
if (dialect instanceof org.hibernate.dialect.HSQLDialect) {
// https://stackoverflow.com/questions/44472280/cte-based-sequence-generation-with-hsqldb/52329862
return "SELECT " + dialect.getSelectSequenceNextValString(sequenceName) + " FROM UNNEST(SEQUENCE_ARRAY(1, ?, 1))";
}
if (dialect instanceof org.hibernate.dialect.FirebirdDialect) {
return "WITH RECURSIVE t(n, level_num) AS ("
+ "SELECT " + dialect.getSelectSequenceNextValString(sequenceName) + " AS n, 1 AS level_num "
// difference
+ " FROM rdb$database "
+ "UNION ALL "
+ "SELECT " + dialect.getSelectSequenceNextValString(sequenceName) + " AS n, level_num + 1 AS level_num "
+ " FROM t "
+ " WHERE level_num < ?) "
+ "SELECT n FROM t";
}
return "WITH RECURSIVE t(n, level_num) AS ("
+ "SELECT " + dialect.getSelectSequenceNextValString(sequenceName) + " AS n, 1 AS level_num "
+ "UNION ALL "
+ "SELECT " + dialect.getSelectSequenceNextValString(sequenceName) + " AS n, level_num + 1 AS level_num "
+ " FROM t "
+ " WHERE level_num < ?) "
+ "SELECT n FROM t";
}
private SequenceStructure buildDatabaseStructure(Type type, String sequenceName, JdbcEnvironment jdbcEnvironment) {
return new SequenceStructure(jdbcEnvironment,
QualifiedNameParser.INSTANCE.parse(sequenceName), 1, 1, type.getReturnedClass());
}
private static String determineSequenceName(Properties params) {
String sequenceName = params.getProperty(SEQUENCE_PARAM);
if (sequenceName == null) {
throw new MappingException("no squence name specified");
}
return sequenceName;
}
private static int determineFetchSize(Properties params) {
int fetchSize = ConfigurationHelper.getInt(FETCH_SIZE_PARAM, params, DEFAULT_FETCH_SIZE);
if (fetchSize <= 0) {
throw new MappingException("fetch size must be positive");
}
return fetchSize;
}
@Override
public boolean supportsBulkInsertionIdentifierGeneration() {
return true;
}
@Override
public String determineBulkInsertionIdentifierGenerationSelectFragment(Dialect dialect) {
return dialect.getSelectSequenceNextValString(this.getSequenceName());
}
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
this.lock.lock();
try {
if (this.identifierPool.isEmpty()) {
this.identifierPool = this.replenishIdentifierPool(session);
}
return this.identifierPool.next();
} finally {
this.lock.unlock();
}
}
@Override
public Object generatorKey() {
return this.getSequenceName();
}
private String getSequenceName() {
return this.databaseStructure.getName();
}
public String[] sqlCreateStrings(Dialect dialect) {
return ReflectionUtils.invokeMethod(databaseStructure, "sqlCreateStrings", dialect);
}
public String[] sqlDropStrings(Dialect dialect) {
return ReflectionUtils.invokeMethod(databaseStructure, "sqlDropStrings", dialect);
}
@Override
public void registerExportables(Database database) {
this.databaseStructure.registerExportables(database);
}
private IdentifierPool replenishIdentifierPool(SharedSessionContractImplementor session)
throws HibernateException {
JdbcCoordinator coordinator = session.getJdbcCoordinator();
List<Serializable> identifiers = new ArrayList<>(this.fetchSize);
try (PreparedStatement statement = coordinator.getStatementPreparer().prepareStatement(this.select)) {
statement.setFetchSize(this.fetchSize);
statement.setInt(1, this.fetchSize);
try (ResultSet resultSet = coordinator.getResultSetReturn().extract(statement)) {
while (resultSet.next()) {
identifiers.add(this.identifierExtractor.extractIdentifier(resultSet));
}
}
} catch (SQLException e) {
throw session.getJdbcServices().getSqlExceptionHelper().convert(
e, "could not get next sequence value", this.select);
}
if (identifiers.size() != this.fetchSize) {
throw new IdentifierGenerationException("expected " + this.fetchSize + " values from " + this.getSequenceName()
+ " but got " + identifiers.size());
}
return IdentifierPool.forList(identifiers);
}
/**
* Holds a number of prefetched identifiers.
*/
static final class IdentifierPool {
private final Iterator<Serializable> iterator;
private IdentifierPool (List<Serializable> identifiers) {
this.iterator = identifiers.iterator();
}
static IdentifierPool forList(List<Serializable> identifiers) {
return new IdentifierPool(identifiers);
}
static IdentifierPool empty() {
return new IdentifierPool(Collections.emptyList());
}
boolean isEmpty() {
return !this.iterator.hasNext();
}
Serializable next() {
return this.iterator.next();
}
}
/**
* Extracts a {@link Serializable} identifier from a {@link ResultSet}.
*
* @see org.hibernate.id.IntegralDataTypeHolder
*/
enum IdentifierExtractor {
INTEGER_IDENTIFIER_EXTRACTOR {
@Override
Serializable extractIdentifier(ResultSet resultSet) throws SQLException {
int intValue = resultSet.getInt(1);
if (resultSet.wasNull()) {
throw new IdentifierGenerationException("sequence returned null");
}
return intValue;
}
},
LONG_IDENTIFIER_EXTRACTOR {
@Override
Serializable extractIdentifier(ResultSet resultSet) throws SQLException {
long longValue = resultSet.getLong(1);
if (resultSet.wasNull()) {
throw new IdentifierGenerationException("sequence returned null");
}
return longValue;
}
},
BIG_INTEGER_IDENTIFIER_EXTRACTOR {
@Override
Serializable extractIdentifier(ResultSet resultSet) throws SQLException {
BigDecimal bigDecimal = resultSet.getBigDecimal(1);
if (resultSet.wasNull()) {
throw new IdentifierGenerationException("sequence returned null");
}
return bigDecimal.setScale(0, BigDecimal.ROUND_UNNECESSARY).toBigInteger();
}
},
BIG_DECIMAL_IDENTIFIER_EXTRACTOR {
@Override
Serializable extractIdentifier(ResultSet resultSet) throws SQLException {
BigDecimal bigDecimal = resultSet.getBigDecimal(1);
if (resultSet.wasNull()) {
throw new IdentifierGenerationException("sequence returned null");
}
return bigDecimal;
}
};
abstract Serializable extractIdentifier(ResultSet resultSet) throws SQLException;
static IdentifierExtractor getIdentifierExtractor(Class<?> integralType) {
if ((integralType == Integer.class) || (integralType == int.class)) {
return INTEGER_IDENTIFIER_EXTRACTOR;
}
if ((integralType == Long.class) || (integralType == long.class)) {
return LONG_IDENTIFIER_EXTRACTOR;
}
if (integralType == BigInteger.class) {
return BIG_INTEGER_IDENTIFIER_EXTRACTOR;
}
if (integralType == BigDecimal.class) {
return BIG_DECIMAL_IDENTIFIER_EXTRACTOR;
}
throw new IdentifierGenerationException("unsupported integral type: " + integralType);
}
}
@Override
public String toString() {
// for debugging only
return this.getClass().getSimpleName() + '(' + this.getSequenceName() + ')';
}
}