I\'m using AbstractRoutingDatasource to route between databases in runtime. In real condition on the informix database everything works just fine.
For tests I create
After a lot research in the last two days I found a solution to initialize all datasources. I created a method to create and initialize the local H2 databases using the SchemaExport.class of hibernate. Since I only need the local db's for testing, I call the method for each DatabaseEnvirement in a @Before cucumber method.
public void createDatabase(DatabaseEnvironment environment)
throws Exception
{
// load hibernate configuration from hibernate.cfg.xml in classpath
Configuration configuration = new Configuration().configure();
MetadataSources metadata =
new MetadataSources(new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true);
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
for (BeanDefinition def : scanner.findCandidateComponents(MarkEntity.class.getPackage().getName()))
{
metadata.addAnnotatedClass(Class.forName(def.getBeanClassName()));
}
Connection connection = DriverManager.getConnection("jdbc:h2:mem:test-" + environment.name().toLowerCase(), "sa", "");
SchemaExport export = new SchemaExport((MetadataImplementor) metadata.buildMetadata(), connection);
export.create(true, true);
}
@Before
public void beforeTest()
throws Exception
{
// initialise databases
for (DatabaseEnvironment env : DatabaseEnvironment.values())
{
createDatabase(env);
}
}
and this is how my hibernate.cfg.xml looks like:
org.hibernate.dialect.H2Dialect
true
create-drop