micronaut-data
micronaut-data copied to clipboard
Caused by: com.mongodb.MongoCommandException: Command failed with error 20 (IllegalOperation): 'Transaction numbers are only allowed on a replica set member or mongos'
Expected Behavior
Micronaut Data MongoDB documentation should contain information about:
- Automatic transaction management (how to disable it)
- How to configure MongoDB connection string if we don't have replica set but still need transactions
Because now Micronaut Data MongoDB is completely unusable.
Actual Behaviour
Hi
We tried to migrated our Micronaut application to use Micronaut Data Mongo but encountered a lot of issues, for example, this runtime errors:
Caused by: com.mongodb.MongoClientException: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.
at com.mongodb.internal.operation.CommandOperationHelper.transformWriteException(CommandOperationHelper.java:634)
at com.mongodb.internal.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:209)
at com.mongodb.internal.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:77)
Caused by: com.mongodb.MongoCommandException: Command failed with error 20 (IllegalOpera-tion): 'Transaction numbers are only allowed on a replica set member or mongos' on server lo-calhost:49882. The full response is {"ok": 0.0, "errmsg": "Transaction numbers are only allowed on a replica set member or mongos", "code": 20, "codeName": "IllegalOperation"}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:205)
So it seems that Micronaut Data automatically starts transaction when executing insert/update operations. Unfortunately documentation has no information about that and how to disable transactions if we don't need them. But the main issue is that even if we add retryWrites to connection string then issue still reproduces:
mongodb:
uri: mongodb://test:test@${MONGO_HOST:localhost}:${MONGO_PORT:27017}/?retryWrites=false
Steps To Reproduce
No response
Environment Information
No response
Example Application
No response
Version
4.1.4
I assume you are using @MicronautTest which is using the TX by default, you need @MicronautTest(transactional=false)
I assume you are using
@MicronautTestwhich is using the TX by default, you need@MicronautTest(transactional=false)
I just read your guide about transactions and tests: https://micronaut-projects.github.io/micronaut-test/latest/guide/#junit5TransactionSemantics
And here's statement:
By default, if org.springframework:spring-tx is in the test classpath (e.g. transitively via io.micronaut.configuration:micronaut-hibernate-jpa-spring), when using @MicronautTest, each @Test method will be wrapped in a transaction that will be rolled back when the test finishes
I don't have org.springframework:spring-tx is in the test classpath so why do you start transaction each time the test starts ?
@dstepanov Another question which I couldn't find answer in the documentation - how to globally disable transactions in the production code?
You cannot disable it globally for MongoDB. The documentation was written when we only had JPA, and TX manager was only one.
Have you tried @MicronautTest(transactional=false)?
You cannot disable it globally for MongoDB. The documentation was written when we only had JPA, and TX manager was only one.
Have you tried
@MicronautTest(transactional=false)?
Yes, that worked. But we had to manually disable transactions using @Transactional(NOT_SUPPORTED) which is not very convenient.
I don’t think it’s needed to add not supported