Search in sources :

Example 1 with SequenceSupport

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();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) SequenceSupport(io.jmix.data.persistence.SequenceSupport) Query(javax.persistence.Query) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) List(java.util.List)

Aggregations

SequenceSupport (io.jmix.data.persistence.SequenceSupport)1 List (java.util.List)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1