【问题标题】:why this code on error still con.commit()为什么这个错误代码仍然是 con.commit()
【发布时间】:2017-11-05 11:15:53
【问题描述】:

我已在数据库中将stdImage 设置为不为空,当我不插入图像时,它会引发错误,但 con.commit() 仍然可以正常工作,并且数据而不是 rollback() 可以在没有图像的情况下保存到数据库请有人解决此代码中的逻辑错误。实际上 rollback() 不起作用。我会很感激的。

public void stdSave(String stdID, String stdName, String fName, String sSec, String contactNo, Date bdate, String address, byte[] image) throws RemoteException, SQLException {

    String stdQuery = "INSERT INTO dbo.StudentTable (stID, stName, stFName, classSection,"
                    + "contact, date, pAddress) VALUES (?, ?, ?, ?, ?, ?, ?)";
            String imgQuery = "INSERT INTO dbo.StdImage (stdID, stdImage) VALUES (?, ?)";
            Savepoint save = null;

            try { 
            try(PreparedStatement stmt = con.prepareStatement(stdQuery); 
                    PreparedStatement stmt1 = con.prepareStatement(imgQuery)) {
                con.setAutoCommit(false);
                save = con.setSavepoint();
                stmt.setString(1, stdID);
                stmt.setString(2, stdName);
                stmt.setString(3, fName);
                stmt.setString(4, sSec);
                stmt.setString(5, contactNo);
                stmt.setDate(6, bdate);
                stmt.setString(7, address);
                stmt.execute();

                stmt1.setString(1, stdID);
                stmt1.setBytes(2, image);
                stmt1.execute();

            }
            }
            catch(Exception e) {
                try {
                    con.rollback(save);
                }
                catch (Exception e1) {
                JOptionPane.showConfirmDialog(null, "Cannot add the student to database " + e);
                }
            }
            con.commit();

        }

【问题讨论】:

    标签: java jdbc


    【解决方案1】:

    您在 try-catch 块之后调用 con.commit();,并且由于您捕获了所有异常,因此您总是调用 commit。

    您可能应该将 commit 移动到 try 块中:

            ...
            con.commit();
        }
        catch(Exception e) {
            try {
                con.rollback(save);
            }
            catch (Exception e1) {
                JOptionPane.showConfirmDialog(null, "Cannot add the student to database " + e);
            }
        }
    

    【讨论】:

    • 我这样做了,但仍然在为空图像引发异常之后,我无法将 stdImage 添加到数据库中,我不明白为什么。你可以看到我已经 setAutoCommit() 为 false 但仍然
    • @user8297824 你调用con.commit() 即使发生异常。你不应该。
    • 我已将 con.commit() 放在 try 块中,但在我尝试使用空图像保存数据然后尝试使用图像保存后它无法正常工作,在表中它没有保存在数据库中第二次尝试的图像
    • @user8297824 您是说要从仅插入 StudentTable 行而没有相应的 StdImage 行的情况中恢复吗?假设 stID 是这些表的主键,您不能在 StudentTable 中插入已经存在的 stID。您必须检查该表中是否已经存在 stID(使用 SELECT 语句),如果没有,则调用 INSERT。
    • 是的,当我在没有 stdImage 的 StudentTable 中插入数据时,这就是我所说的已使用但未将图像添加到 stdImage 表中,因此如何从 stdImage 的此空异常中恢复并将 stdImage 插入到 stdImage 表中
    【解决方案2】:

    永远不要在 catch 块中使用con.commit(),这可能会导致意外提交。将con.commit() 移动到您的尝试块。 接下来,请查看您正在回滚到保存点,但保存点为空。它没有被初始化。 con.rollback(save); 已完成,但这里是 save=null

    要么简单地执行con.rollback(),要么初始化保存点,然后回滚到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      相关资源
      最近更新 更多