Skip to content

Commit ef0a287

Browse files
committed
#250 ensure collections before transaction start
1 parent 5ed1cc6 commit ef0a287

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/main/java/com/arangodb/springframework/transaction/ArangoTransactionManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import com.arangodb.ArangoDBException;
2424
import com.arangodb.ArangoDatabase;
25-
import com.arangodb.DbName;
2625
import com.arangodb.springframework.core.ArangoOperations;
26+
import com.arangodb.springframework.core.template.CollectionCallback;
2727
import com.arangodb.springframework.repository.query.QueryTransactionBridge;
2828
import org.springframework.beans.factory.InitializingBean;
2929
import org.springframework.lang.Nullable;
@@ -67,10 +67,10 @@ public void afterPropertiesSet() {
6767
throw new IllegalStateException("Nested transactions must not be allowed");
6868
}
6969
if (!isGlobalRollbackOnParticipationFailure()) {
70-
throw new IllegalStateException("Global rollback on participating failure is needed");
70+
throw new IllegalStateException("Global rollback on participating failure is required");
7171
}
7272
if (getTransactionSynchronization() == SYNCHRONIZATION_NEVER) {
73-
throw new IllegalStateException("Transaction synchronization is needed always");
73+
throw new IllegalStateException("Transaction synchronization must not be disabled");
7474
}
7575
}
7676

@@ -81,7 +81,7 @@ public void afterPropertiesSet() {
8181
protected ArangoTransactionObject doGetTransaction() {
8282
ArangoTransactionHolder holder = (ArangoTransactionHolder) TransactionSynchronizationManager.getResource(this);
8383
try {
84-
return new ArangoTransactionObject(operations.db(), getDefaultTimeout(), holder);
84+
return new ArangoTransactionObject(operations.db(), CollectionCallback.fromOperations(operations), getDefaultTimeout(), holder);
8585
} catch (ArangoDBException error) {
8686
throw new TransactionSystemException("Cannot create transaction object", error);
8787
}

src/main/java/com/arangodb/springframework/transaction/ArangoTransactionObject.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.arangodb.ArangoDatabase;
2525
import com.arangodb.entity.StreamTransactionStatus;
2626
import com.arangodb.model.StreamTransactionOptions;
27+
import com.arangodb.springframework.core.template.CollectionCallback;
2728
import org.apache.commons.logging.Log;
2829
import org.apache.commons.logging.LogFactory;
2930
import org.springframework.lang.Nullable;
@@ -46,11 +47,13 @@ class ArangoTransactionObject implements SmartTransactionObject {
4647
private static final Log logger = LogFactory.getLog(ArangoTransactionObject.class);
4748

4849
private final ArangoDatabase database;
50+
private final CollectionCallback collectionCallback;
4951
private final ArangoTransactionHolder holder;
5052
private int timeout;
5153

52-
ArangoTransactionObject(ArangoDatabase database, int defaultTimeout, @Nullable ArangoTransactionHolder holder) {
54+
ArangoTransactionObject(ArangoDatabase database, CollectionCallback collectionCallback, int defaultTimeout, @Nullable ArangoTransactionHolder holder) {
5355
this.database = database;
56+
this.collectionCallback = collectionCallback;
5457
this.holder = holder == null ? new ArangoTransactionHolder() : holder;
5558
this.timeout = defaultTimeout;
5659
}
@@ -71,6 +74,7 @@ void configure(TransactionDefinition definition) {
7174
ArangoTransactionHolder getOrBegin(Collection<String> collections) throws ArangoDBException {
7275
addCollections(collections);
7376
if (holder.getStreamTransactionId() == null) {
77+
holder.getCollectionNames().forEach(collectionCallback::collection);
7478
StreamTransactionOptions options = new StreamTransactionOptions()
7579
.allowImplicit(true)
7680
.writeCollections(holder.getCollectionNames().toArray(new String[0]))

src/test/java/com/arangodb/springframework/testdata/Actor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.List;
2424

25+
import com.arangodb.springframework.annotation.PersistentIndex;
2526
import org.springframework.data.annotation.Id;
2627

2728
import com.arangodb.springframework.annotation.Document;
@@ -33,6 +34,7 @@
3334
* @author Christian Lechner
3435
*/
3536
@Document("actors")
37+
@PersistentIndex(fields = "name")
3638
public class Actor {
3739

3840
@Id

src/test/java/com/arangodb/springframework/transaction/ArangoTransactionManagerRepositoryTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.arangodb.springframework.ArangoTransactionalTestConfiguration;
55
import com.arangodb.springframework.repository.ActorRepository;
66
import com.arangodb.springframework.repository.MovieRepository;
7-
import com.arangodb.springframework.testdata.Actor;
87
import com.arangodb.springframework.testdata.Movie;
98
import org.junit.Test;
109
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,7 +17,7 @@
1817
public class ArangoTransactionManagerRepositoryTest extends AbstractArangoTest {
1918

2019
public ArangoTransactionManagerRepositoryTest() {
21-
super(Movie.class, Actor.class);
20+
super(Movie.class);
2221
}
2322

2423
@Autowired
@@ -66,4 +65,10 @@ public void shouldRollbackWithinTransaction() {
6665

6766
assertThat(movieRepository.findById(starWars.getId())).isNotPresent();
6867
}
68+
69+
@Test
70+
@Transactional(label = "actors")
71+
public void shouldCreateCollectionsBeforeTransaction() {
72+
actorRepository.findAll();
73+
}
6974
}

0 commit comments

Comments
 (0)