SQLite在多线程的java应用程序中
我写了一个Java应用程序,它从多个线程偶尔地将事件记录到SQLite数据库。我注意到,我可以通过同时产生少量事件相对容易地触发SQLite的“数据库锁定”错误。这使我开始编写一个模拟最坏情况行为的测试程序,而且我在这个用例中表现出来的表现令人惊讶。下面的代码简单地将五个记录添加到数据库中,首先顺序获取“控制”值。然后同时添加相同的五个记录。
import java.sql.*; public class Main { public static void main(String[] args) throws Exception { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db"); Statement stat = conn.createStatement(); stat.executeUpdate("drop table if exists people"); stat.executeUpdate("create table people (name,occupation)"); conn.close(); SqlTask tasks[] = { new SqlTask("Gandhi","politics"),new SqlTask("Turing","computers"),new SqlTask("Picaso","artist"),new SqlTask("shakespeare","writer"),new SqlTask("tesla","inventor"),}; System.out.println("Sequential DB access:"); Thread threads[] = new Thread[tasks.length]; for(int i = 0; i < tasks.length; i++) threads[i] = new Thread(tasks[i]); for(int i = 0; i < tasks.length; i++) { threads[i].start(); threads[i].join(); } System.out.println("Concurrent DB access:"); for(int i = 0; i < tasks.length; i++) threads[i] = new Thread(tasks[i]); for(int i = 0; i < tasks.length; i++) threads[i].start(); for(int i = 0; i < tasks.length; i++) threads[i].join(); } private static class SqlTask implements Runnable { String name,occupation; public SqlTask(String name,String occupation) { this.name = name; this.occupation = occupation; } public void run() { Connection conn = null; PreparedStatement prep = null; long startTime = System.currentTimeMillis(); try { try { conn = DriverManager.getConnection("jdbc:sqlite:test.db"); prep = conn.prepareStatement("insert into people values (?,?)"); prep.setString(1,name); prep.setString(2,occupation); prep.executeUpdate(); long duration = System.currentTimeMillis() - startTime; System.out.println(" SQL Insert completed: " + duration); } finally { if (prep != null) prep.close(); if (conn != null) conn.close(); } } catch(SQLException e) { long duration = System.currentTimeMillis() - startTime; System.out.print(" SQL Insert failed: " + duration); System.out.println(" SQLException: " + e); } } } } 这是输出当我运行这个java代码: [java] Sequential DB access: [java] SQL Insert completed: 132 [java] SQL Insert completed: 133 [java] SQL Insert completed: 151 [java] SQL Insert completed: 134 [java] SQL Insert completed: 125 [java] Concurrent DB access: [java] SQL Insert completed: 116 [java] SQL Insert completed: 1117 [java] SQL Insert completed: 2119 [java] SQL Insert failed: 3001 SQLException: java.sql.SQLException: database locked [java] SQL Insert completed: 3136 顺序插入5条记录大约需要750毫秒,我期望并发插入大概花费相同的时间。但是你可以看到给定3秒的超时时间,甚至没有完成。我也在C中编写了一个类似的测试程序,使用SQLite的本机库调用,并在大部分同时插入的时候完成了同时插入。所以问题是我的java库。 这是运行C版本时的输出: Sequential DB access: SQL Insert completed: 126 milliseconds SQL Insert completed: 126 milliseconds SQL Insert completed: 126 milliseconds SQL Insert completed: 125 milliseconds SQL Insert completed: 126 milliseconds Concurrent DB access: SQL Insert completed: 117 milliseconds SQL Insert completed: 294 milliseconds SQL Insert completed: 461 milliseconds SQL Insert completed: 662 milliseconds SQL Insert completed: 862 milliseconds 我尝试使用两个不同的JDBC驱动程序(http://www.zentus.com/sqlitejdbc和http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC)和sqlite4java包装器。每次结果相似。有没有人知道没有这种行为的java的SQLite库? 这是 core SQLite library的问题,而不是任何Java包装器。 SQLite使用基于文件系统的锁进行进程之间的并发访问同步,因为作为嵌入式数据库,它没有专门的进程(服务器)来调度操作。由于代码中的每个线程都创建了自己与数据库的连接,所以它被视为一个单独的进程,通过基于文件的锁进行同步,这比任何其他同步方法要慢得多。另外,SQLite不支持每行锁定(还有?)。基本上每个操作的整个数据库文件变为locked。如果您幸运,并且文件系统支持字节范围锁定,则多个读卡器可能会同时访问您的数据库,但您不应该假定这种行为。 核心SQLite库by default allows multiple threads to use the same connection concurrently没有问题。我认为任何理智的JDBC包装器也将允许Java程序中的行为,尽管我还没有实际尝试过。 所以你有两个解决方案: >在所有线程之间共享相同的JDBC连接。 您可能想看看this old question of mine – 似乎已经累积了几个提示,以提高SQLite中的更新性能。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |