我编写了一个 java 应用程序,它偶尔将事件从多个线程记录到 SQLite 数据库。我注意到我可以通过同时产生少量事件相对容易地触发 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 库?