简体   繁体   English

Java 中的回滚存储过程

[英]Rollback Stored Procedure in Java

I got a requirement where I have to rollback a stored procedure based on a condition.我有一个要求,我必须根据条件回滚存储过程。

First i call the stored procedure and later check a condition and if condition fails, have to rollback.首先我调用存储过程,然后检查条件,如果条件失败,则必须回滚。 Below is the code I have tried.下面是我试过的代码。

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();
    }
}

In the above code, getStudents() method prints the list of name in student table.在上面的代码中, getStudents()方法打印学生表中的姓名列表。 Am running this getStudents() method before rollback and after rollback.在回滚之前和回滚之后运行这个getStudents()方法。 I have set the Savepoint as Savepoint savepoint1 = conn.setSavepoint("ROLLBACK_SP");我已经设置了保存点作为保存点SAVEPOINT1 = conn.setSavepoint(“ROLLBACK_SP”); and am rolling back using this savepoint.并且正在使用此保存点回滚。

But rollback is not happening.但是回滚没有发生。 Am I missing something?我错过了什么吗? please help.请帮忙。

您可以使用 ui-bootstrap 中的模态组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM