Skip to content
广告❤️成为赞助商

Transactions

MongoPlus provides support for MongoDB transactions (declarative and programmatic), but the prerequisite is that MongoDB must have replica sets enabled and the version must be 4 or above.

Configuration File

  • We need to configure some additional settings in the configuration file:
yaml
mongo-plus:
  data:
    mongodb:
      host: 192.168.110.1,192.168.110.1
      port: 27017,27018
      database: mp
      connectTimeoutMS: 50000
      retryWrites: true
      w: majority
      replicaSet: mp    # specify the replica set name

Declarative Transactions -> @MongoTransactional Annotation

  • No need for manual try-catch or commit/rollback; just a single annotation is enough.
  • By marking a method with this annotation, you can easily perform MongoDB transaction operations.

Using Spring's @Transactional Annotation:

  • MongoPlus supports using Spring's @Transactional annotation to control transactions.
  • Note that Spring has deprecated the class that combines transaction managers, and currently, there is no replacement.
  • When using it, you need to enable this configuration. For details, see Configuration -> Configuration File -> Spring Config -> transaction section. Since combining managers is not possible, MongoPlus' transaction manager will be used.

Declarative Transaction Example:

  • Here is a simple example using declarative transactions:
java
@PostMapping("/addRole")
@ApiOperation("Add Role")
// Use transaction annotation
@MongoTransactional
public Boolean addRole(@RequestBody Role role){
    return roleService.save(role);
}

Programmatic Transactions -> MongoTransactionalOperate Class

  • This class provides operations for transactions, including creating, starting, committing, rolling back, and closing transactions.

createTransaction()

  • Creates a transaction and returns a ClientSession interface, which contains the transaction information.

startTransaction()

  • Creates and starts a transaction, returning a ClientSession interface.

startTransaction(ClientSession clientSession)

  • Starts a transaction using an existing ClientSession passed manually.

commitTransaction()

  • Commits a transaction using a manually provided ClientSession.

commitAndCloseTransaction(ClientSession clientSession)

  • Commits and closes a transaction using a manually provided ClientSession.

rollbackTransaction(ClientSession clientSession)

  • Rolls back a transaction using a manually provided ClientSession.

rollbackAndCloseTransaction(ClientSession clientSession)

  • Rolls back and closes a transaction using a manually provided ClientSession.

closeTransaction(ClientSession clientSession)

  • Closes a transaction using a manually provided ClientSession.

Programmatic Transaction Example:

  • Here is a simple example demonstrating programmatic transaction operations:
java
// Create and start a transaction
ClientSession clientSession = MongoTransactionalOperate.startTransaction();
try {
    // Execute delete operation
    userService.removeById(clientSession,1);
    int i = 1 / 0;
}catch (Exception e){
    // Rollback and close the transaction
    MongoTransactionalOperate.rollbackAndCloseTransaction(clientSession);
    return false;
}
// Commit and close the transaction
MongoTransactionalOperate.commitAndCloseTransaction(clientSession);
return true;