Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 在Spring中重新加载数据库连接+;休眠测试用例_Hibernate_Spring_Testing - Fatal编程技术网

Hibernate 在Spring中重新加载数据库连接+;休眠测试用例

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

我有一套使用Spring的JUnit runner的系统测试,数据库配置如下所示:

<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不具备此功能,因此我:

  • 为数据源bean创建了自己的包装器
  • 将bean标记为原型范围,这样每当我需要一个新的bean时,我就不必自己构建它
  • 向包装类添加了一个刷新方法,该方法调用新bean实例的applicationContext(自动连接到包装类)
  • 将包装器bean注入Hibernate的sessionFactory
  • 自动将包装器连接到测试类,并在测试中需要时调用refresh方法
  • 代码如下:

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