当前版本:Mongodb 4.4.2 springboot 2.3.2.RELEASE
Mongodb官方文档涉及事务的文案
1)读选择
Multi-document transactions that contain read operations must use read preference primary. All operations in a given transaction must route to the same member.
翻译:包含读取操作的多文档事务处理必须使用读取首选项主。给定事务中的所有操作都必须路由到同一成员。
read preference = primary
2)读关注点
For operations in multi-document transactions, read concern "majority" provides its guarantees only if the transaction commits with write concern "majority". Otherwise, the "majority" read concern provides no guarantees about the data read in transactions.
Regardless of the read concern level, the most recent data on a node may not reflect the most recent version of the data in the system.
翻译:对于多文档事务中的操作,只有当事务以写关注点“多数”提交时,读关注点“大多数”才提供其保证。否则,“多数”读取问题无法保证事务中读取的数据。
无论读取关注级别如何,节点上的最新数据都可能无法反映系统中数据的最新版本。
结论:
1、官方mongodb多文本事务只支持 read preference = primary,所有 springboot 集成mongodb使用事务时,直接使用默认配置即可
2、读关注 read Concern "majority"和 write Concern "majority" 同时出现才有意义。(正常都是出现在事务中)对读写分离来说,没有什么必然的联系,配置了也不能保证读写分离一定读取到最新的从库消息。
3、是否使用读写分离的问题,对数据实时性要求不高,例如:消费订单,历史记录。。
4、关于在mongodb使用事务时,事务未提交,其他线程读取事务中操作的文档时,
- 选择read preference =primary 没有延迟直接返回结果。
- 选择read preference = secondaryPreferred 有大概1秒延迟后返回结果
目前在研究mongodb的官方文档
后续会有其他的技术分享。。。