Hibernate--coreapi--configuration sessionfactory--getcurrentsession--opensession

Source: Internet
Author: User

Purpose of Sessionfactory: Create session, maintain database connection pool

The sessionfactory in the test file creates a database connection, so sessionfactory generates a database connection pool from the configuration information in the configuration file, from which a database connection is taken out.

Sessionfactory = new Annotationconfiguration (). Configure (). Buildsessionfactory ();

Configure is used to invoke database information, configure () can specify Hibernate.cfg.xml name

Create a session with Getcurrentsession:

@Testpublic void Testteachersave () {Teacher t = new Teacher (); T.setname ("T1"); T.settitle ("Middle"); T.setbirthdate (new Date ());//session Session = Sessionfactory.opensession (); Session session = Sessionfactory.getcurrentsession (); Session.begintransaction (); Session.save (t); Session Session2 = Sessionfactory.getcurrentsession (); SYSTEM.OUT.PRINTLN (Session = = Session2); Session.gettransaction (). commit (); Session Session3 = Sessionfactory.getcurrentsession (); SYSTEM.OUT.PRINTLN (Session = = Session3);}
Opensession () is always creating a new session, this session needs close, and getcurrentsession () if the environment has a session to take the environment, do not need close.

  

  

Hibernate--coreapi--configuration sessionfactory--getcurrentsession--opensession

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.