弹簧& plsql存储过程-异常处理&交易
我需要从 spring (通过 Hibernate)调用 oracle 存储过程。
我对 PL/SQL 不熟悉,所以我想知道如何继续。
1)我可以使用spring/hibernate(例如注释)来开始/关闭事务吗?我认为这不是问题,并且我不必在存储过程中开始/关闭。
2)在上述过程中,我打开和关闭游标,但如果出现异常,我想关闭它并向Java部分重新抛出异常,所以这就是我所做的:
EXCEPTION
WHEN OTHERS THEN
CLOSE my_cursor;
RAISE e_cannot_do_sth;
我想引发异常,这样我就可以让用户知道出了问题...
最好的方法是什么?
I need to call oracle stored procedure from spring (via Hibernate).
I'm not familiar with PL/SQL so I would like to know how to proceed with that.
1) Can I use spring/hibernate (for instance annotations) to begin/close transaction. I assume that it's not an issue and I don't have to begin/close inside stored procedure.
2) In the mentioned procedure I'm opening and closing the cursor but in case of exception I would like to close it and rethrow an exception to Java part, so here's what I've done:
EXCEPTION
WHEN OTHERS THEN
CLOSE my_cursor;
RAISE e_cannot_do_sth;
I want to raise an exception so I could let user know that something went wrong...
What is the best way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以使用 Spring 来管理事务。我不认为 Hibernate 会给你带来太多优势。
要引发异常,请使用以下命令:
raise_application_error(errno, '您选择的错误消息');
其中
errno
是 -20000 到 -20999 之间的数字。然后在您的 Java 代码中,捕获 org.springframework.jdbc.UncategorizedSQLException,如下所示提取根 SQLExcection:并在 SQLException 上调用
getErrorCode()
以获取自定义错误 PL/SQL 错误号。Yes you can use Spring do manage the transactions. I don't think Hibernate will give you much of an edge here.
To throw an exception, use the following:
raise_application_error(errno, 'Error Message of your choice');
Where
errno
is a number between -20000 and -20999. Then in your Java code, catch the org.springframework.jdbc.UncategorizedSQLException, pull out the root SQLExcection as follows:And call
getErrorCode()
on the SQLException to get your custom error PL/SQL error number.