我制作了一个使用 SQLite4Java 连接到 SQLite 数据库的 Java 程序。
我从串行端口读取并将值写入数据库。这在开始时效果很好,但现在我的程序已经增长并且我有几个线程。我试图用一个 SQLiteQueue 变量来处理这个问题,该变量执行数据库操作,如下所示:
public void insertTempValue(final SQLiteStatement stmt, final long logTime, final double tempValue)
{
if(checkQueue("insertTempValue(SQLiteStatement, long, double)", "Queue is not running!", false))
{
queue.execute(new SQLiteJob<Object>()
{
protected Object job(SQLiteConnection connection) throws SQLiteException
{
stmt.bind(1, logTime);
stmt.bind(2, tempValue);
stmt.step();
stmt.reset(true);
return null;
}
});
}
} // end insertTempValue(SQLiteStatement, long, double)
但现在我的 SQLite 类无法执行报表报告:
DB[1][U]: disposing [INSERT INTO Temperatures VALUES (?,?)]DB[1][U] from alien thread
SQLiteDB$6@8afbefd: job exception com.almworks.sqlite4java.SQLiteException: [-92] statement is disposed
所以执行不会发生。
我试图找出问题所在,我认为我需要一个 Java 包装器,它可以从其他线程通过的单个线程进行所有数据库操作调用。
这是我的问题,我不知道如何以一种好的方式实现它。如何进行方法调用并确保它始终从同一个线程运行?