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 6227335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:11:44+00:00 2026-05-24T09:11:44+00:00

I am using hsqldb for testing some of the data access layer in Java.

  • 0

I am using hsqldb for testing some of the data access layer in Java. I have certain test cases like 100 around. I create a in memory database and then insert some values in the table so that with my test case i can load it, but the problem is for every test case i need to clear the in memory database, only the values not the tables.

Is it possible, one thing is i need to manually delete the rows from the table and is there some thing else I can use.

Thanks

  • 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-24T09:11:45+00:00Added an answer on May 24, 2026 at 9:11 am

    If you use DbUnit in unit-tests, you can specify that DbUnit should perform a clean-and-insert operation before every test to ensure that the contents of the database are in a valid state before every test. This can be done in a manner similar to the one below:

    @Before
    public void setUp() throws Exception
    {
        logger.info("Performing the setup of test {}", testName.getMethodName());
        IDatabaseConnection connection = null;
        try
        {
            connection = getConnection();
            IDataSet dataSet = getDataSet();
            //The following line cleans up all DbUnit recognized tables and inserts and test data before every test.
            DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
        }
        finally
        {
            // Closes the connection as the persistence layer gets it's connection from elsewhere
            connection.close();
        }
    }
    

    Note that it is always recommended to perform any setup activities in a @Before setup method, rather than in a @After teardown method. The latter indicates that you are creating new database objects in a method being tested, which IMHO does not exactly lend easily to testable behavior. Besides, if you are cleaning up after a test, to ensure that a second test runs correctly, then any such cleanup is actually a part of the setup of the second test, and not a teardown of the first.

    The alternative to using DbUnit is to start a new transaction in your @Before setup method, and to roll it back in the @After teardown method. This would depend on how your data access layer is written.

    If your data access layer accepts Connection objects, then your setup routine should create them, and turn off auto-commit. Also, there is an assumption that your data access layer will not invoke Connection.commit. Assuming the previous, you can rollback the transaction using Connection.rollback() in your teardown method.

    With respect to transaction control, the below snippet demonstrates how one would do it using JPA for instance:

    @Before
    public void setUp() throws Exception
    {
        logger.info("Performing the setup of test {}", testName.getMethodName());
        em = emf.createEntityManager();
        // Starts the transaction before every test
        em.getTransaction.begin();
    }
    
    @After
    public void tearDown() throws Exception
    {
        logger.info("Performing the teardown of test {}", testName.getMethodName());
        if (em != null)
        {
            // Rolls back the transaction after every test
            em.getTransaction().rollback();
            em.close();
        }
    }
    

    Similar approaches would have to be undertaken for other ORM frameworks or even your custom persistence layer, if you have written one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using HSQLDB for data layer integration testing, which is great. However, I'm finding
I have a simple Java web application using HSQLDB embedded database. The application is
I have a problem using Hibernate and PostgreSQL for production and HSQLDB for testing.
We have a setup where we are using an embedded HSQLDB for backing Hibernate/JPA
I have build my data model using JPA and am using Hibernate's EntityManager 3
some methods in our model pojos have been annotated like this: @Column(name=cli_clipping_id, updatable=false, columnDefinition
I am using Datanucleus JDO on top of HSqlDb. I would like to execute
In my project I can successfully test database code. I'm using Spring, Hibernate, HSQLDB,
I wish to store UUIDs created using java.util.UUID in a HSQLDB database. The obvious
I'm considering using HSQLDB version 1.8.x in a desktop app for storing local data.

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.