我有一个要求,我必须根据条件回滚存储过程。
首先我调用存储过程,然后检查条件,如果条件失败,则必须回滚。下面是我尝试过的代码。
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()方法打印学生表中的姓名列表。在回滚之前和回滚之后运行这个getStudents()方法。我已将 Savepoint 设置为Savepoint savepoint1 = conn.setSavepoint("ROLLBACK_SP"); 并使用此保存点回滚。
但是回滚没有发生。我错过了什么吗?请帮忙。