2012-02-22 90 views
0

我有一套系统测试使用Spring的JUnit运行,数据库的配置是这样的:刷新数据库连接

<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 /> 

的测试用例中,我运行一些的设置bash脚本,该脚本从先前完成的备份中在底层PostgreSQL数据库上运行pg_restore。这是因为我需要在每个测试方法之前使数据库的状态保持一致。这种还原是在用@BeforeTransaction进行注解的方法中完成的。

测试类都被注解

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({ "/systemTests-applicationContext.xml", "/applicationContext.xml" }) 
@TransactionConfiguration(defaultRollback = false) 
@Transactional() 

在测试中,当我执行使用当前Hibernate的Session的代码,它不会看到已恢复的表。当我重新启动整个测试然后看到他们,但显然这是我想要的,但它证明了数据库是好的,但是当我做pg_restore时,Spring/Hibernate丢失了。我得到SQLGrammarException表,该表不存在。

我正在寻找一种方法来手动重新连接到数据库。我怎样才能做到这一点?我应该以某种方式在sessionFactory或某些Spring组件上执行此操作吗?

回答

2

好的,我解决了这个问题。看起来Spring和Hibernate都不知道DB中发生的变化,并且如果DBCP池中有任何连接,它们将被重用,然后会发生错误,因为连接下方的模式已更改。所以我想到在重置数据库时测试过程中戳的地方是提供给Hibernate SessionFactory的javax.sql.DataSource对象。 DBCP BasicDataSource不具备此功能,因此我:

  1. 为dataSource bean创建了我自己的包装器。
  2. 标志着bean作为原型的范围,所以我不会有构建它自己,每当我需要一个新的
  3. 添加一个刷新方法,其调用ApplicationContext的包装类(自动装配到包装类)的新的bean实例
  4. 注入包装器bean加入到Hibernate的SessionFactory
  5. 自动装配包装上,以测试类,当我需要在测试中调用刷新方法

代码如下:

@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>