Java中的回滚存储过程



我有一个要求,必须根据条件回滚存储过程。

首先我调用存储过程,然后检查一个条件,如果条件失败,就必须回滚。下面是我尝试过的代码。

public static void main(String[] args) {
    Student student=new Student();
    student.setName("AAAA");
    student.setAge("20");
    student.setDob("14/08/1988");
    student.setPhone("98841");
    student.setSslc("1111");
    student.setHsc("222");
    student.setCollege("333");
    System.out.println(student);
    try {
        Connection conn=ConnectDB.getConnection();
        conn.setAutoCommit(false);
        CallableStatement callableStatement = null;
        String proc = "{call STUDENT_OP(?,?,?,?,?,?,?,?)}";
        callableStatement = conn.prepareCall(proc);
        Savepoint savepoint1 = conn.setSavepoint("ROLLBACK_SP");
        int age=Integer.parseInt(student.getAge());
        callableStatement.setString(1, student.getName());
        callableStatement.setInt(2, age);
        callableStatement.setString(3, student.getDob());
        callableStatement.setString(4, student.getPhone());
        callableStatement.setString(5, student.getSslc());
        callableStatement.setString(6, student.getHsc());
        callableStatement.setString(7, student.getCollege());
        callableStatement.registerOutParameter(8, java.sql.Types.NUMERIC);
        callableStatement.executeUpdate();
        int returnCode=callableStatement.getInt(8);
        getStudents();
        if(SOME CONDITION){
            conn.rollback(savepoint1);
        }
        getStudents();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

在上面的代码中,getStudents()方法打印student表中的名称列表。在回滚之前和回滚之后运行此getStudents()方法。我已将保存点设置为存储点保存点1=conn.setSavepoint("ROLLBACK_SP")并且正在使用此保存点进行回滚。

但回滚并没有发生。我是不是错过了什么?请帮忙。

您可以使用ui引导程序中的模态组件。

最新更新