micronaut-data icon indicating copy to clipboard operation
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'

Open sergey-morenets opened this issue 1 year ago • 6 comments

Expected Behavior

Micronaut Data MongoDB documentation should contain information about:

  1. Automatic transaction management (how to disable it)
  2. 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

sergey-morenets avatar Nov 09 '23 21:11 sergey-morenets

I assume you are using @MicronautTest which is using the TX by default, you need @MicronautTest(transactional=false)

dstepanov avatar Nov 13 '23 14:11 dstepanov

I assume you are using @MicronautTest which 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 ?

sergey-morenets avatar Nov 13 '23 16:11 sergey-morenets

@dstepanov Another question which I couldn't find answer in the documentation - how to globally disable transactions in the production code?

sergey-morenets avatar Nov 13 '23 17:11 sergey-morenets

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)?

dstepanov avatar Nov 13 '23 17:11 dstepanov

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.

sergey-morenets avatar Nov 13 '23 19:11 sergey-morenets

I don’t think it’s needed to add not supported

dstepanov avatar Nov 13 '23 19:11 dstepanov