spring-boot-mongodb-transaction
Today, I need to use the tansaction of mongodb in my work, so I consulted various materials and stepped on a lot of pits. Here is a summary.
Premise
- The version of MongoDB must be version 4.0 or above
- Introduce spring-data
Configure
1 |
|
After we finished the configuration, all we need to do to use native MongoDB transactions – is to annotate our method with @Transactional.
Everything inside the annotated method will be executed in one transaction:
1 |
|
Note that we can’t use listCollections command inside a multi-document transaction – for example:
1 |
|
This example throws a MongoTransactionException as we used the collectionExists() method.
We also can’t run count inside a multi-document transaction:
1 |
|
But, we can work around this one with a simple query and then get the size of the resulting list:
1 |
|
Precautions
Annotation must be @Transactional (rollbackFor = {Exception.class}), not just @Transactional
Replica-set must be configured in the properties file, otherwise the error “Sessions are not supported by the MongoDB cluster to which this client is connected” will be reported. The configuration method is spring.data mongodb.uri=mongodb://host1:port1,host2:port2,host3:port3/Database name? replicaSet = replicaset name
This is because transactions must be based on replica sets to achieve
If there is a catch exception, you must add a line of code, otherwise you can still insert into the database when an exception occurs; you can only roll back the insert operation after adding it
1
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly
No need to introduce the spring-data-mongodb package, just spring-boot-starter-data-mongodb, spring-data-commons and mongo-java-driver.
@Transactional can only be applied to public methods
@Transactional configuration item
Property Name | Description |
---|---|
name | When there are multiple TransactionManagers in the configuration file, you can use this property to specify which transaction manager to choose. |
propagation | The propagation behavior of transactions, the default value is REQUIRED. |
Isolation | The isolation of the transaction, the default value is DEFAULT. |
Timeout | The timeout time for the transaction, the default value is -1. If the time limit is exceeded but the transaction has not yet completed, the transaction is automatically rolled back. |
Read-only | Specifies whether the transaction is read-only, the default value is false; in order to ignore methods that do not require transactions, such as reading data, you can set read-only to true. |
rollback-for | is used to specify the exception type that can trigger transaction rollback. If there are multiple exception types to be specified, the types can be separated by commas. |
Throws the exception type specified by no-rollback-for, and does not roll back the transaction. |
In addition, the @Transactional annotation can also be added at the class level. When the @Transactional annotation is placed at the class level, it means that all public methods of the class are configured with the same transaction attribute information.