Search in sources :

Example 41 with DataAccessException

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

the class MongoExceptionTranslatorUnitTests method translateToUncategorizedMongoDbException.

@Test
public void translateToUncategorizedMongoDbException() {
    MongoException exception = new MongoException(0, "");
    DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
    expectExceptionWithCauseMessage(translatedException, UncategorizedMongoDbException.class);
}
Also used : MongoException(com.mongodb.MongoException) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 42 with DataAccessException

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

the class MongoExceptionTranslatorUnitTests method translateMongoInternalException.

@Test
public void translateMongoInternalException() {
    MongoInternalException exception = new MongoInternalException("Internal exception");
    DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
    expectExceptionWithCauseMessage(translatedException, InvalidDataAccessResourceUsageException.class);
}
Also used : DataAccessException(org.springframework.dao.DataAccessException) MongoInternalException(com.mongodb.MongoInternalException) Test(org.junit.Test)

Example 43 with DataAccessException

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

the class MongoTemplateTests method shouldExecuteQueryShouldMapQueryBeforeQueryExecution.

// DATAMONGO-816
@Test
public void shouldExecuteQueryShouldMapQueryBeforeQueryExecution() {
    ObjectWithEnumValue o = new ObjectWithEnumValue();
    o.value = EnumValue.VALUE2;
    template.save(o);
    Query q = Query.query(Criteria.where("value").in(EnumValue.VALUE2));
    template.executeQuery(q, StringUtils.uncapitalize(ObjectWithEnumValue.class.getSimpleName()), new DocumentCallbackHandler() {

        @Override
        public void processDocument(org.bson.Document document) throws MongoException, DataAccessException {
            assertThat(document, is(notNullValue()));
            ObjectWithEnumValue result = template.getConverter().read(ObjectWithEnumValue.class, document);
            assertThat(result.value, is(EnumValue.VALUE2));
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 44 with DataAccessException

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

the class MongoTemplate method doRemove.

protected <T> DeleteResult doRemove(final String collectionName, final Query query, @Nullable final Class<T> entityClass) {
    Assert.notNull(query, "Query must not be null!");
    Assert.hasText(collectionName, "Collection name must not be null or empty!");
    final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
    final Document queryObject = queryMapper.getMappedObject(query.getQueryObject(), entity);
    return execute(collectionName, new CollectionCallback<DeleteResult>() {

        public DeleteResult doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
            maybeEmitEvent(new BeforeDeleteEvent<T>(queryObject, entityClass, collectionName));
            Document removeQuery = queryObject;
            DeleteOptions options = new DeleteOptions();
            query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);
            MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName, entityClass, null, queryObject);
            WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
            DeleteResult dr = null;
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Remove using query: {} in collection: {}.", new Object[] { serializeToJsonSafely(removeQuery), collectionName });
            }
            if (query.getLimit() > 0 || query.getSkip() > 0) {
                MongoCursor<Document> cursor = new QueryCursorPreparer(query, entityClass).prepare(collection.find(removeQuery).projection(new Document(ID_FIELD, 1))).iterator();
                Set<Object> ids = new LinkedHashSet<>();
                while (cursor.hasNext()) {
                    ids.add(cursor.next().get(ID_FIELD));
                }
                removeQuery = new Document(ID_FIELD, new Document("$in", ids));
            }
            if (writeConcernToUse == null) {
                dr = collection.deleteMany(removeQuery, options);
            } else {
                dr = collection.withWriteConcern(writeConcernToUse).deleteMany(removeQuery, options);
            }
            maybeEmitEvent(new AfterDeleteEvent<T>(queryObject, entityClass, collectionName));
            return dr;
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) Document(org.bson.Document) BeforeDeleteEvent(org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent) Collation(org.springframework.data.mongodb.core.query.Collation) AfterDeleteEvent(org.springframework.data.mongodb.core.mapping.event.AfterDeleteEvent) WriteConcern(com.mongodb.WriteConcern) MongoCursor(com.mongodb.client.MongoCursor) DeleteResult(com.mongodb.client.result.DeleteResult) DataAccessException(org.springframework.dao.DataAccessException)

Example 45 with DataAccessException

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

the class MongoTemplate method executeCommand.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.MongoOperations#executeCommand(org.bson.Document)
	 */
@Override
public Document executeCommand(final Document command) {
    Assert.notNull(command, "Command must not be null!");
    Document result = execute(new DbCallback<Document>() {

        public Document doInDB(MongoDatabase db) throws MongoException, DataAccessException {
            return db.runCommand(command, Document.class);
        }
    });
    return result;
}
Also used : MongoException(com.mongodb.MongoException) Document(org.bson.Document) DataAccessException(org.springframework.dao.DataAccessException) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

DataAccessException (org.springframework.dao.DataAccessException)89 SQLException (java.sql.SQLException)40 Test (org.junit.Test)26 Connection (java.sql.Connection)17 ResultSet (java.sql.ResultSet)16 PreparedStatement (java.sql.PreparedStatement)14 MongoException (com.mongodb.MongoException)13 Document (org.bson.Document)8 TransactionStatus (org.springframework.transaction.TransactionStatus)7 HashMap (java.util.HashMap)6 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)5 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)5 IOException (java.io.IOException)4 ConnectionCallback (org.springframework.jdbc.core.ConnectionCallback)4 SpringSqlParams (com.opengamma.elsql.SpringSqlParams)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IJoinQueryString (org.apereo.portal.jdbc.IJoinQueryString)3