调用存储过程时JPA事务出错 [英] Error with JPA transaction when calling a stored procedure

查看:200
本文介绍了调用存储过程时JPA事务出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Oracle DB中有一个存储过程,我希望使用以下JPA代码从我的EJB AS(Websphere)中调用该存储过程. 该过程在其主体中包含一些"COMMIT".关键是,当我删除该程序的COMMIT时,JPA能够正确调用该程序.如果我离开COMMIT,则会收到异常消息,告诉我该调用无法执行.

I have a stored procedure in an Oracle DB, which I wish to call from my EJB AS (websphere) using the following JPA code. The procedure includes some "COMMIT" in its body. The point is that when I remove the COMMIT of the procedure, JPA is able to invoque the procedure correctly. If I leave the COMMIT then I get an exception telling me that the call cannot be executed.

将Java代码放置在会话Bean中,并且默认情况下将交易设置为必需".

The java code is placed in a session bean with the transaction set up as REQUIRED by default.

    public void updateProc() {
       Query query = entityManager.createNativeQuery("call UPDATE_MYPROC()");
       query.executeUpdate();
    }

我认为问题与交易有关,但不确定.有人可以帮忙吗?谢谢!

I think the problem has to do with the transaction, but not sure. Can anybody help? Thanks!

推荐答案

您正在使用容器来管理持久性.通过使用必需的事务,JPA将为每次对数据库的调用启动一个新事务.因此,没有必要在存储过程中启动或提交事务.实际上,这是一个坏主意.

You are using the container to manage persistence. By using transaction REQUIRED, JPA will start a new transaction for each call to the database. Therefore it's not necessary to start or commit a transaction within the stored procedure. In fact, it's a bad idea.

您没有提到是否在存储过程中开始新事务,只是您在那里有提交.如果您不启动(新)事务,那么该提交将引起问题.即使您这样做,也没有必要,这是我们使用JPA开始的那种事情.

You don't mention if you start a new transaction in the stored procedure, just that you have a commit in there. If you don't start a (new) transaction, that commit will cause problems. Even if you do, it's unnecessary, this is the sort of stuff we use JPA for to start with.

这篇关于调用存储过程时JPA事务出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆