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:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
</session-factory>
</hibernate-configuration>