Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:34:18+00:00 2026-05-30T10:34:18+00:00

I have a suite of system tests that uses Spring’s JUnit runner, the database

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:34:19+00:00Added an answer on May 30, 2026 at 10:34 am

    OK, I resolved the issue. It appears that both Spring and Hibernate are unaware of the changes happening down in the DB and if there are any connections in the DBCP pool they are reused and then the error occurs, because the schema has changed underneath the connection. So I figured that the place to poke during the test when I reset the DBs is the javax.sql.DataSource object provided to the Hibernate SessionFactory. The DBCP BasicDataSource does not have this capability, thus I:

    1. created my own wrapper for the dataSource bean.
    2. marked the bean as prototype scope so I will not have to construct it myself, every time I need a new one
    3. added a refresh method to the wrapping class which calls the applicationContext (autowired to the wrapper class) for the new bean instance
    4. injected the wrapper bean to Hibernate’s sessionFactory
    5. autowire the wrapper to the test class and when I need in the test call the refresh method

    code follows:

    @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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a test suite DLL written in C# that uses Selenium.This is then
We currently have a suite of integration tests that run via MbUnit test suites.
I have a view that uses a strongly typed ViewData similar to this: namespace
I have a simple WCF service setup that uses JSON. In this service I
My site is going to have a credit system that basically works a lot
We have a suite of programs that check for new versions at startup, and
I have a test suite to perform smoke tests. I have all my script
I have a test suite to perform smoke tests. I have all my script
I would like to make integration tests and system tests for my applications but
I have a large acceptance test suite that runs the source application many thousands

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.