शायद एक बेवकूफ सवाल है, लेकिन क्या EntityManager.merge() अपवाद फेंकने पर कैच-ब्लॉक में लेनदेन पर रोलबैक करना आवश्यक है? या अपवाद का मतलब यह है कि विलय काम नहीं करता है ताकि अगली बार जब मैं पिछले बदलावों को चलाता हूं तो अपवाद फेंक दिया गया लागू नहीं होता?कैच-ब्लॉक में लेनदेन पर रोलबैक करना आवश्यक है?
उदाहरण:
public void setPerson(Person person) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyLib");
EntityManager em = emf.createEntityManager();
try {
if(!em.getTransaction().isActive()){
em.getTransaction().begin();
}
em.merge(person);
em.getTransaction().commit();
emf.getCache().evict(Person.class); // clear Person cache
} catch (Exception ex){
em.getTransaction().rollback(); // Is this necessary?
} finally {
em.close();
}
}
लेकिन फिर आप कह सकते हैं कि कैच ब्लॉक में रोलबैक होने से कोई चोट नहीं पहुंची है? क्या मैं सही हू? – Rox
@ रोक्स न केवल यह चोट पहुंचाता है, यदि आप कुछ अवांछित प्रभावों से बचना चाहते हैं तो यह आवश्यक हो सकता है। –