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