57
57
import org .springframework .context .expression .BeanFactoryAccessor ;
58
58
import org .springframework .context .expression .BeanFactoryResolver ;
59
59
import org .springframework .dao .DataAccessException ;
60
+ import org .springframework .dao .InvalidDataAccessResourceUsageException ;
60
61
import org .springframework .dao .support .DataAccessUtils ;
61
62
import org .springframework .dao .support .PersistenceExceptionTranslator ;
62
63
import org .springframework .data .domain .Persistable ;
@@ -155,30 +156,33 @@ private DataAccessException translateExceptionIfPossible(final RuntimeException
155
156
return exceptionTranslator .translateExceptionIfPossible (exception );
156
157
}
157
158
158
- private ArangoCollection _collection (final String name ) {
159
- return _collection (name , null , null );
159
+ private ArangoCollection _collection (final String name , boolean transactional ) {
160
+ return _collection (name , null , null , transactional );
160
161
}
161
162
162
- private ArangoCollection _collection (final Class <?> entityClass ) {
163
- return _collection (entityClass , null );
163
+ private ArangoCollection _collection (final Class <?> entityClass , boolean transactional ) {
164
+ return _collection (entityClass , null , transactional );
164
165
}
165
166
166
- private ArangoCollection _collection (final Class <?> entityClass , final Object id ) {
167
+ private ArangoCollection _collection (final Class <?> entityClass , final Object id , boolean transactional ) {
167
168
final ArangoPersistentEntity <?> persistentEntity = converter .getMappingContext ()
168
169
.getRequiredPersistentEntity (entityClass );
169
170
final String name = determineCollectionFromId (id ).orElse (persistentEntity .getCollection ());
170
- return _collection (name , persistentEntity , persistentEntity .getCollectionOptions ());
171
+ return _collection (name , persistentEntity , persistentEntity .getCollectionOptions (), transactional );
171
172
}
172
173
173
174
private ArangoCollection _collection (final String name , final ArangoPersistentEntity <?> persistentEntity ,
174
- final CollectionCreateOptions options ) {
175
+ final CollectionCreateOptions options , boolean transactional ) {
175
176
176
177
final ArangoDatabase db = db ();
177
178
final Class <?> entityClass = persistentEntity != null ? persistentEntity .getType () : null ;
178
179
final CollectionCacheValue value = collectionCache .computeIfAbsent (new CollectionCacheKey (db .name (), name ),
179
180
key -> {
180
181
final ArangoCollection collection = db .collection (name );
181
182
if (!collection .exists ()) {
183
+ if (transactional ) {
184
+ throw new InvalidDataAccessResourceUsageException ("Missing collection cannot be created during transaction" );
185
+ }
182
186
collection .create (options );
183
187
}
184
188
return new CollectionCacheValue (collection );
@@ -187,7 +191,9 @@ private ArangoCollection _collection(final String name, final ArangoPersistentEn
187
191
final ArangoCollection collection = value .getCollection ();
188
192
if (persistentEntity != null && !entities .contains (entityClass )) {
189
193
value .addEntityClass (entityClass );
190
- ensureCollectionIndexes (collection (collection ), persistentEntity );
194
+ if (!transactional ) {
195
+ ensureCollectionIndexes (collection (collection ), persistentEntity );
196
+ }
191
197
}
192
198
return collection ;
193
199
}
@@ -334,14 +340,14 @@ public ArangoDBVersion getVersion() throws DataAccessException {
334
340
@ Override
335
341
public <T > ArangoCursor <T > query (final String query , final Map <String , Object > bindVars ,
336
342
final AqlQueryOptions options , final Class <T > entityClass ) throws DataAccessException {
337
- return db ().query (query , bindVars == null ? null : prepareBindVars (bindVars ), options , entityClass );
343
+ return db ().query (query , bindVars == null ? null : prepareBindVars (bindVars , false ), options , entityClass );
338
344
}
339
345
340
- private Map <String , Object > prepareBindVars (final Map <String , Object > bindVars ) {
346
+ private Map <String , Object > prepareBindVars (final Map <String , Object > bindVars , boolean transactional ) {
341
347
final Map <String , Object > prepared = new HashMap <>(bindVars .size ());
342
348
for (final Entry <String , Object > entry : bindVars .entrySet ()) {
343
349
if (entry .getKey ().startsWith ("@" ) && entry .getValue () instanceof Class ) {
344
- prepared .put (entry .getKey (), _collection ((Class <?>) entry .getValue ()).name ());
350
+ prepared .put (entry .getKey (), _collection ((Class <?>) entry .getValue (), transactional ).name ());
345
351
} else {
346
352
prepared .put (entry .getKey (), toVPack (entry .getValue ()));
347
353
}
@@ -357,7 +363,7 @@ public MultiDocumentEntity<? extends DocumentEntity> delete(final Iterable<Objec
357
363
358
364
MultiDocumentEntity <? extends DocumentEntity > result ;
359
365
try {
360
- result = _collection (entityClass ).deleteDocuments (toVPackCollection (values ), entityClass , options );
366
+ result = _collection (entityClass , options . getStreamTransactionId () != null ).deleteDocuments (toVPackCollection (values ), entityClass , options );
361
367
} catch (final ArangoDBException e ) {
362
368
throw translateExceptionIfPossible (e );
363
369
}
@@ -374,7 +380,7 @@ public DocumentEntity delete(final Object id, final Class<?> entityClass, final
374
380
375
381
final DocumentEntity result ;
376
382
try {
377
- result = _collection (entityClass , id ).deleteDocument (determineDocumentKeyFromId (id ), entityClass , options );
383
+ result = _collection (entityClass , id , options . getStreamTransactionId () != null ).deleteDocument (determineDocumentKeyFromId (id ), entityClass , options );
378
384
} catch (final ArangoDBException e ) {
379
385
throw translateExceptionIfPossible (e );
380
386
}
@@ -391,7 +397,7 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> update(final Iterable<T
391
397
392
398
final MultiDocumentEntity <? extends DocumentEntity > result ;
393
399
try {
394
- result = _collection (entityClass ).updateDocuments (toVPackCollection (values ), options );
400
+ result = _collection (entityClass , options . getStreamTransactionId () != null ).updateDocuments (toVPackCollection (values ), options );
395
401
} catch (final ArangoDBException e ) {
396
402
throw translateExceptionIfPossible (e );
397
403
}
@@ -409,7 +415,7 @@ public DocumentEntity update(final Object id, final Object value, final Document
409
415
410
416
final DocumentEntity result ;
411
417
try {
412
- result = _collection (value .getClass (), id ).updateDocument (determineDocumentKeyFromId (id ), toVPack (value ),
418
+ result = _collection (value .getClass (), id , options . getStreamTransactionId () != null ).updateDocument (determineDocumentKeyFromId (id ), toVPack (value ),
413
419
options );
414
420
} catch (final ArangoDBException e ) {
415
421
throw translateExceptionIfPossible (e );
@@ -428,7 +434,7 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> replace(final Iterable<
428
434
429
435
final MultiDocumentEntity <? extends DocumentEntity > result ;
430
436
try {
431
- result = _collection (entityClass ).replaceDocuments (toVPackCollection (values ), options );
437
+ result = _collection (entityClass , options . getStreamTransactionId () != null ).replaceDocuments (toVPackCollection (values ), options );
432
438
} catch (final ArangoDBException e ) {
433
439
throw translateExceptionIfPossible (e );
434
440
}
@@ -445,7 +451,7 @@ public DocumentEntity replace(final Object id, final Object value, final Documen
445
451
446
452
final DocumentEntity result ;
447
453
try {
448
- result = _collection (value .getClass (), id ).replaceDocument (determineDocumentKeyFromId (id ), toVPack (value ),
454
+ result = _collection (value .getClass (), id , false ).replaceDocument (determineDocumentKeyFromId (id ), toVPack (value ),
449
455
options );
450
456
} catch (final ArangoDBException e ) {
451
457
throw translateExceptionIfPossible (e );
@@ -460,7 +466,7 @@ public DocumentEntity replace(final Object id, final Object value, final Documen
460
466
public <T > Optional <T > find (final Object id , final Class <T > entityClass , final DocumentReadOptions options )
461
467
throws DataAccessException {
462
468
try {
463
- final VPackSlice doc = _collection (entityClass , id ).getDocument (determineDocumentKeyFromId (id ),
469
+ final VPackSlice doc = _collection (entityClass , id , options . getStreamTransactionId () != null ).getDocument (determineDocumentKeyFromId (id ),
464
470
VPackSlice .class , options );
465
471
return Optional .ofNullable (fromVPack (entityClass , doc ));
466
472
} catch (final ArangoDBException e ) {
@@ -481,7 +487,7 @@ public <T> Iterable<T> find(final Iterable<?> ids, final Class<T> entityClass, D
481
487
try {
482
488
final Collection <String > keys = new ArrayList <>();
483
489
ids .forEach (id -> keys .add (determineDocumentKeyFromId (id )));
484
- final MultiDocumentEntity <VPackSlice > docs = _collection (entityClass ).getDocuments (keys , VPackSlice .class , options );
490
+ final MultiDocumentEntity <VPackSlice > docs = _collection (entityClass , options . getStreamTransactionId () != null ).getDocuments (keys , VPackSlice .class , options );
485
491
return docs .getDocuments ().stream ().map (doc -> fromVPack (entityClass , doc )).collect (Collectors .toList ());
486
492
} catch (final ArangoDBException e ) {
487
493
throw translateExceptionIfPossible (e );
@@ -496,7 +502,7 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> insert(final Iterable<T
496
502
497
503
final MultiDocumentEntity <? extends DocumentEntity > result ;
498
504
try {
499
- result = _collection (entityClass ).insertDocuments (toVPackCollection (values ), options );
505
+ result = _collection (entityClass , options . getStreamTransactionId () != null ).insertDocuments (toVPackCollection (values ), options );
500
506
} catch (final ArangoDBException e ) {
501
507
throw translateExceptionIfPossible (e );
502
508
}
@@ -512,7 +518,7 @@ public DocumentEntity insert(final Object value, final DocumentCreateOptions opt
512
518
513
519
final DocumentEntity result ;
514
520
try {
515
- result = _collection (value .getClass ()).insertDocument (toVPack (value ), options );
521
+ result = _collection (value .getClass (), options . getStreamTransactionId () != null ).insertDocument (toVPack (value ), options );
516
522
} catch (final ArangoDBException e ) {
517
523
throw translateExceptionIfPossible (e );
518
524
}
@@ -529,7 +535,7 @@ public DocumentEntity insert(final String collectionName, final Object value, fi
529
535
530
536
final DocumentEntity result ;
531
537
try {
532
- result = _collection (collectionName ).insertDocument (toVPack (value ), options );
538
+ result = _collection (collectionName , options . getStreamTransactionId () != null ).insertDocument (toVPack (value ), options );
533
539
} catch (final ArangoDBException e ) {
534
540
throw translateExceptionIfPossible (e );
535
541
}
@@ -610,7 +616,7 @@ public <T> void upsert(final Iterable<T> value, final UpsertStrategy strategy) t
610
616
@ Override
611
617
public <T > void repsert (final T value , AqlQueryOptions options ) throws DataAccessException {
612
618
@ SuppressWarnings ("unchecked" ) final Class <T > clazz = (Class <T >) value .getClass ();
613
- final String collectionName = _collection (clazz ).name ();
619
+ final String collectionName = _collection (clazz , options . getStreamTransactionId () != null ).name ();
614
620
615
621
potentiallyEmitEvent (new BeforeSaveEvent <>(value ));
616
622
@@ -638,7 +644,7 @@ public <T> void repsert(final Iterable<? extends T> values, final Class<T> entit
638
644
return ;
639
645
}
640
646
641
- final String collectionName = _collection (entityClass ).name ();
647
+ final String collectionName = _collection (entityClass , options . getStreamTransactionId () != null ).name ();
642
648
potentiallyEmitBeforeSaveEvent (values );
643
649
644
650
final Iterable <? extends T > result ;
@@ -730,7 +736,7 @@ private void updateDBFields(final Object value, final DocumentEntity documentEnt
730
736
@ Override
731
737
public boolean exists (final Object id , final Class <?> entityClass , DocumentExistsOptions options ) throws DataAccessException {
732
738
try {
733
- return _collection (entityClass ).documentExists (determineDocumentKeyFromId (id ), options );
739
+ return _collection (entityClass , options . getStreamTransactionId () != null ).documentExists (determineDocumentKeyFromId (id ), options );
734
740
} catch (final ArangoDBException e ) {
735
741
throw translateExceptionIfPossible (e );
736
742
}
@@ -751,18 +757,18 @@ public void dropDatabase() throws DataAccessException {
751
757
752
758
@ Override
753
759
public CollectionOperations collection (final Class <?> entityClass ) throws DataAccessException {
754
- return collection (_collection (entityClass ));
760
+ return collection (_collection (entityClass , false ));
755
761
}
756
762
757
763
@ Override
758
- public CollectionOperations collection (final String name ) throws DataAccessException {
764
+ public CollectionOperations collection (String name ) {
759
765
return ArangoOperations .super .collection (name );
760
766
}
761
767
762
768
@ Override
763
769
public CollectionOperations collection (final String name , final CollectionCreateOptions options )
764
770
throws DataAccessException {
765
- return collection (_collection (name , null , options ));
771
+ return collection (_collection (name , null , options , false ));
766
772
}
767
773
768
774
private CollectionOperations collection (final ArangoCollection collection ) {
0 commit comments