What is the difference between HttpSession and Hibernate Session? hibernatesession

Source: Internet
Author: User

What is the difference between HttpSession and Hibernate Session? hibernatesession
 1. javax. servlet. http. HttpSession is an abstract interface.
It is generated: When a J2EE Web program is running, an HttpSession is created for each new visitor, which is the unique expression of the user identity. Note that the container (Tomcat, Resin) is automatically created.
Purpose: store some frequently used information of this user, such as the user name and permissions. For example, in the shopping cart program, store the items bought by the user.
Destroy: within a certain period of time (related to the container), the session is automatically destroyed without any action.
Method:
HttpSession session = request. getSession ();
Common Methods: setAttribute
Session. setAttribute (key, value );
In this way, in another jsp or Servlet, you can use
Session. getAttribute (key); get value
Similar to a Map
2. org. hibernate. Session
It is a handle object for hibernate to operate databases. Its unique similarity with the above Session is that its name is a bit like anything else.
In a general Hibernate program, the Session is manually obtained by the user and closed manually.
In a regular project, obtain the Session at the business layer.
Session session = HibernateSessionFactory. openSession ();
The session is then passed to the dao layer to persist data or perform other operations.
A business logic may call multiple dao methods at a time. For example, bank transfer is a process of first subtraction and then increment. Therefore, two dao methods are called (account a subtraction, B ). Therefore, you can use the same Session generated by the business layer to do this.
A1 and a2 represent account entities.
A1 is a and a2 is B.
A1.setAcount (a1.getAcount ()-1000 );
A2.setAcount (a2.getAcount () + 1000 );
Dao. update (a1, session );
Dao. update (a2, session );
Transaction tx = session. beginTransaction ();
Mit ();
Finally, close the session at the business layer.
Session. close ();
Or call HibernateSessionFactory. closeSession (session );
It is best to add exception capture, and so on, such as exceptions and instant rollback. Ensure that the operation is not unexpected.
Try {
...
Mit ();
} Catch (Exception e ){
Tx. rollback ();
} Finally {
HibernateSessionFactory. closeSession (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.