flapdoodle-oss / de.flapdoodle.embed.mongo.spring

embedded mongo spring integration
Apache License 2.0
30 stars 7 forks source link

Spring Transaction with mongodb not working #3

Closed haiderali22 closed 1 year ago

haiderali22 commented 1 year ago

I was working on a sample to test transaction with mongodb and spring boot and as per the docs i have also provided the MongoTransactionManager bean and set the replica property but still not working as expected

GitHub repo link https://github.com/haiderali22/mongodb-transaction-test

michaelmosmann commented 1 year ago

@haiderali22 i will have a look at this.. (later this day) thanks for the repo:)

haiderali22 commented 1 year ago

you can run TestTransaction(addTransactionalTest) to verify this

michaelmosmann commented 1 year ago

@haiderali22 .. hmm.. did not had the time yesterday.. will do today.

michaelmosmann commented 1 year ago

@haiderali22 .. first finding: you should not mix spring and flapdoodle property prefix .. spring will win.. if you change it to either spring or flapdoodle:

de.flapdoodle.mongodb.embedded.storage.replSetName=testing de.flapdoodle.mongodb.embedded.storage.oplogSize=10 de.flapdoodle.mongodb.embedded.version=5.0.5

.. then mongodb will not start as he states that replSetName does not work with --nojournal

michaelmosmann commented 1 year ago

@haiderali22 .. and it looks like that you may need more then just a replica set name to make it work (https://www.baeldung.com/spring-data-mongodb-transactions) .. i will try to create a working solution for this, but it is a little bit more complicated...

michaelmosmann commented 1 year ago

@haiderali22 ok.. it looks like you need a working replica set to make this work: https://stackoverflow.com/questions/50255195/how-to-configure-a-mongodb-cluster-which-supports-sessions

haiderali22 commented 1 year ago

@michaelmosmann I tested the code by creating a docker container with following docker compose file

version: "3.8" services: mongodb: image: mongo:latest container_name: mongodb-transaction hostname: mongodb restart: on-failure environment:

The transaction part is working fine, i was using this library to see if i can write test cases with embeded database

michaelmosmann commented 1 year ago

Ah.. thanks.. I think rs.initiate() is important.. I will see if I can make this work.

michaelmosmann commented 1 year ago

@haiderali22 .. yes.. rs.initiate() did the trick..will make a new release and will tests this with your example:) .. in the next days..

michaelmosmann commented 1 year ago

@haiderali22 i made an pull request with a working solution.. as you see i am using the new beta-version of this framework .. as this kind of change is hard in the legacy code base.. i will fine tune this a little bit, improve the documentation and will make beta to the new release version..

Can you try if this solves your problem? there are very subtle tweaks.. f.i. you can not create the service instance yourself as spring has no way to intercept the method call..

haiderali22 commented 1 year ago

@michaelmosmann thankyou working fine with suggested changes

michaelmosmann commented 1 year ago

@haiderali22 .. did a cleanup.. now even less changes needed:)

michaelmosmann commented 1 year ago

@haiderali22 .. close it it works for you:)