Reputation: 103
I'm having trouble with persisting data using de.flapdoodle.embed.mongo. I've followed the documentation and examples I found online, but when I shut down my application, the data disappears.
Here's the code I'm using to start the embedded MongoDB server:
Mongod mongod = new Mongod() {
@Override
public Transition<PersistentDir> persistentBaseDir() {
return Start.to(PersistentDir.class)
.providedBy(PersistentDir.inUserHome(".embeddedMongodbCustomPath")
.mapToUncheckedException(RuntimeException::new));
}
};
TransitionWalker.ReachedState<RunningMongodProcess> running = Mongod.builder()
.net(Start.to(Net.class)
.initializedWith(Net.defaults().withPort(37017)))
.persistentBaseDir(mongod.persistentBaseDir())
.build()
.start(Version.V6_0_1);
I'm using the MongoClient class to connect to the embedded MongoDB server:
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
MongoClient.getDefaultCodecRegistry(),
CodecRegistries.fromCodecs(new AccountCodec())
);
MongoClientOptions options = MongoClientOptions.builder().codecRegistry(codecRegistry)
.writeConcern(WriteConcern.JOURNALED).build();
MongoClient mongoClient = new MongoClient("localhost:37017", options);
Until here, I'm not getting any exceptions.
And here's the code I'm using to insert data:
Document accounts = new Document();
List<Account> accountList = new ArrayList<>();
Account account1 = new Account("[email protected]", "sdfhsidf");
Account account2 = new Account("[email protected]", "ouihod");
accountList.add(account1);
accountList.add(account2);
db.getCollection("accounts").insertOne(accounts.append("account", accountList));
But after insert I get the following exception:
com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): 'cannot use 'j' option when a host does not have journaling enabled' on server localhost:37017. The full response is {"ok": 0.0, "errmsg": "cannot use 'j' option when a host does not have journaling enabled", "code": 2, "codeName": "BadValue"}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:175)
and the data doesn't persist after I shut down my application. Perhaps am I not configuring MongoDB Embedded Server to use the persistence of data? In my filesystem, as shown in the code above I note that the application created the folder and some files inside it.
Can anyone help me to figure out what I'm doing wrong? Thanks in advance for your help.
Upvotes: 1
Views: 644