com.mongodb.TransactionOptions Java Examples

The following examples show how to use com.mongodb.TransactionOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoDB.java    From aion with MIT License 5 votes vote down vote up
@Override
public boolean open() {
    if (isOpen()) {
        return true;
    }

    LOG.info("Initializing MongoDB at {}", mongoClientUri);

    // Get the client and create a session for this instance
    MongoClient mongoClient =
            MongoConnectionManager.inst().getMongoClientInstance(this.mongoClientUri);
    ClientSessionOptions sessionOptions =
            ClientSessionOptions.builder()
                    .causallyConsistent(true)
                    .defaultTransactionOptions(
                            TransactionOptions.builder()
                                    .readConcern(ReadConcern.DEFAULT)
                                    .writeConcern(WriteConcern.MAJORITY)
                                    .readPreference(ReadPreference.nearest())
                                    .build())
                    .build();
    this.clientSession = mongoClient.startSession(sessionOptions);

    // Get the database and our collection. Mongo takes care of creating these if they don't
    // exist
    MongoDatabase mongoDb = mongoClient.getDatabase(MongoConstants.AION_DB_NAME);

    // Gets the collection where we will be saving our values. Mongo creates it if it doesn't
    // yet exist
    this.collection = mongoDb.getCollection(this.name, BsonDocument.class);

    LOG.info("Finished opening the Mongo connection");
    return isOpen();
}
 
Example #2
Source File: MongoDBContainerTest.java    From testcontainers-java with MIT License 4 votes vote down vote up
/**
 * Taken from <a href="https://docs.mongodb.com/manual/core/transactions/">https://docs.mongodb.com</a>
 */
@Test
public void shouldExecuteTransactions() {
    try (
        // creatingMongoDBContainer {
        final MongoDBContainer mongoDBContainer = new MongoDBContainer()
        // }
    ) {

        // startingMongoDBContainer {
        mongoDBContainer.start();
        // }

        final String mongoRsUrl = mongoDBContainer.getReplicaSetUrl();
        assertNotNull(mongoRsUrl);
        final MongoClient mongoSyncClient = MongoClients.create(mongoRsUrl);
        mongoSyncClient.getDatabase("mydb1").getCollection("foo")
            .withWriteConcern(WriteConcern.MAJORITY).insertOne(new Document("abc", 0));
        mongoSyncClient.getDatabase("mydb2").getCollection("bar")
            .withWriteConcern(WriteConcern.MAJORITY).insertOne(new Document("xyz", 0));

        final ClientSession clientSession = mongoSyncClient.startSession();
        final TransactionOptions txnOptions = TransactionOptions.builder()
            .readPreference(ReadPreference.primary())
            .readConcern(ReadConcern.LOCAL)
            .writeConcern(WriteConcern.MAJORITY)
            .build();

        final String trxResult = "Inserted into collections in different databases";

        TransactionBody<String> txnBody = () -> {
            final MongoCollection<Document> coll1 =
                mongoSyncClient.getDatabase("mydb1").getCollection("foo");
            final MongoCollection<Document> coll2 =
                mongoSyncClient.getDatabase("mydb2").getCollection("bar");

            coll1.insertOne(clientSession, new Document("abc", 1));
            coll2.insertOne(clientSession, new Document("xyz", 999));
            return trxResult;
        };

        try {
            final String trxResultActual = clientSession.withTransaction(txnBody, txnOptions);
            assertEquals(trxResult, trxResultActual);
        } catch (RuntimeException re) {
            throw new IllegalStateException(re.getMessage(), re);
        } finally {
            clientSession.close();
            mongoSyncClient.close();
        }
    }
}
 
Example #3
Source File: ClientSessionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
@Override
public TransactionOptions getTransactionOptions() {
    return wrapped.getTransactionOptions();
}
 
Example #4
Source File: ClientSessionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
@Override
public void startTransaction(final TransactionOptions transactionOptions) {
     wrapped.startTransaction(transactionOptions);
}
 
Example #5
Source File: BaseMorphiaSession.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public TransactionOptions getTransactionOptions() {
    return session.getTransactionOptions();
}
 
Example #6
Source File: BaseMorphiaSession.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public void startTransaction(final TransactionOptions transactionOptions) {
    session.startTransaction(transactionOptions);
}
 
Example #7
Source File: BaseMorphiaSession.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T withTransaction(final TransactionBody<T> transactionBody, final TransactionOptions options) {
    return session.withTransaction(transactionBody, options);
}
 
Example #8
Source File: ClientSession.java    From mongo-java-driver-reactivestreams with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the transaction options.  Only call this method of the session has an active transaction
 *
 * @return the transaction options
 */
TransactionOptions getTransactionOptions();
 
Example #9
Source File: ClientSession.java    From mongo-java-driver-reactivestreams with Apache License 2.0 2 votes vote down vote up
/**
 * Start a transaction in the context of this session with the given transaction options. A transaction can not be started if there is
 * already an active transaction on this session.
 *
 * @param transactionOptions the options to apply to the transaction
 *
 * @mongodb.server.release 4.0
 */
void startTransaction(TransactionOptions transactionOptions);