Skip to content

Commit

Permalink
#250 ensure collections before transaction start
Browse files Browse the repository at this point in the history
  • Loading branch information
aburmeis committed Aug 23, 2023
1 parent 5ed1cc6 commit ef0a287
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

import com.arangodb.ArangoDBException;
import com.arangodb.ArangoDatabase;
import com.arangodb.DbName;
import com.arangodb.springframework.core.ArangoOperations;
import com.arangodb.springframework.core.template.CollectionCallback;
import com.arangodb.springframework.repository.query.QueryTransactionBridge;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -67,10 +67,10 @@ public void afterPropertiesSet() {
throw new IllegalStateException("Nested transactions must not be allowed");
}
if (!isGlobalRollbackOnParticipationFailure()) {
throw new IllegalStateException("Global rollback on participating failure is needed");
throw new IllegalStateException("Global rollback on participating failure is required");
}
if (getTransactionSynchronization() == SYNCHRONIZATION_NEVER) {
throw new IllegalStateException("Transaction synchronization is needed always");
throw new IllegalStateException("Transaction synchronization must not be disabled");
}
}

Expand All @@ -81,7 +81,7 @@ public void afterPropertiesSet() {
protected ArangoTransactionObject doGetTransaction() {
ArangoTransactionHolder holder = (ArangoTransactionHolder) TransactionSynchronizationManager.getResource(this);
try {
return new ArangoTransactionObject(operations.db(), getDefaultTimeout(), holder);
return new ArangoTransactionObject(operations.db(), CollectionCallback.fromOperations(operations), getDefaultTimeout(), holder);
} catch (ArangoDBException error) {
throw new TransactionSystemException("Cannot create transaction object", error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.StreamTransactionStatus;
import com.arangodb.model.StreamTransactionOptions;
import com.arangodb.springframework.core.template.CollectionCallback;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable;
Expand All @@ -46,11 +47,13 @@ class ArangoTransactionObject implements SmartTransactionObject {
private static final Log logger = LogFactory.getLog(ArangoTransactionObject.class);

private final ArangoDatabase database;
private final CollectionCallback collectionCallback;
private final ArangoTransactionHolder holder;
private int timeout;

ArangoTransactionObject(ArangoDatabase database, int defaultTimeout, @Nullable ArangoTransactionHolder holder) {
ArangoTransactionObject(ArangoDatabase database, CollectionCallback collectionCallback, int defaultTimeout, @Nullable ArangoTransactionHolder holder) {
this.database = database;
this.collectionCallback = collectionCallback;
this.holder = holder == null ? new ArangoTransactionHolder() : holder;
this.timeout = defaultTimeout;
}
Expand All @@ -71,6 +74,7 @@ void configure(TransactionDefinition definition) {
ArangoTransactionHolder getOrBegin(Collection<String> collections) throws ArangoDBException {
addCollections(collections);
if (holder.getStreamTransactionId() == null) {
holder.getCollectionNames().forEach(collectionCallback::collection);
StreamTransactionOptions options = new StreamTransactionOptions()
.allowImplicit(true)
.writeCollections(holder.getCollectionNames().toArray(new String[0]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.List;

import com.arangodb.springframework.annotation.PersistentIndex;
import org.springframework.data.annotation.Id;

import com.arangodb.springframework.annotation.Document;
Expand All @@ -33,6 +34,7 @@
* @author Christian Lechner
*/
@Document("actors")
@PersistentIndex(fields = "name")
public class Actor {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.arangodb.springframework.ArangoTransactionalTestConfiguration;
import com.arangodb.springframework.repository.ActorRepository;
import com.arangodb.springframework.repository.MovieRepository;
import com.arangodb.springframework.testdata.Actor;
import com.arangodb.springframework.testdata.Movie;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -18,7 +17,7 @@
public class ArangoTransactionManagerRepositoryTest extends AbstractArangoTest {

public ArangoTransactionManagerRepositoryTest() {
super(Movie.class, Actor.class);
super(Movie.class);
}

@Autowired
Expand Down Expand Up @@ -66,4 +65,10 @@ public void shouldRollbackWithinTransaction() {

assertThat(movieRepository.findById(starWars.getId())).isNotPresent();
}

@Test
@Transactional(label = "actors")
public void shouldCreateCollectionsBeforeTransaction() {
actorRepository.findAll();
}
}

0 comments on commit ef0a287

Please sign in to comment.