openSession :- When you call SessionFactory.openSession, it always create new Session object afresh and give it to you.
आप और स्पष्ट रूप से फ्लश करने की जरूरत है इन सत्र वस्तुओं को बंद करें। सत्र ऑब्जेक्ट्स थ्रेड सुरक्षित नहीं हैं, इसलिए आपको मल्टीथ्रेडेड वातावरण में प्रति अनुरोध एक सत्र ऑब्जेक्ट और वेब अनुप्रयोगों में प्रति सत्र एक सत्र भी बनाना होगा।
getCurrentSession :- When you call SessionFactory. getCurrentSession, it will provide you session object which is in hibernate context and managed by hibernate internally. It is bound to transaction scope.
When you call SessionFactory. getCurrentSession , it creates a new Session if not exists , else use same session which is in current hibernate context. It automatically flush and close session when transaction ends, so you do not need to do externally.
If you are using hibernate in single threaded environment , you can use getCurrentSession, as it is faster in performance as compare to creating new session each time.
You need to add following property to hibernate.cfg.xml to use getCurrentSession method.
<session-factory>
<!-- Put other elements here -->
<property name="hibernate.current_session_context_class">
thread
</property>
</session-factory>
स्रोत
2017-08-17 11:22:30
आपको बहुत धन्यवाद, @gkamal। मैं [ओपन सत्र में दृश्य] में कोड देखता हूं (http://community.jboss.org/wiki/OpenSessionInView) दस्तावेज़। (आपका लिंक उस दस्तावेज़ को इंगित करता है।) लेखक फ़िल्टर के उपयोग का सुझाव देता है। अपने फ़िल्टर कोड में, वह 'ओपन सत्र() 'या' क्लोज़()' नहीं कहता है। वह केवल 'getCurrentSession() 'कहता है। मुझे लगता है कि वह 'current_session_context' को' थ्रेड 'पर सेट करता है। अब मुझे लगता है कि मैं समझता हूं 'getCurrentSession() '। हालांकि, मुझे नहीं पता कि मुझे 'openSession() 'का उपयोग कब करना चाहिए। – wannik
यदि आप सत्र को किसी भी संदर्भ से बाध्य नहीं करना चाहते हैं तो आप OpenSession का उपयोग करेंगे। ऐसी कुछ स्थितियां हैं जब आपको एक अलग सत्र की आवश्यकता होगी - संदर्भ के लिए एक से अधिक बाध्य (हाइबरनेट इंटरसेप्टरों की एक सीमा है कि आप मूल सत्र का उपयोग नहीं कर सकते हैं) - उन मामलों में आप वर्तमान सत्र के बजाय ओपनसेशन का उपयोग करेंगे। ओपनसेशन एक नया सत्र बनाता है जिसे आपको स्पष्ट रूप से बंद करना होगा। उदाहरण के लिए, एक डीएओ विधि में आप ओपनसेशन को कॉल करेंगे - सत्र का उपयोग करें और इसे बंद करें। GetCurrentSession() का उपयोग कर – gkamal
बजे; क्योंकि मैंने इसे श्रोता में शुरू किया है फ़िल्टर नहीं है यह आपके विचार से ठीक है; मैं mvc2 jsp servlet – shareef