Spring + multiple H2 instances in memory -
two different h2 instances created in-memory. make sure happens, initialized both instances same shema , different data. when query using dao different set of data picked different datasource. not happening. doing wrong? how name instance of h2 correct?
@bean(name = "ds1") @primary public embeddeddatabase datasource1() { return new embeddeddatabasebuilder(). settype(embeddeddatabasetype.h2). setname("db1"). addscript("schema.sql"). addscript("data-1.sql"). build(); } @bean(name = "ds2") public embeddeddatabase datasource2() { return new embeddeddatabasebuilder(). settype(embeddeddatabasetype.h2). setname("db2"). addscript("schema.sql"). addscript("data-2.sql"). build(); }
you have created 2 datasources , have marked 1 @primary -- 1 used when entitymanagerfactories , repositories autoconfigured. that's why both dao's accessing same database.
in order around this, need declare 2 separate entitymanagerfactories, described in spring boot documentation:
after that, you'll need declare 2 separate repositories , tell each repository of entitymanagerfactory use. this, in @enablejparepositories annotation you'll have specify correct entitymangerfactory. article describes nicely how that:
http://scattercode.co.uk/2013/11/18/spring-data-multiple-databases/
it nice if spring boot supported autoconfiguration 2 datasources, don't think it's going happen soon:
Comments
Post a Comment