0

I have the following test class:

import org.junit.Assert;
import java.sql.*;

import org.junit.Test;

import javax.xml.crypto.Data;

public class TestSuite {

    private DatabaseHandler dbh = new DatabaseHandler("testdb");

    @Test
    public void testInsertEntryDuplicateNoChange() {
        Listing listing = new Listing();

        dbh.truncateEntryTable();

        // Create a random entry
        listing.setId("12345");
        listing.setName("John");
        listing.setAge(24);

        dbh.insertEntry(listing);
        Assert.assertEquals(3, dbh.insertEntry(listing));
    }

    @Test
    public void testInsertEntryDuplicatePriceChange() {
        Listing listing = new Listing();
        dbh.truncateEntryTable();

        // Create a random entry
        listing.setId("54321");
        listing.setName("John");
        listing.Age(34);

        dbh.insertEntry(listing);

        listing.setAge(55);

        Assert.assertEquals(1, dbh.insertEntry(listing));
    }
}

With insertEntry() giving the following return integers depending on what is written:

/*
     * Insert a record into the Entry table
     * -------------------------------------------
     * -1   - Error
     * 0    - Successfully inserted a new record
     * 1    - Record found, updated age
     * 2    - Record found, updated something else
     * 3    - Record found, no action taken
     */

And the constructor of my DatabaseHandler object looks as follows:

DatabaseHandler(String dbName) {

    String dbString = "jdbc:sqlite:" + dbName + ".db";

    try {
        connection = DriverManager.getConnection(dbString);
        statement = connection.createStatement();
        statement.setQueryTimeout(30); // set timemout to 30 seconds

        cal = Calendar.getInstance();
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    } catch (SQLException e) {
        System.err.println(e.getMessage());
    }
}

The first test passes, but I keep getting this error when the second test runs:

[SQLITE_BUSY]  The database file is locked (database is locked)

But I don't understand why. I'm only creating one connection to my sqlite database right?

NOTE: I never close the connection in DatabaseHandler

Cœur
  • 37,241
  • 25
  • 195
  • 267
Cornel Verster
  • 1,664
  • 3
  • 27
  • 55
  • Yesterday I closed your other question as a duplicate. If you think your question is significantly different, then edit the original question. It will then go into the reopen review queue for reconsideration. In any case, JUnit creates a new instance of the test class for each test, so **each test** will create a new `DataHandler`, and although you haven't shown the full code of `DataHandler`, this likely means you are leaking connections with each test. So you do have multiple connections, which causes the error. You will need to close the connection after each test, using `@After`. – Mark Rotteveel May 21 '18 at 08:54
  • Hi Mark. Thanks, that's the answer I need. I noticed that you marked the question as duplicate, and investigated the question that you linked me to, but could not find the answer I was looking for there. By the way, the blog post linked to in the answer of that question links to code that has been removed from github, thus I asked the question again and moved away from asking for "best practice" as I felt I could not an answer from the linked question. – Cornel Verster May 21 '18 at 09:05
  • Be aware that asking for "best practices" is likely to get your question voted to close as primarily opinion-based; don't ask for 'best'. In any case, the linked blog is not necessary to get the point that you get the error if you open multiple connections (which is the gist of **all answers**), then it is just a matter of finding out where your code is creating multiple connections (despite your assertion it is only creating one connection). In this case it is the way JUnit works. You really need to learn the JUnit lifecycle to be able to write proper tests! – Mark Rotteveel May 21 '18 at 09:12
  • Thanks Mark. Noted. I think sharpening up on my understanding of the JUnit lifecycle is a very good idea. – Cornel Verster May 21 '18 at 09:37

0 Answers0