HttpSession and Hibernate session

Source: Internet
Author: User

One, Javax.servlet.http.HttpSession is an abstract interface   Its production: the Java EE Web program runs, will give each new visitor to create a httpsession, This session is the only representation of the user's identity. Note that the container (Tomcat,resin) is created automatically.   Use: To store the user's frequently used information, such as: User name, permissions. For example, in the shopping cart program, store the goods that the user buys.   Destruction: A certain time (related to the container), the user does not have any action, the session automatically destroyed.   Method: HttpSession session = Request.getsession (); Common method Setattributesession.setattribute (key,value); So in another JSP or servlet, you can use Session.getattribute (key), get value like a map   two, org.hibernate.session  It is a handle object of the Hibernate operations database. The only resemblance to the previous session is that the name is a bit like the rest of the place.   In general Hibernate programs, the session is manually acquired by the user and closed manually. In a regular project, the business layer gets sessionsession session = hibernatesessionfactory.opensession ();  then passes this session to the DAO layer, persisting the data or other operations. A single business logic may invoke multiple DAO methods, such as bank transfers, which is a first-in-a-minus-increment process, so calls to 2 DAO methods (account minus, B account increase) are called. Therefore, you can use the same session generated by the business layer to do this  a1 and A2 represent account entities. A1 is a, A2 is B. A1.setacount (A1.getacount () -1000), A2.setacount (A2.getacount () +1000);d ao.update (a1,session);d ao.update (A2, session); Transaction tx = Session.begintransaction (), Tx.commit ();  finally in the business layer, closes the session session.close (), or calls HibernaTesessionfactory.closesession (session);  better be able to add abnormal catch, such as the abnormal, instant rollback. Ensure the operation is not unexpected.  try{...tx.commit ();} catch (Exception e) {tx.rollback ();} Finally{hibernatesessionfactory.closesession (session);}  

HttpSession and Hibernate session

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.