mongoDB4.0 用事务insert 报错,求指教!

self-transcendence 2018-11-14 04:16:04
十一月 14, 2018 4:05:21 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Cluster created with settings {hosts=[192.168.0.150:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
十一月 14, 2018 4:05:21 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Cluster description not yet available. Waiting for 30000 ms before timing out
十一月 14, 2018 4:05:21 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Opened connection [connectionId{localValue:1, serverValue:20}] to 192.168.0.150:27017
十一月 14, 2018 4:05:21 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Monitor thread successfully connected to server with description ServerDescription{address=192.168.0.150:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 3]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2683466}
Exception in thread "main" com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected
at com.mongodb.MongoClient.startSession(MongoClient.java:579)
at com.mongodb.MongoClient.startSession(MongoClient.java:564)
at com.hyzk.mongo.dao.MongoDBUtil.install(MongoDBUtil.java:29)
at com.hyzk.mongo.controller.MongoDBController.main(MongoDBController.java:35)

===================================================================================

public void install(List<BasicDBObject> list){
MongoClient mongo = new MongoClient("192.168.0.150", 27017);
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("test");
//据说是数据库不存在就创建该数据库
MongoDatabase mongoDatabase = mongo.getDatabase("user");
DBCollection table = db.getCollection("user");



ClientSession clientSession = mongo.startSession();
clientSession.startTransaction();
clientSession.commitTransaction();
table.insert(list);
clientSession.abortTransaction();

}

以上是我的代码
===================================================================================

请大牛们指导一下我
...全文
968 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lussionLiu 2018-12-14
  • 打赏
  • 举报
回复
事务只支持主从模式的mongodb,推荐一篇文章,教你安装支持事务的mongodb,并且支持spring boot注解事务。 https://blog.csdn.net/quanmaoluo5461/article/details/84880850
weixin_42625723 2018-12-08
  • 打赏
  • 举报
回复
意思是, 要试 transaction 这功能, 单节点的 MongoDB 不支持, 你得在 副本集的环境中才能使用. 分片集群目前应该也还没支持, 据说到 4.2 (2019) 才会支持
self-transcendence 2018-11-26
  • 打赏
  • 举报
回复
引用 1 楼 weixin_42625723 的回复:
我用 C# 代码, 在本机 单机(MongoDB 4) 测试与你相似思路的代码. 会在 insert 时 try...catch 捕获错误信息: Command insert failed: Transaction numbers are only allowed on a replica set member or mongos. 看来你得在副本集或分片的集群环境测试才可.
不太明白是什么意思
weixin_42625723 2018-11-14
  • 打赏
  • 举报
回复
我用 C# 代码, 在本机 单机(MongoDB 4) 测试与你相似思路的代码. 会在 insert 时 try...catch 捕获错误信息:

Command insert failed: Transaction numbers are only allowed on a replica set member or mongos.

看来你得在副本集或分片的集群环境测试才可.

1,798

社区成员

发帖
与我相关
我的任务
社区描述
MongoDB相关内容讨论区
社区管理员
  • MongoDB社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧