HttpSession与Hibernate中Session的区别

HttpSession与Hibernate中Session的区别

一、javax.servlet.http.HttpSession是一个抽象接口
它的产生:J2EE的Web程序在运行的时候,会给每一个新的访问者建立一个HttpSession,这个Session是用户身份的唯一表示。注意,是容器(Tomcat,Resin)自动创建的。
用途:存放这个用户的一些经常被用到的信息,例如:用户名,权限。例如在购物车程序里,存放用户买的商品。
销毁:一定时间(跟容器有关)内,用户无任何动作,session自动销毁。
得到的方法: www.2cto.com
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);
}
默认session的时间为20分钟,如果想在这之前清除的话可以使用Session.Abandorn方法
 
注:在struts中每一个action实际上就是一个servlet,所以在action中获得的session对象其实就是httpsession对象。调用其中的getattribute和setattribute方法是session最大的用处。
在hibernate中的session相当于jdbc中的connection对象,是对数据库操作的句柄。与httpsession一点关系都没有,完全是两个独立的类对象。在使用时没有getattribute,setattribute等相关方法。最大的用处是建立数据库访问的事物操作时获得sesssion对象然后创建查询条件对象获得查询结果list返回到业务层中。
在使用hibernate中 的session获得当前session对象时经常报错:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here对于以上错误解决方法如下
方法一:
  

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactiona

getHibernateTemplate().getSessionFactory().getCurrentSession()的意思是得到当前线程 绑定的session,而当前线程绑定的session是通过当前的事务产生的,如果你没有配置事务的话,当前线程threadlocal中就不存在 session,这样就出现no session错误。

而execute的回调方法,看源码HibernateTemplate中写道
Java code
public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");

Session session = getSession();
boolean existingTransaction = (!isAlwaysUseNewSession() &&
(!isAllowCreate() || SessionFactoryUtils.isSessionTransactional(session, getSessionFactory())));

其中getSession,代码如下
Java code
protected Session getSession() {
if (isAlwaysUseNewSession()) {
return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());
}
else if (isAllowCreate()) {
return SessionFactoryUtils.getSession(
getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
}
else {
try {
return getSessionFactory().getCurrentSession();
}
catch (HibernateException ex) {
throw new DataAccessResourceFailureException("Could not obtain current Hibernate Session", ex);
}
}
}

其中默认private boolean alwaysUseNewSession = false,所以代码会走到else if (isAllowCreate())
注意这里:else if (isAllowCreate()),其中在HibernateTemplate类中默认private boolean allowCreate = true;
意思说如果当前线程中的session不存在的话,是否允许创建,而默认是允许的,通过函数名字就很清楚,接下来是创建当前线程中的session的代码,所以在没有事务的状态下,用execute回调方法,就不会出现上述问题。
 
方法二:
 

No Hibernate Session bound to thread, and configuration does not allow creation

出现了这个问题,原因就是没有给操作数据库的service方法配置事务。
事务传播行为种类解析及配置如下例:

Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播:

表1事务传播行为类型

事务传播行为类型 说明
PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。

当使用PROPAGATION_NESTED时,底层的数据源必须基于JDBC 3.0,并且实现者需要支持保存点事务机制。

<!–Hibernate事务管理器–>
<bean id=”transactionManager”
class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory”>
<ref bean=”sessionFactory” />
</property>
</bean>

<!– 定义事务拦截器bean–>
<bean id=”transactionInterceptor”
class=”org.springframework.transaction.interceptor.TransactionInterceptor”>
<!– 事务拦截器bean需要依赖注入一个事务管理器–>
<property name=”transactionManager” ref=”transactionManager” />
<property name=”transactionAttributes”>
<!– 下面定义事务传播属性–>
<props>
<prop key=”save*”>PROPAGATION_REQUIRED</prop>
<prop key=”find*”>PROPAGATION_REQUIRED,readOnly</prop>
<prop key=”delete*”>PROPAGATION_REQUIRED</prop>
<prop key=”update*”>PROPAGATION_REQUIRED</prop>
<prop key=”*”>PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean id=”managerTemplate” abstract=”true” lazy-init=”true”>
<property name=”teamDao”>
<ref bean=”teamDao” />
</property>
<property name=”studentDao”>
<ref bean=”studentDao” />
</property>
</bean>

<bean id =”manager” class=”com.zd.service.impl.Manager” parent=”managerTemplate” />

<!– 定义BeanNameAutoProxyCreator–>
<bean class=”org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator”>
<!– 指定对满足哪些bean name的bean自动生成业务代理 –>
<property name=”beanNames”>
<!– 下面是所有需要自动创建事务代理的bean–>
<list>
<value>manager</value>
</list>
<!– 此处可增加其他需要自动创建事务代理的bean–>
</property>
<!– 下面定义BeanNameAutoProxyCreator所需的事务拦截器–>
<property name=”interceptorNames”>
<list>
<!– 此处可增加其他新的Interceptor –>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<!– 基本数据库操作 –>
<bean id=”baseDao” class=”com.zd.service.impl.BaseDao”>
<property name=”hibernateTemplate”>
<ref bean=”hibernateTemplate”/>
</property>
</bean>

<!– 班级 –>
<bean id=”teamDao” class=”com.zd.service.impl.TeamDao”>
<property name=”baseDao”>
<ref bean=”baseDao” />
</property>
</bean>

<!– 学生 –>
<bean id=”studentDao” class=”com.zd.service.impl.StudentDao”>
<property name=”baseDao”>
<ref bean=”baseDao” />
</property>
</bean>

public void testSaveTeam() {
Team team = new Team();
team.setTeamId(DBKeyCreator.getRandomKey(12));
team.setTeamName(“Class CCC”);
IManager manager = (IManager) SpringContextUtil.getContext().getBean(“manager”);

Student student = new Student();
student.setStudentId(DBKeyCreator.getRandomKey(13));
student.setSex(Student.SEX_FEMALE);
student.setStudentName(“Tom”);
student.setTeamId(“60FHDXDIG5JQ”);
manager.saveTeamAndStu(team, student);
System.out.println(“Save Team and Student Success”);

 
原文地址:https://www.cnblogs.com/moonfans/p/3362686.html