多个SQLite连接 - Java

时间:2018-05-21 07:42:22

标签: java sqlite jdbc

我有以下测试类:

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));
    }
}

使用insertEntry()根据写入内容给出以下返回整数:

/*
     * 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
     */

我的DatabaseHandler对象的构造函数如下所示:

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());
    }
}

第一次测试通过,但是在第二次测试运行时我不断收到此错误:

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

但我不明白为什么。我只创建了一个与我的sqlite数据库的连接吗?

注意:我从不关闭DatabaseHandler中的连接

0 个答案:

没有答案