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.

Vivek
  • 11,938
  • 19
  • 92
  • 127
  • 4
    possible duplicate of [Can MongoDB be hosted in-process?](http://stackoverflow.com/questions/2210029/can-mongodb-be-hosted-in-process) See also http://stackoverflow.com/questions/6115637/can-mongodb-be-used-as-an-embedded-database – Matt Ball Jul 21 '11 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 :) – milan Jul 21 '11 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. – Scott Hernandez Feb 16 '12 at 18:30

2 Answers2

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.

Scott Hernandez
  • 7,452
  • 2
  • 34
  • 25
  • Answer is not quite true, since you can run shell commands from java (with this apporach: http://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. – Ed Michel Feb 16 '12 at 12:20
  • 2
    That is not embedded but simply spawned as a sep. process. – Scott Hernandez Feb 16 '12 at 18:30
  • yes, I missunderstood the role of embedded... but still this appraoch would fit sam's need, don't you think? – Ed Michel Feb 17 '12 at 08:58
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

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259