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;
}
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;
}
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();
}
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
}
}
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);
}
Aggregations