I'm planning on using Apache Cayenne in a project, but I struggle with getting it actually set up correctly.
The Cayenne tutorial shows how to set a database location in the CayenneModeller. My problem is that I want to use Apache Derby at a path that is determined dynamically at runtime and I can't find out how to actually make Cayenne use that path.
I tried to set the path like this:
private static boolean setupDataBase() {
String path = Globals.USER_DATA_DIRECTORY + File.separator + "db";
try {
DataSource dataSource = new PoolManager(
"org.apache.derby.jdbc.EmbeddedDriver",
"jdbc:derby:" + path + ";create=true",
1,
1,
null,
null
);
Configuration configuration = Configuration.getSharedConfiguration();
DataDomain domain = configuration.getDomain();
DataNode node = domain.getNode("MaciNode");
node.setDataSource(dataSource);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
But when I later call DataContext.createDataContext()
, the log in the console shows this:
Information: Created connection pool: jdbc:derby:memory:testdb;create=true
That is the in-memory test URL I've set in CayenneModeller, not the one I've set in setupDataBase
(and yes, I did very that that method gets called). Also, the database files are not created (I verified with the debugger that the path is correct).
So, what is the canonical solution to this problem?