Error with JPA transaction when calling a stored procedure

China☆狼群 提交于 2019-12-25 07:57:54

问题


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.

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!


回答1:


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.

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.



来源:https://stackoverflow.com/questions/29117201/error-with-jpa-transaction-when-calling-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!