20190410
第一次写博客,留影做个纪念
mongoldb 事务
看《mongodb应用设计模式》,看到了里面的事务模拟,感觉挺繁琐的,突然想起mongo4好像支持事务了,就百度了一下,实际测试一下.
mongodb事务需要mongo4以上版本,且是在副本集下,本机装了mongo3.2升级失败,就选择了docker部署,参考下面网址:
https://www.cnblogs.com/cowboys/p/9264494.html
-
部署成功后用可视化工具连到副本集
springboot maven项目搭建
https://gitee.com/humaohua/mongo-transaction.git
- 项目运行时会报错下面错误(需要在shell里创建数据库,集合):
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Apr 10 18:12:47 CST 2019
There was an unexpected error (type=Internal Server Error, status=500).
Command failed with error 263 (OperationNotSupportedInTransaction): 'Cannot create namespace testdb1.test in multi-document transaction.' on server 192.168.0.12:37017. The full response is { "operationTime" : { "$timestamp" : { "t" : 1554891165, "i" : 1 } }, "ok" : 0.0, "errmsg" : "Cannot create namespace testdb1.test in multi-document transaction.", "code" : 263, "codeName" : "OperationNotSupportedInTransaction", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1554891165, "i" : 1 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }; nested exception is com.mongodb.MongoCommandException: Command failed with error 263 (OperationNotSupportedInTransaction): 'Cannot create namespace testdb1.test in multi-document transaction.' on server 192.168.0.12:37017. The full response is { "operationTime" : { "$timestamp" : { "t" : 1554891165, "i" : 1 } }, "ok" : 0.0, "errmsg" : "Cannot create namespace testdb1.test in multi-document transaction.", "code" : 263, "codeName" : "OperationNotSupportedInTransaction", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1554891165, "i" : 1 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
-
解决方法可以手动使用mongo shell 创建数据库和集合
-
再测试创建数据,就不会报错了
http://localhost:8080/test/t1?rollback=false
-
测试事务回滚,不会插入数据
http://localhost:8080/test/t1?rollback=true