Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

Join us at AWS re:Invent 2024! Learn how to use MongoDB for AI use cases.
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

Java 21: Unlocking the Power of the MongoDB Java Driver With Virtual Threads

Maxime Beugnet2 min read • Published Jan 31, 2024 • Updated Jan 31, 2024
SpringMongoDBJava
Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

Greetings, dev community! Java 21 is here, and if you're using the MongoDB Java driver, this is a ride you won't want to miss. Increased performances and non-blocking threads are on the menu today! 🚀
In this article, we're going to take a stroll through some of the key features of Java 21 that are not just exciting for Java devs in general but are particularly juicy for those of us pushing the boundaries with MongoDB.

JDK 21

To begin with, let's have a look at all the features released in Java 21, also known as JDK Enhancement Proposal (JEP).

The Project Loom and MongoDB Java driver 4.11

While some of these JEPs, like deprecations, might not be the most exciting, some are more interesting, particularly these three.
Let's discuss a bit more about them.
These three JEPs are closely related to the Project Loom which is an initiative within the Java ecosystem that introduces lightweight threads called virtual threads. These virtual threads simplify concurrent programming, providing a more scalable and efficient alternative to traditional heavyweight threads.
With Project Loom, developers can create thousands of virtual threads without the typical performance overhead, making it easier to write concurrent code. Virtual threads offer improved resource utilization and simplify code maintenance, providing a more accessible approach to managing concurrency in Java applications. The project aims to enhance the developer experience by reducing the complexities associated with thread management while optimizing performance.
Since version 4.11 of the MongoDB Java driver, virtual threads are fully supported.
If you want more details, you can read the epic in the MongoDB Jira which explains the motivations for this support.
You can also read more about the Java driver’s new features and compatibility.

Spring Boot and virtual threads

In Spring Boot 3.2.0+, you just have to add the following property in your application.properties file to enable virtual threads.
1spring.threads.virtual.enabled=true
It's huge because this means that your accesses to MongoDB resources are now non-blocking — thanks to virtual threads.
This is going to dramatically improve the performance of your back end. Managing a large workload is now easier as all the threads are non-blocking by default and the overhead of the context switching for the platform threads is almost free.
You can read the blog post from Dan Vega to learn more about Spring Boot and virtual threads.

Conclusion

Java 21's recent release has unleashed exciting features for MongoDB Java driver users, particularly with the introduction of virtual threads. Since version 4.11, these lightweight threads offer a streamlined approach to concurrent programming, enhancing scalability and efficiency.
For Spring Boot enthusiasts, embracing virtual threads is a game-changer for backend performance, making MongoDB interactions non-blocking by default.
Curious to experience these advancements? Dive into the future of Java development and explore MongoDB with Spring Boot using the Java Spring Boot MongoDB Starter in GitHub.
If you don't have one already, claim your free MongoDB cluster in MongoDB Atlas to get started with the above repository faster.
Any burning questions? Come chat with us in the MongoDB Community Forums.
Happy coding! 🚀

Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

How to Secure MongoDB Data Access with Views


Apr 07, 2023 | 4 min read
Article

Document Validation for Polymorphic Collections


Oct 01, 2024 | 6 min read
Quickstart

Getting Started With MongoDB and FastAPI


Jul 12, 2024 | 7 min read
Article

Using MongoDB Change Streams in Java


Aug 28, 2024 | 6 min read
Table of Contents