I have a suite of system tests that uses Spring's JUnit runner, the database config looks like this:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${clustercatalog.jdbc.url}" />
<property name="username" value="${clustercatalog.jdbc.username}" />
<property name="password" value="${clustercatalog.jdbc.password}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="50" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="100" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.xxx" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">${jdbc.show.sql}</prop>
<prop key="hibernate.id.new_generator_mappings">true</prop>
</props>
</property>
<property name="namingStrategy" ref="namingStrategy" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
The test case has a setup in which I run some bash scripts that run pg_restore on the underlying PostgreSQL database from a previously done backup. This is because I need the same state of the database to be the same before every test method. This restore is done in a method annontated with @BeforeTransaction.
The test class is annotated with
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/systemTests-applicationContext.xml", "/applicationContext.xml" })
@TransactionConfiguration(defaultRollback = false)
@Transactional()
In the test when I execute code that uses the current hibernate session, it doesn't see the tables that were restored. When I restart the whole test then is sees them, but obviously this is what I want but it proves that the db is fine but Spring/Hibernate got lost when I did pg_restore. I get SQLGrammarException's that the table does not exist.
I'm looking for a way to manually restart the connection to the DB. How can I achieve that? Should I do it somehow on the sessionFactory or some Spring components?