【问题标题】:sqlite not updating tables / database busy [locked] using javasqlite没有更新表/数据库繁忙[锁定]使用java
【发布时间】:2021-03-15 15:39:46
【问题描述】:

我在应用程序中使用 java 和 sqlite。当我尝试更新表时工作正常,但是当我尝试在第一个表之后直接更新第二个表时,sqlite 告诉我忙[锁定]。我搜索解决方案但找不到。我在开始第二个更新之前关闭第一个更新的所有 Resultsets 、 PreparedStatements 和 Connection 但它不起作用。任何实际的解决方案或导致此错误的任何原因?

    String sql2 = "UPDATE wages SET EPIDOMAADEIAS = ?,FMYEPIDOMATOSADEIAS = ?,ERGODOTHSEPIDOMAADEIAS = ?,ERGAZOMENOSEPIDOMAADEIAS = ? WHERE SURNAME = ?";


                if(kathgoria.equals("ΥΠΑΛΛΗΛΟΣ")){
                    model.addRow(new String[]{surname,etairia,kathgoria,ep,erg,ergod,fmyS,syn});
                     ps = connection.prepareStatement(sql2);
                     try {
                        ps.setString(1,epS);
                        ps.setString(2, fmySS);
                        ps.setString(3, ergodS);
                        ps.setString(4, ergS);
                        ps.setString(5, surname);
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    int reS1 = 0;
                    try {
                        reS1 = ps.executeUpdate();
                        ps.close();
                        connection.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }   
                }

【问题讨论】:

  • 你试过检查自动提交吗?
  • 是的..它向我展示了它的真实性..
  • 要得到答案,请出示代码。
  • 我展示了。有什么建议吗?
  • 关闭语句通常应该放在 finally 块中。因为如果您的更新引发异常,连接(也包括语句和结果集)将不会关闭!

标签: java sqlite


【解决方案1】:

我遇到了这个问题,并且阅读的任何解决方案(关闭连接、关闭结果集等)都不适合我。对我真正有帮助的唯一字体是遵循如下事务模式:

  1. 创建连接

    private Connection connect() {
        // SQLite connection string
        String url = "jdbc:sqlite:C://sqlite/db/test.db";
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return conn;
    }

  1. 声明:n 个您需要的字符串句子、1 个结果集、1 个连接和 n 个您希望在交易中执行的准备好的语句。示例:
    // SQL for creating a new material
    String sqlMaterial = "INSERT INTO materials(description) VALUES(?)";
            
    // SQL for posting inventory
    String sqlInventory = "INSERT INTO inventory(warehouse_id,material_id,qty)"
                    + "VALUES(?,?,?)";

    ResultSet rs = null;
    Connection conn = null;
    PreparedStatement pstmt1 = null, pstmt2 = null;

(*) 如果您的交易有多个句子,您也可以使用列表 (pstmt[])。

  1. 打开一个 try/catch/finally 块并执行您的事务,如下所示:
        try {
            // connect to the database
            conn = this.connect();
            if(conn == null)
                return;
            
            // set auto-commit mode to false
            conn.setAutoCommit(false);
            
            // 1. insert a new material
            pstmt1 = conn.prepareStatement(sqlMaterial,
                    Statement.RETURN_GENERATED_KEYS);

            pstmt1.setString(1, material);
            int rowAffected = pstmt1.executeUpdate();

            // get the material id
            rs = pstmt1.getGeneratedKeys();
            int materialId = 0;
            if (rs.next()) {
                materialId = rs.getInt(1);
            }

            if (rowAffected != 1) {
                conn.rollback();
            }
            // 2. insert the inventory
            pstmt2 = conn.prepareStatement(sqlInventory);
            pstmt2.setInt(1, warehouseId);
            pstmt2.setInt(2, materialId);
            pstmt2.setDouble(3, qty);
            // 
            pstmt2.executeUpdate();
            // commit work
            conn.commit();

        } catch (SQLException e1) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException e2) {
                System.out.println(e2.getMessage());
            }
            System.out.println(e1.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt1 != null) {
                    pstmt1.close();
                }
                if (pstmt2 != null) {
                    pstmt2.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e3) {
                System.out.println(e3.getMessage());
            }
        }

示例摘自 - https://www.sqlitetutorial.net/sqlite-java/transaction/

请注意,此示例还有助于了解如果在数据库中没有产生不一致的情况下任何事情都不起作用,您如何能够回滚事务。

【讨论】:

  • 嗨 :),您的回答只是一个链接?请注意,发布的链接将来可能无法使用,并且在这件事上非常不可靠。仅发布链接以支持您的回答。
猜你喜欢
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多