Skip to content

Commit 3b8d654

Browse files
committed
#80 log collection creation or index creation inside transaction
1 parent f589bf3 commit 3b8d654

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/main/java/com/arangodb/springframework/core/template/ArangoTemplate.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.springframework.context.expression.BeanFactoryAccessor;
4646
import org.springframework.context.expression.BeanFactoryResolver;
4747
import org.springframework.dao.DataAccessException;
48-
import org.springframework.dao.InvalidDataAccessResourceUsageException;
4948
import org.springframework.dao.support.DataAccessUtils;
5049
import org.springframework.dao.support.PersistenceExceptionTranslator;
5150
import org.springframework.data.mapping.PersistentPropertyAccessor;
@@ -158,7 +157,7 @@ private ArangoCollection _collection(final String name, final ArangoPersistentEn
158157
final ArangoCollection collection = db.collection(name);
159158
if (!collection.exists()) {
160159
if (transactional) {
161-
throw new InvalidDataAccessResourceUsageException("Missing collection cannot be created during transaction");
160+
LOGGER.debug("Creating collection {} during transaction", name);
162161
}
163162
collection.create(options);
164163
}
@@ -169,7 +168,7 @@ private ArangoCollection _collection(final String name, final ArangoPersistentEn
169168
if (persistentEntity != null && !entities.contains(entityClass)) {
170169
value.addEntityClass(entityClass);
171170
if (transactional) {
172-
LOGGER.debug("Not ensuring any indexes of collection {} for {} during transaction", collection.name(), entityClass);
171+
LOGGER.debug("Not ensuring any indexes of collection {} for {} during transaction", name, entityClass);
173172
} else {
174173
ensureCollectionIndexes(collection(collection), persistentEntity);
175174
}
@@ -363,7 +362,8 @@ public ArangoDBVersion getVersion() throws DataAccessException {
363362
public <T> ArangoCursor<T> query(final String query, final Map<String, Object> bindVars,
364363
final AqlQueryOptions options, final Class<T> entityClass) throws DataAccessException {
365364
try {
366-
ArangoCursor<T> cursor = db().query(query, entityClass, bindVars == null ? null : prepareBindVars(bindVars, options != null && options.getStreamTransactionId() != null), options);
365+
boolean transactional = options != null && options.getStreamTransactionId() != null;
366+
ArangoCursor<T> cursor = db().query(query, entityClass, bindVars == null ? null : prepareBindVars(bindVars, transactional), options);
367367
return new ArangoExtCursor<>(cursor, entityClass, eventPublisher);
368368
} catch (final ArangoDBException e) {
369369
throw translateException(e);
@@ -393,7 +393,8 @@ public <T> MultiDocumentEntity<DocumentDeleteEntity<T>> deleteAll(
393393

394394
MultiDocumentEntity<DocumentDeleteEntity<T>> result;
395395
try {
396-
result = _collection(entityClass, options != null && options.getStreamTransactionId() != null).deleteDocuments(toList(values), options, entityClass);
396+
boolean transactional = options != null && options.getStreamTransactionId() != null;
397+
result = _collection(entityClass, transactional).deleteDocuments(toList(values), options, entityClass);
397398
} catch (final ArangoDBException e) {
398399
throw translateException(e);
399400
}
@@ -425,7 +426,8 @@ public <T> DocumentDeleteEntity<T> delete(final Object id, final DocumentDeleteO
425426

426427
DocumentDeleteEntity<T> result;
427428
try {
428-
result = _collection(entityClass, id, options != null && options.getStreamTransactionId() != null).deleteDocument(determineDocumentKeyFromId(id), options, entityClass);
429+
boolean transactional = options != null && options.getStreamTransactionId() != null;
430+
result = _collection(entityClass, id, transactional).deleteDocument(determineDocumentKeyFromId(id), options, entityClass);
429431
} catch (final ArangoDBException e) {
430432
throw translateException(e);
431433
}
@@ -445,7 +447,8 @@ public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> updateAll(
445447

446448
MultiDocumentEntity<DocumentUpdateEntity<T>> result;
447449
try {
448-
result = _collection(entityClass, options != null && options.getStreamTransactionId() != null).updateDocuments(toList(values), options, entityClass);
450+
boolean transactional = options != null && options.getStreamTransactionId() != null;
451+
result = _collection(entityClass, transactional).updateDocuments(toList(values), options, entityClass);
449452
} catch (final ArangoDBException e) {
450453
throw translateException(e);
451454
}
@@ -463,7 +466,8 @@ public <T> DocumentUpdateEntity<T> update(final Object id, final T value, final
463466

464467
DocumentUpdateEntity<T> result;
465468
try {
466-
result = _collection(value.getClass(), id, options != null && options.getStreamTransactionId() != null).updateDocument(determineDocumentKeyFromId(id), value, options);
469+
boolean transactional = options != null && options.getStreamTransactionId() != null;
470+
result = _collection(value.getClass(), id, transactional).updateDocument(determineDocumentKeyFromId(id), value, options);
467471
} catch (final ArangoDBException e) {
468472
throw translateException(e);
469473
}
@@ -484,7 +488,8 @@ public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> replaceAll(
484488

485489
MultiDocumentEntity<DocumentUpdateEntity<T>> result;
486490
try {
487-
result = _collection(entityClass, options != null && options.getStreamTransactionId() != null).replaceDocuments(toList(values), options, entityClass);
491+
boolean transactional = options != null && options.getStreamTransactionId() != null;
492+
result = _collection(entityClass, transactional).replaceDocuments(toList(values), options, entityClass);
488493
} catch (final ArangoDBException e) {
489494
throw translateException(e);
490495
}
@@ -501,7 +506,8 @@ public <T> DocumentUpdateEntity<T> replace(final Object id, final T value, final
501506

502507
DocumentUpdateEntity<T> result;
503508
try {
504-
result = _collection(value.getClass(), id, options != null && options.getStreamTransactionId() != null).replaceDocument(determineDocumentKeyFromId(id), value, options);
509+
boolean transactional = options != null && options.getStreamTransactionId() != null;
510+
result = _collection(value.getClass(), id, transactional).replaceDocument(determineDocumentKeyFromId(id), value, options);
505511
} catch (final ArangoDBException e) {
506512
throw translateException(e);
507513
}
@@ -515,7 +521,8 @@ public <T> DocumentUpdateEntity<T> replace(final Object id, final T value, final
515521
public <T> Optional<T> find(final Object id, final Class<T> entityClass, final DocumentReadOptions options)
516522
throws DataAccessException {
517523
try {
518-
T res = _collection(entityClass, id, options != null && options.getStreamTransactionId() != null).getDocument(determineDocumentKeyFromId(id), entityClass, options);
524+
boolean transactional = options != null && options.getStreamTransactionId() != null;
525+
T res = _collection(entityClass, id, transactional).getDocument(determineDocumentKeyFromId(id), entityClass, options);
519526
if (res != null) {
520527
potentiallyEmitEvent(new AfterLoadEvent<>(res));
521528
}
@@ -538,7 +545,8 @@ public <T> Iterable<T> findAll(final Iterable<?> ids, final Class<T> entityClass
538545
try {
539546
final Collection<String> keys = new ArrayList<>();
540547
ids.forEach(id -> keys.add(determineDocumentKeyFromId(id)));
541-
Collection<T> docs = _collection(entityClass, options != null && options.getStreamTransactionId() != null).getDocuments(keys, entityClass).getDocuments();
548+
boolean transactional = options != null && options.getStreamTransactionId() != null;
549+
Collection<T> docs = _collection(entityClass, transactional).getDocuments(keys, entityClass).getDocuments();
542550
for (T doc : docs) {
543551
if (doc != null) {
544552
potentiallyEmitEvent(new AfterLoadEvent<>(doc));
@@ -558,7 +566,8 @@ public <T> MultiDocumentEntity<DocumentCreateEntity<T>> insertAll(
558566

559567
MultiDocumentEntity<DocumentCreateEntity<T>> result;
560568
try {
561-
result = _collection(entityClass, options != null && options.getStreamTransactionId() != null).insertDocuments(toList(values), options, entityClass);
569+
boolean transactional = options != null && options.getStreamTransactionId() != null;
570+
result = _collection(entityClass, transactional).insertDocuments(toList(values), options, entityClass);
562571
} catch (final ArangoDBException e) {
563572
throw translateException(e);
564573
}
@@ -574,7 +583,8 @@ public <T> DocumentCreateEntity<T> insert(final T value, final DocumentCreateOpt
574583

575584
DocumentCreateEntity<T> result;
576585
try {
577-
result = _collection(value.getClass(), options != null && options.getStreamTransactionId() != null).insertDocument(value, options);
586+
boolean transactional = options != null && options.getStreamTransactionId() != null;
587+
result = _collection(value.getClass(), transactional).insertDocument(value, options);
578588
} catch (final ArangoDBException e) {
579589
throw translateException(e);
580590
}
@@ -587,7 +597,8 @@ public <T> DocumentCreateEntity<T> insert(final T value, final DocumentCreateOpt
587597
@Override
588598
public <T> T repsert(final T value, AqlQueryOptions options) throws DataAccessException {
589599
@SuppressWarnings("unchecked") final Class<T> clazz = (Class<T>) value.getClass();
590-
final String collectionName = _collection(clazz, options != null && options.getStreamTransactionId() != null).name();
600+
boolean transactional = options != null && options.getStreamTransactionId() != null;
601+
final String collectionName = _collection(clazz, transactional).name();
591602

592603
potentiallyEmitEvent(new BeforeSaveEvent<>(value));
593604

@@ -619,7 +630,8 @@ public <T> Iterable<T> repsertAll(final Iterable<T> values, final Class<? super
619630
return Collections.emptyList();
620631
}
621632

622-
final String collectionName = _collection(entityClass, options != null && options.getStreamTransactionId() != null).name();
633+
boolean transactional = options != null && options.getStreamTransactionId() != null;
634+
final String collectionName = _collection(entityClass, transactional).name();
623635
potentiallyEmitBeforeSaveEvent(values);
624636

625637
Map<String, Object> bindVars = new HashMap<>();
@@ -720,7 +732,8 @@ private void updateDBFields(final Object value, final DocumentEntity documentEnt
720732
@Override
721733
public boolean exists(final Object id, final Class<?> entityClass, DocumentExistsOptions options) throws DataAccessException {
722734
try {
723-
return _collection(entityClass, options != null && options.getStreamTransactionId() != null).documentExists(determineDocumentKeyFromId(id), options);
735+
boolean transactional = options != null && options.getStreamTransactionId() != null;
736+
return _collection(entityClass, transactional).documentExists(determineDocumentKeyFromId(id), options);
724737
} catch (final ArangoDBException e) {
725738
throw translateException(e);
726739
}

0 commit comments

Comments
 (0)