javascript
Spring or hibernate saveOrUpdateAll 偶然失效
最近遇到的一個問題
當我們使用hibernatetemplate的saveOrUpdateAll 方法,在數據量達到一定程度時,會必然出現如下的錯誤:
“identifier of an instance altered from X to Y”?
這是很奇怪的,問題【1】該方法實現是這個樣子的:
public void saveOrUpdateAll(final Collection entities) throws DataAccessException { 768 executeWithNativeSession(new HibernateCallback() { 769 public Object doInHibernate(Session session) throws HibernateException { 770 checkWriteOperationAllowed(session); 771 for (Iterator it = entities.iterator(); it.hasNext();) { 772 session.saveOrUpdate(it.next()); 773 } 774 return null; 775 } 776 }); 777 }如果真的是這樣的話,為什么還會出現上述的錯誤?
從上面的方法看就是一個一個的迭代執行,而這樣與自己迭代執行有什么區別? 為什么這個會報錯?
問題【2】經測試,單獨一次提交1000,提交多次 是不會出現該問題得。這是問什么?
性能方面1w條-》152s 確實較慢 后面試一下直接使用jdbc卡看需要多久。
TO BE CONTINUED .....[代碼隨后附上]
https://github.com/gaoxingliang/algintroduction/tree/master/src/hibernate
update:hibernate4 兼容性有點問題,沒有找到updateAll方法,可能正如下面的鏈接中說說的可能已經刪除了。
下面這些鏈接會有幫助:
http://stackoverflow.com/questions/4179166/hibernate-how-to-fix-identifier-of-an-instance-altered-from-x-to-y
http://forum.spring.io/forum/spring-projects/data/56941-problem-with-saveorupdateall-method ?這里說了如何替代該方法,就是在迭代器中單獨調用saveOrUpdate
http://forum.spring.io/forum/spring-projects/data/113874-hibernatetemplate-saveorupdateall-collection-entities-performance 這里描述它的性能,2000/15s 與實際一致。
http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-orm/2.5.6/org/springframework/orm/hibernate3/HibernateTemplate.java#767
PS:
從這里下載打好的spring zip包:http://maven.springframework.org/release/org/springframework/spring/3.2.4.RELEASE/
hibernate3:http://sourceforge.net/projects/hibernate/files/hibernate3/3.5.0-Final/hibernate-distribution-3.5.0-Final-dist.zip/download?use_mirror=nchc
轉載于:https://www.cnblogs.com/scugxl/p/4131791.html
總結
以上是生活随笔為你收集整理的Spring or hibernate saveOrUpdateAll 偶然失效的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 司以类聚,人以群分
- 下一篇: c#中windows验证连接字符串