问题
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