1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)
 

Recent Questions

!!! Hibernate Frequently Asked Questions !!!

Q.Difference between getCurrentSession() and openSession() in Hibernate ?

!!!Answer!!!- From Technical Expert

getCurrentSession() :
The "current session" refers to a Hibernate Session bound by Hibernate behind the scenes, to the transaction scope.
A Session is opened when getCurrentSession() is called for the first time and closed when the transaction ends.
It is also flushed automatically before the transaction commits. You can call getCurrentSession() as often and anywhere you want as long as the transaction runs.
To enable this strategy in your Hibernate configuration:

set hibernate.transaction.manager_lookup_class to a lookup strategy for your JEE container
set hibernate.transaction.factory_class to org.hibernate.transaction.JTATransactionFactory

Only the Session that you obtained with sf.getCurrentSession() is flushed and closed automatically.

Example :
try {
UserTransaction tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");

tx.begin();

// Do some work
sf.getCurrentSession().createQuery(...);
sf.getCurrentSession().persist(...);

tx.commit();
}
catch (RuntimeException e) {
tx.rollback();
throw e; // or display error message
}

openSession() :
If you decide to use manage the Session yourself the go for sf.openSession() , you have to flush() and close() it.
It does not flush and close() automatically.
Example :
UserTransaction tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");

Session session = factory.openSession();

try {
tx.begin();

// Do some work
session.createQuery(...);
session.persist(...);

session.flush(); // Extra work you need to do

tx.commit();
}
catch (RuntimeException e) {
tx.rollback();
throw e; // or display error message
}
finally {
session.close(); // Extra work you need to do
}

Answered By : null Replied Date : Feb 3 2014
Answer :


Answered By : null Replied Date : Jan 30 2014
Answer :


Answered By : null Replied Date : Jan 21 2014
Answer :


Answered By : null Replied Date : Jan 6 2014
Answer :


Answered By : null Replied Date : Dec 4 2013
Answer :


Answered By : null Replied Date : Dec 1 2013
Answer :


Answered By : null Replied Date : Nov 3 2013
Answer :


Answered By : null Replied Date : Nov 2 2013
Answer :


Answered By : null Replied Date : Nov 2 2013
Answer :


Answered By : null Replied Date : Oct 22 2013
Answer :


Answered By : null Replied Date : Feb 14 2014
Answer :


Answered By : null Replied Date : Feb 19 2014
Answer :


Answered By : null Replied Date : Mar 24 2014
Answer :


Answered By : null Replied Date : Apr 2 2014
Answer :


Answered By : null Replied Date : May 10 2014
Answer :


Answered By : null Replied Date : Jun 6 2014
Answer :


Answered By : null Replied Date : Jun 26 2014
Answer :


Answered By : null Replied Date : Jun 29 2014
Answer :


Answered By : null Replied Date : Jul 17 2014
Answer :


Answered By : null Replied Date : Nov 4 2014
Answer :


Answered By : null Replied Date : Nov 5 2014
Answer :


Answered By : null Replied Date : Nov 22 2014
Answer :


Answered By : null Replied Date : Nov 25 2014
Answer :


Answered By : null Replied Date : Jan 11 2015
Answer :


Answered By : null Replied Date : May 20 2015
Answer :


Answered By : null Replied Date : Jun 28 2015
Answer :


Answered By : null Replied Date : Sep 24 2015
Answer :


Answered By : null Replied Date : Mar 21 2016
Answer :


Answered By : null Replied Date : Apr 10 2016
Answer :


Answered By : null Replied Date : Aug 2 2017
Answer :


 

You can also contribute to this answer:

Your Name:
Answer:

 
Ask Question and get answer from Expert.
View Answers List from Expert.

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy