Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

mongodb - PyMongo transaction error:Transaction numbers are only allowed on a replica set member or mongos

When I am using pymongo 3.7 transaction function to connect to mongo server 4.0, this error "Transaction numbers are only allowed on a replica set member or mongos" occurred and I cannot find any answer to solve this problem. My code is:

from pymongo import MongoClient
conn = MongoClient(host, port)
tb = conn.collector_gateway.try_table
with conn.start_session() as session:
    with session.start_transaction():
        tb.insert_one({"sku": "abc123", "qty": 100}, session=session)

The error is:

Traceback (most recent call last):
  File "/Users/yuzgu/PycharmProjects/seller_loss_alert/try_mongo.py", line 22, in <module>
    tb.insert_one({"sku": "abc123", "qty": 100}, session=session)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/collection.py", line 693, in insert_one
    session=session),
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/collection.py", line 607, in _insert
    bypass_doc_val, session)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/collection.py", line 595, in _insert_one
    acknowledged, _insert_command, session)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1243, in _retryable_write
    return self._retry_with_session(retryable, func, s, None)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1196, in _retry_with_session
    return func(session, sock_info, retryable)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/collection.py", line 590, in _insert_command
    retryable_write=retryable_write)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/pool.py", line 579, in command
    unacknowledged=unacknowledged)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/network.py", line 150, in command
    parse_write_concern_error=parse_write_concern_error)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/helpers.py", line 155, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Transaction numbers are only allowed on a replica set member or mongos

Since mongo transaction function was published this year, I couldn't find the solution, can anyone help me with this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Transactions are only available in a replica set setup (https://docs.mongodb.com/master/core/transactions/#transactions-and-replica-sets):

Multi-document transactions are available for replica sets only.

This error message is shown when you are attempting to do a transactional operation on a standalone mongod instance.

Please see Transactions page for more details & requirements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...