java - Difference between SessionFactory.openSession() & SessionFactory.getCurrentTransaction().commit() -
what happen if close session session.getcurrenttransaction().commit()
or close session session.close()
@ end of method?
public static void deleteyear(years year) { session session = sessionfactory.opensession(); session.begintransaction(); session.delete(year); // best way close session session.gettransaction().commit(); //or : session.close(); ??? }
asaik, these 2 entirely different operations.
sessionfactory.opensession()
: opens session
session.begintransaction()
: begins transaction, transaction atomic unit of work should succeed or fail entirely.
session.gettransaction().commit()
: commit transaction, means things happened between begintransaction , call persisted database. if doesn't commit transaction changes happened in transaction lost. commiting transaction deosn't close session
session.close()
: closes session , releases acquired resources.
that means need call both commit()
, session.close()
.
public static void deleteyear(years year) { session session = sessionfactory.opensession(); session.begintransaction(); session.delete(year); session.gettransaction().commit(); session.close(); }
Comments
Post a Comment