6

Is there a way to embed mongodb in the java application instead of running a daemon.

I have downloaded mongodb jdbc drivers and added them to classpath.

3
  • 4
    possible duplicate of Can MongoDB be hosted in-process? See also stackoverflow.com/questions/6115637/… Commented Jul 21, 2011 at 13:44
  • 1
    monogodb is written in c++, so you might want to check jni, but if you're thinking it has something to do with jdbc, then it's probably overkill for you :) Commented Jul 21, 2011 at 16:58
  • 1
    There is no jdbc driver for mongodb. The official driver/client-library is not a jdbc one but simply a custom API. Commented Feb 16, 2012 at 18:30

2 Answers 2

3

You can not embed mongodb in the java process. It is a separate process.

You can start and stop it when your program starts/stops by spawning the process. This is generally not suggested as mongodb is meant to run as a background server; if you do this is probably best you pick a random port to start it on so it doesn't affect any other mongodb instances. You will also need to make sure you include the correct binaries for the platform your java app is deployed on since it requires more than the JVM.

Sign up to request clarification or add additional context in comments.

3 Comments

Answer is not quite true, since you can run shell commands from java (with this apporach: stackoverflow.com/questions/2460297/run-shell-command-from-java). I'm also looking for such a solution and was wondering if any code already exsists. I'll keep this question updated if I find any solution or develope one on my own.
That is not embedded but simply spawned as a sep. process.
yes, I missunderstood the role of embedded... but still this appraoch would fit sam's need, don't you think?
0

In java unit tests we can use Test Containers to spawn a Docker container with embedded MongoDB.

See autoconfiguration for Spring Boot projects: https://github.com/testcontainers/testcontainers-spring-boot#embedded-mongodb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.