use of io.jmix.data.persistence.SequenceSupport in project jmix by jmix-framework.
the class SequencesImpl method checkSequenceExists.
protected void checkSequenceExists(Sequence sequence) {
String sequenceName = sequence.getName();
if (existingSequences.contains(sequenceName)) {
return;
}
lock.readLock().unlock();
lock.writeLock().lock();
try {
try {
// Create sequence in separate transaction because it's name is cached and we want to be sure it is created
// regardless of possible errors in the invoking code
String storeName = getDataStore(sequence);
TransactionTemplate transactionTemplate = new TransactionTemplate(storeAwareLocator.getTransactionManager(storeName));
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
transactionTemplate.executeWithoutResult(status -> {
EntityManager entityManager = storeAwareLocator.getEntityManager(storeName);
SequenceSupport sequenceSupport = getSequenceSupport(sequence);
Query query = entityManager.createNativeQuery(sequenceSupport.sequenceExistsSql(sequenceName));
List list = query.getResultList();
if (list.isEmpty()) {
query = entityManager.createNativeQuery(sequenceSupport.createSequenceSql(sequenceName, sequence.getStartValue(), sequence.getIncrement()));
query.executeUpdate();
}
existingSequences.add(sequenceName);
});
} finally {
lock.readLock().lock();
}
} finally {
lock.writeLock().unlock();
}
}
Aggregations