Search in sources :

Example 71 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project sw360portal by sw360.

the class Sw360ReleaseService method createRelease.

public Release createRelease(Release release, User sw360User) throws TException {
    ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
    AddDocumentRequestSummary documentRequestSummary = sw360ComponentClient.addRelease(release, sw360User);
    if (documentRequestSummary.getRequestStatus() == AddDocumentRequestStatus.SUCCESS) {
        release.setId(documentRequestSummary.getId());
        return release;
    } else if (documentRequestSummary.getRequestStatus() == AddDocumentRequestStatus.DUPLICATE) {
        throw new DataIntegrityViolationException("sw360 release with name '" + release.getName() + "' already exists.");
    }
    return null;
}
Also used : AddDocumentRequestSummary(org.eclipse.sw360.datahandler.thrift.AddDocumentRequestSummary) ComponentService(org.eclipse.sw360.datahandler.thrift.components.ComponentService) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 72 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project workbench by all-of-us.

the class UserService method createServiceAccountUser.

public User createServiceAccountUser(String email) {
    User user = new User();
    user.setDataAccessLevel(DataAccessLevel.PROTECTED);
    user.setEmail(email);
    user.setDisabled(false);
    user.setEmailVerificationStatus(EmailVerificationStatus.UNVERIFIED);
    user.setFreeTierBillingProjectStatus(BillingProjectStatus.NONE);
    try {
        userDao.save(user);
    } catch (DataIntegrityViolationException e) {
        user = userDao.findUserByEmail(email);
        if (user == null) {
            throw e;
        }
    // If a user already existed (due to multiple requests trying to create a user simultaneously)
    // just return it.
    }
    return user;
}
Also used : User(org.pmiops.workbench.db.model.User) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 73 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project uPortal by Jasig.

the class BaseJpaDaoTest method deleteAllEntities.

/**
 * Deletes ALL entities from the database
 */
@After
public final void deleteAllEntities() {
    final EntityManager entityManager = getEntityManager();
    final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
    final Metamodel metamodel = entityManagerFactory.getMetamodel();
    Set<EntityType<?>> entityTypes = new LinkedHashSet<>(metamodel.getEntities());
    do {
        final Set<EntityType<?>> failedEntitieTypes = new HashSet<>();
        for (final EntityType<?> entityType : entityTypes) {
            final String entityClassName = entityType.getBindableJavaType().getName();
            try {
                this.executeInTransaction(new CallableWithoutResult() {

                    @Override
                    protected void callWithoutResult() {
                        logger.trace("Purging all: " + entityClassName);
                        final Query query = entityManager.createQuery("SELECT e FROM " + entityClassName + " AS e");
                        final List<?> entities = query.getResultList();
                        logger.trace("Found " + entities.size() + " " + entityClassName + " to delete");
                        for (final Object entity : entities) {
                            entityManager.remove(entity);
                        }
                    }
                });
            } catch (DataIntegrityViolationException e) {
                logger.trace("Failed to delete " + entityClassName + ". Must be a dependency of another entity");
                failedEntitieTypes.add(entityType);
            }
        }
        entityTypes = failedEntitieTypes;
    } while (!entityTypes.isEmpty());
    // Reset all spring managed mocks after every test
    MockitoFactoryBean.resetAllMocks();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Query(javax.persistence.Query) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) EntityType(javax.persistence.metamodel.EntityType) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) List(java.util.List) LinkedList(java.util.LinkedList) AccessibleObject(java.lang.reflect.AccessibleObject) Metamodel(javax.persistence.metamodel.Metamodel) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) After(org.junit.After)

Example 74 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project uPortal by Jasig.

the class JdbcAuthDao method createToken.

protected void createToken(final String serviceName) {
    try {
        this.jdbcOperations.execute(new ConnectionCallback<Object>() {

            @Override
            public Object doInConnection(Connection con) throws SQLException, DataAccessException {
                // This is horribly hacky but we can't rely on the main uPortal TM
                // directly or we get
                // into a circular dependency loop from JPA to Ehcache to jGroups and
                // back to JPA
                final DataSource ds = new SingleConnectionDataSource(con, true);
                final PlatformTransactionManager ptm = new DataSourceTransactionManager(ds);
                final TransactionOperations to = new TransactionTemplate(ptm);
                to.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) {
                        logger.info("Creating jGroups auth token");
                        final String authToken = RandomTokenGenerator.INSTANCE.generateRandomToken(authTokenLength);
                        final ImmutableMap<String, String> params = ImmutableMap.of(PRM_SERVICE_NAME, serviceName, PRM_RANDOM_TOKEN, authToken);
                        namedParameterJdbcOperations.update(INSERT_SQL, params);
                    }
                });
                return null;
            }
        });
    } catch (ConstraintViolationException e) {
    // Ignore, just means a concurrent token creation
    } catch (DataIntegrityViolationException e) {
    // Ignore, just means a concurrent token creation
    }
}
Also used : SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) TransactionOperations(org.springframework.transaction.support.TransactionOperations) SQLException(java.sql.SQLException) Connection(java.sql.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) DataSource(javax.sql.DataSource) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataAccessException(org.springframework.dao.DataAccessException) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 75 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project spring-data-mongodb by spring-projects.

the class ReactiveMongoPersistentEntityIndexCreator method translateException.

private Mono<? extends String> translateException(Throwable e, IndexDefinitionHolder indexDefinition) {
    Mono<IndexInfo> existingIndex = fetchIndexInformation(indexDefinition);
    Mono<String> defaultError = Mono.error(new DataIntegrityViolationException(String.format("Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.", indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()), e.getCause()));
    return existingIndex.flatMap(it -> {
        return Mono.<String>error(new DataIntegrityViolationException(String.format("Index already defined as '%s'.", indexDefinition.getPath()), e.getCause()));
    }).switchIfEmpty(defaultError);
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) IndexDefinitionHolder(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder) MongoException(com.mongodb.MongoException) ObjectUtils(org.springframework.util.ObjectUtils) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MappingContext(org.springframework.data.mapping.context.MappingContext) Mono(reactor.core.publisher.Mono) ArrayList(java.util.ArrayList) Document(org.springframework.data.mongodb.core.mapping.Document) Flux(reactor.core.publisher.Flux) List(java.util.List) MongoDbErrorCodes(org.springframework.data.mongodb.util.MongoDbErrorCodes) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Map(java.util.Map) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) UncategorizedMongoDbException(org.springframework.data.mongodb.UncategorizedMongoDbException) Assert(org.springframework.util.Assert) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)117 Test (org.junit.Test)26 HashMap (java.util.HashMap)12 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)11 Transactional (org.springframework.transaction.annotation.Transactional)11 Test (org.junit.jupiter.api.Test)10 Transactional (javax.transaction.Transactional)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 User (ca.corefacility.bioinformatics.irida.model.user.User)8 SQLException (java.sql.SQLException)8 ConstraintViolation (javax.validation.ConstraintViolation)8 HashSet (java.util.HashSet)7 Locale (java.util.Locale)6 ConstraintViolationException (javax.validation.ConstraintViolationException)6 Date (java.util.Date)5 List (java.util.List)5 Set (java.util.Set)5 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)4 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)4 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)4