This is a demo project showcasing the integration of Spring Boot with MongoDB. It includes multiple methods for updating MongoDB collections using various transaction management techniques. The project is developed and tested using Visual Studio Code.
- Java 17
- Maven
- MongoDB
git clone https://github.com/rafagalinarispain/trxspringbootPOC.gitBuild:
mvn clean packageRun:
mvn spring-boot:runAlternatively, you can use the built-in tools in Visual Studio Code to build and run the project.
-
Database and Collections:
Ensure your MongoDB instance contains a database nameddb1with the following collections:coll1,coll2,coll3,coll4, andcoll5. Each collection should follow the schema below:{ _id: ObjectId('6739c4063ac3a50065be690b'), a: 1, b: 20 }
-
Profiling and Logging:
To verify how writes are processed and determine if they are attached to a transaction, adjust the profiling level and increase the logging verbosity for thetransactioncomponent:db.setProfilingLevel(0, { "slowms": 0 }); db.setLogLevel(1, "transaction");
Once the project is running, you can update collections via POST requests. The project provides five endpoints, each targeting a specific collection. Example usage with curl:
curl -X POST http://localhost:8080/update/coll1/10
curl -X POST http://localhost:8080/update/coll2/20
curl -X POST http://localhost:8080/update/coll3/30
curl -X POST http://localhost:8080/update/coll4/40
curl -X POST http://localhost:8080/update/coll5/50- coll1 & coll2:
Uses a transaction manager constructed as a Java Bean with the@Transactionalannotation. - coll3:
Updates the collection with explicit usage of the transaction manager. - coll4:
Updates the collection using a transaction within theClientSessionmechanism. - coll5:
UsesTransactionTemplateto update the collection.
Edit the application.properties file to provide a reachable MongoDB URI:
spring.data.mongodb.uri=mongodb://localhost:27017/your-database- Open the project folder in VSCode.
- Use the built-in test runner to run tests. Click "Run Test" or "Debug Test" above the test method in your test file.
- View the test results in the Test Explorer panel.
- Set breakpoints in your code by clicking in the gutter next to the line numbers.
- Click "Debug Test" above the test method or select "Debug" from the context menu.
- Inspect variables in the Variables panel and step through the code using the Step Over, Step Into, and Step Out buttons.
- Ensure that your MongoDB instance is running before starting the application.
- For security, do not expose sensitive credentials or data while configuring MongoDB URIs.
- This PoC was built by a Spring Boot/Java dummy, so any contributions or suggestions for improvement are warmly welcomed!