मैं डेटाबेस में एक ही पंक्ति में 2 समवर्ती अद्यतन अनुकरण करने की कोशिश कर, हाइबरनेट का उपयोग कर रहा हूं।हाइबरनेट लॉक प्रतीक्षा टाइमआउट पार हो गया;
संपादित करें: मैंने em1.getTransaction() को स्थानांतरित किया है। Em1.flush() के बाद सही होने के लिए प्रतिबद्ध है; मुझे कोई StaleObjectException नहीं मिल रहा है, दो लेनदेन सफलतापूर्वक किए गए हैं।
Session em1=Manager.sessionFactory.openSession();
Session em2=Manager.sessionFactory.openSession();
em1.getTransaction().begin();
em2.getTransaction().begin();
UserAccount c1 = (UserAccount)em1.get(UserAccount.class, "root");
UserAccount c2 = (UserAccount)em2.get(UserAccount.class, "root");
c1.setBalance(c1.getBalance() -1);
em1.flush();
System.out.println("balance1 is "+c2.getBalance());
c2.setBalance(c2.getBalance() -1);
em2.flush(); // fail
em1.getTransaction().commit();
em2.getTransaction().commit();
System.out.println("balance2 is "+c2.getBalance());
मुझे em2.flush()
पर निम्नलिखित अपवाद मिल रहा है। क्यूं कर?
2009-12-23 21:48:37,648 WARN JDBCExceptionReporter:100 - SQL Error: 1205, SQLState: 41000
2009-12-23 21:48:37,649 ERROR JDBCExceptionReporter:101 - Lock wait timeout exceeded; try restarting transaction
2009-12-23 21:48:37,650 ERROR AbstractFlushingEventListener:324 - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.persister.entity.AbstractEntityPersister.processGeneratedProperties(AbstractEntityPersister.java:3702)
at org.hibernate.persister.entity.AbstractEntityPersister.processUpdateGeneratedProperties(AbstractEntityPersister.java:3691)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:147)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at com.ch.whoisserver.test.StressTest.main(StressTest.java:54)
Caused by: java.sql.BatchUpdateException: Lock wait timeout exceeded; try restarting transaction
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1213)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:912)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 10 more
मैं वास्तव में एक परीक्षण केस लिखने की कोशिश कर रहा हूं यह देखने के लिए कि क्या आशावादी लॉकिंग काम करता है, ऑब्जेक्ट उपयोगकर्ता खाता प्रश्न में उपयोग कर रहा है, कृपया इस प्रश्न को विस्तार से देखें http://stackoverflow.com/questions/1938671/concurrency- इन-हाइबरनेट, इस मामले में दो थ्रेड्स के साथ लेनदेन 2 अंतर्निहित डेटा में बदलाव का पता लगाने के लिए एक staledObjectException प्राप्त करें? – user217631
@ अज्ञात हां, यह होगा। – KLE
मैंने ऊपर अपना उत्तर अपडेट किया है - दो धागे का उपयोग करना ** ** विशेष रूप से ** आशावादी लॉकिंग (अप्रत्याशित होने के कारण) – ChssPly76