Hibernate 在Spring中重新加载数据库连接+;休眠测试用例
我有一套使用Spring的JUnit runner的系统测试,数据库配置如下所示:Hibernate 在Spring中重新加载数据库连接+;休眠测试用例,hibernate,spring,testing,Hibernate,Spring,Testing,我有一套使用Spring的JUnit runner的系统测试,数据库配置如下所示: <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.postgresql.Driver" /> <property name="url" value="${clustercatalog.jdb
<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 />
在测试中,当我执行使用当前hibernate会话的代码时,它看不到已还原的表。当我重新启动整个测试时,is看到了它们,但显然这是我想要的,但它证明了db是好的,但是当我进行pg_恢复时,Spring/Hibernate丢失了。我得到SQLGrammarException的结果是该表不存在
我正在寻找一种方法来手动重新启动与数据库的连接。我怎样才能做到这一点?我应该在sessionFactory或某些Spring组件上执行此操作吗?好的,我解决了这个问题。Spring和Hibernate似乎都不知道数据库中发生的更改,如果DBCP池中有任何连接,它们将被重用,然后发生错误,因为连接下的模式已更改。因此,我认为当我重置DBs时,在测试期间要戳的地方是提供给Hibernate SessionFactory的javax.sql.DataSource对象。DBCP BasicDataSource不具备此功能,因此我:
@Component
public class RefreshableDataSource implements DataSource {
@Autowired
DataSource dataSource;
@Autowired
ApplicationContext applicationContext;
public void refresh() {
dataSource = (DataSource) applicationContext.getBean("dataSource");
}
@Override
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
...other DataSource methods ...
}
applicationContext.xml:
<bean id="dataSource" scope="prototype" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
......
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="refreshableDataSource" />
<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>
......
org.hibernate.dialogue.PostgreSqlDialogue
${jdbc.show.sql}
真的
<bean id="dataSource" scope="prototype" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
......
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="refreshableDataSource" />
<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>