一、javax.servlet.http.HttpSession是一个抽象接口
 
它的产生:J2EE的Web程序在运行的时候,会给每一个新的访问者建立一个HttpSession,这个Session是用户身份的唯一表示。注意,是容器(Tomcat,Resin)自动创建的。
 
用途:存放这个用户的一些经常被用到的信息,例如:用户名,权限。例如在购物车程序里,存放用户买的商品。
 
销毁:一定时间(跟容器有关)内,用户无任何动作,session自动销毁。
 
得到的方法:
HttpSession session = request.getSession();
常用方法setAttribute
session.setAttribute(key,value);
这样在另一个jsp或者Servlet里,可以用
session.getAttribute(key);得到value
类似一个Map
 
 
二、org.hibernate.Session
 
它是hibernate操作数据库的一个句柄对象。它跟上面那个Session唯一的相似处就是名字有点像,其他没任何一样的地方。
 
一般的Hibernate程序中,Session由用户手动获取,手动关闭。
正规项目中,在业务层获取Session
Session session = HibernateSessionFactory.openSession();
 
然后把此session传给dao层,将数据持久化或其他的操作。
一次业务逻辑可能调用多个dao方法,例如银行转帐,是一个先减后增的过程,所以会调用2个dao里的方法(甲帐户减,乙帐户增)。因此,可以利用业务层产生的同一个Session来做这件事
 
a1和a2代表帐户实体。
a1是甲的,a2是乙的。
a1.setAcount(a1.getAcount()-1000);
a2.setAcount(a2.getAcount()+1000);
dao.update(a1,session);
dao.update(a2,session);
Transaction tx = session.beginTransaction();
tx.commit();
 
最后在业务层,将session关闭
session.close();
或者调用HibernateSessionFactory.closeSession(session);
 
最好能加上异常捕捉,之类,如产生异常,即时回滚。保证操作不出意外。
 
try{
...
tx.commit();
}catch(Exception e){
tx.rollback();
}finally{
HibernateSessionFactory.closeSession(session);
}
 

HttpSession and Hibernate session的更多相关文章

  1. HttpSession与Hibernate中Session的区别

    一.javax.servlet.http.HttpSession是一个抽象接口 它的产生:J2EE的Web程序在运行的时候,会给每一个新的访问者建立一个HttpSession,这个Session是用户 ...

  2. 关于HttpSession 和 Hibernate框架中 session异同点的简单解析

    快速理解: HttpSession中的session是一个容器用来盛基于会话机制的信息. 比喻:我把钱放进银行的保险柜里. 解析:我的钱就是我的信息,ID等 银行的保险柜就是session容器. Hi ...

  3. Hibernate Session & Transaction详解

    Hibernate Session & Transaction详解 HIbernate中的Session Session是JAVA应用程序和Hibernate进行交互时使用的主要接口,它也是持 ...

  4. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a ...

  5. Use Spring transaction to simplify Hibernate session management

    Spring对Hibernate有很好的支持    DataSource ->SessionFactory-> HibernateTranscationManagerHibernate中通 ...

  6. Could not open Hibernate Session for transaction;

    javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException: Co ...

  7. Hibernate3回顾-5-简单介绍Hibernate session对数据的增删改查

    5. Hibernate对数据的增删改查 5.1Hibernate加载数据 两种:get().load() 一. Session.get(Class arg0, Serializable arg1)方 ...

  8. [转]Hibernate Session各种状态转换方法分析

    摘自http://spiritfrog.iteye.com/blog/221177 我的印象里, Hibernate session中常用的保存操作只有:save, update, saveOrUpd ...

  9. spring事务管理出错。No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy ...

随机推荐

  1. [POJ2778]DNA Sequence(AC自动机 + DP + 矩阵优化)

    传送门 AC自动机加DP就不说了 注意到 m <= 10,所以模式串很少. 而 n 很大就需要 log 的算法,很容易想到矩阵. 但是该怎么构建? 还是矩阵 A(i,j) = ∑A(i,k) * ...

  2. 常州模拟赛d4t2 陶陶摘苹果

    题目描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出 n 个苹果.苹果成熟的时候,陶陶就会 跑去摘苹果. 陶陶的手不能弯 (他仅能把手伸直),当且仅当陶陶达到的高度与苹果的高度相等的时候,陶陶 ...

  3. Spoj-BITDIFF Bit Difference

    Given an integer array of N integers, find the sum of bit differences in all the pairs that can be f ...

  4. Security arrangements for extended USB protocol stack of a USB host system

    Security arrangements for a universal serial bus (USB) protocol stack of a USB host system are provi ...

  5. msp430项目编程54

    msp430综合项目---扩展项目四54 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  6. HDU 5893 List wants to travel(树链剖分+线段树)

    题目链接 HDU5893 $2016$年$ICPC$沈阳网络赛的$B$题.这道题其和 BZOJ2243 基本一样 那道题我也写了题解 点这里 两道题的区别就是$BZOJ$这题是点的权值,这道题是边权. ...

  7. BZOJ——1614: [Usaco2007 Jan]Telephone Lines架设电话线

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1930  Solved: 823[Submit][Status][Discuss] Description ...

  8. B站papi酱、陈一发、李云龙

    李云龙-花田错 https://www.bilibili.com/video/av10842071/?from=timeline&isappinstalled=1 李云龙:你猜旅长怎么说? h ...

  9. Object源码

    1.Object是所有类的父类,默认会继承Object. 2.Object类中常用的方法有:getClass().hashCode().equals().clone().toString().fina ...

  10. Linux配置防火墙添加端口(Ubuntu/Debian无法使用此方法)

    注意:Ubuntu/Debian无法使用此方法 1.打开iptables vi /etc/sysconfig/iptables 2.添加防火墙规则 规则参考:http://www.cnblogs.co ...