[英]JPA executeUpdate always returns 1
我這里有一個問題,即使沒有記錄要更新,executeUpdate命令也總是返回值1。
首先,我檢索了幾條記錄,進行了一些計算,然后更新了某些檢索到的記錄的狀態。
JPA更新代碼:
private int executeUpdateStatusToSuccess(Long id, Query updateQuery) {
updateQuery.setParameter(1, getSysdateFromDB());
updateQuery.setParameter(2, id);
int cnt = updateQuery.executeUpdate();
return cnt; // always return 1
}
更新查詢:
UPDATE PRODUCT_PARAM SET STATUS = 2, DATA_TIMESTAMP=? WHERE ID = ? AND STATUS=-1
請注意, STATUS列實際上從不值<0。我故意在此處添加此條件只是為了表明,即使它不應該更新任何記錄,executeUpdate()仍返回值1。
另外需要注意的是,在數據檢索和更新之間沒有任何更新過程。 這些都是在我的本地環境中完成的。
有什么建議,如果我可能在這里遺漏了什么? 還是如果有一些我需要檢查的配置參數?
編輯:對於JPA,我正在使用EclipseLink。 對於數據庫,我使用帶驅動程序ojdbc5.jar的Oracle 10g。
最后,我必須研究EclipseLink JPA源代碼。 所以系統實際上執行了這一行
return Integer.valueOf(1);
從下面的DatabaseAccessor
類的basicExecuteCall
方法內部的代碼中:
if (isInBatchWritingMode(session)) {
// if there is nothing returned and we are not using optimistic locking then batch
//if it is a StoredProcedure with in/out or out parameters then do not batch
//logic may be weird but we must not batch if we are not using JDBC batchwriting and we have parameters
// we may want to refactor this some day
if (dbCall.isNothingReturned() && (!dbCall.hasOptimisticLock() || getPlatform().canBatchWriteWithOptimisticLocking(dbCall) )
&& (!dbCall.shouldBuildOutputRow()) && (getPlatform().usesJDBCBatchWriting() || (!dbCall.hasParameters())) && (!dbCall.isLOBLocatorNeeded())) {
// this will handle executing batched statements, or switching mechanisms if required
getActiveBatchWritingMechanism().appendCall(session, dbCall);
//bug 4241441: passing 1 back to avoid optimistic lock exceptions since there
// is no way to know if it succeeded on the DB at this point.
return Integer.valueOf(1);
} else {
getActiveBatchWritingMechanism().executeBatchedStatements(session);
}
}
一個簡單的技巧就是不使用批處理。 我嘗試在persistence.xml中將其關閉,並且更新返回的期望值為0。
<property name="eclipselink.jdbc.batch-writing" value="none" />
我期望有更好的解決方案,但是根據我的情況,現在可以使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.