use of com.mongodb.session.ServerSession in project spring-data-mongodb by spring-projects.
the class MongoTransactionManagerUnitTests method suspendTransactionWhilePropagationRequiresNew.
// DATAMONGO-1920
@Test
void suspendTransactionWhilePropagationRequiresNew() {
when(dbFactory.withSession(session2)).thenReturn(dbFactory2);
when(dbFactory2.getMongoDatabase()).thenReturn(db2);
when(session2.getServerSession()).thenReturn(serverSession);
when(serverSession.isClosed()).thenReturn(false);
MongoTransactionManager txManager = new MongoTransactionManager(dbFactory);
TransactionStatus txStatus = txManager.getTransaction(new DefaultTransactionDefinition());
MongoTemplate template = new MongoTemplate(dbFactory);
template.execute(db -> {
db.drop();
return null;
});
TransactionTemplate txTemplate = new TransactionTemplate(txManager);
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
txTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
template.execute(db -> {
db.drop();
return null;
});
}
});
template.execute(MongoDatabase::listCollections);
txManager.commit(txStatus);
verify(session).startTransaction();
verify(session2).startTransaction();
verify(dbFactory, times(2)).withSession(eq(session));
verify(dbFactory).withSession(eq(session2));
verify(db).drop();
verify(db2).drop();
verify(db).listCollections();
verify(session).close();
verify(session2).close();
}
use of com.mongodb.session.ServerSession in project spring-data-mongodb by spring-projects.
the class ReactiveMongoDatabaseUtilsUnitTests method shouldParticipateInOngoingMongoTransactionWhenSessionSychronizationIsNative.
// DATAMONGO-2265
@Test
void shouldParticipateInOngoingMongoTransactionWhenSessionSychronizationIsNative() {
when(session.getServerSession()).thenReturn(serverSession);
when(databaseFactory.getSession(any())).thenReturn(Mono.just(session));
ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
when(session.abortTransaction()).thenReturn(Mono.empty());
TransactionalOperator operator = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
operator.execute(tx -> {
return TransactionSynchronizationManager.forCurrentTransaction().doOnNext(synchronizationManager -> {
assertThat(synchronizationManager.isSynchronizationActive()).isTrue();
assertThat(tx.isNewTransaction()).isTrue();
assertThat(synchronizationManager.hasResource(databaseFactory)).isTrue();
}).then(Mono.fromRunnable(tx::setRollbackOnly));
}).as(StepVerifier::create).verifyComplete();
verify(session).startTransaction();
verify(session).abortTransaction();
verify(session).close();
}
use of com.mongodb.session.ServerSession in project spring-data-mongodb by spring-projects.
the class ReactiveMongoDatabaseUtilsUnitTests method isTransactionActiveShouldLookupTxForActiveTransactionSynchronizationViaTxManager.
// DATAMONGO-2265
@Test
void isTransactionActiveShouldLookupTxForActiveTransactionSynchronizationViaTxManager() {
when(session.getServerSession()).thenReturn(serverSession);
when(session.hasActiveTransaction()).thenReturn(true);
when(databaseFactory.getSession(any())).thenReturn(Mono.just(session));
when(databaseFactory.isTransactionActive()).thenReturn(false);
when(session.commitTransaction()).thenReturn(Mono.empty());
ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
TransactionalOperator operator = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
operator.execute(tx -> {
return ReactiveMongoDatabaseUtils.isTransactionActive(databaseFactory);
}).as(StepVerifier::create).expectNext(true).verifyComplete();
}
Aggregations