<property name="current_session_context_class">thread</property> 属性
来源:互联网 发布:分析软件测试的复杂性 编辑:程序博客网 时间:2024/05/29 03:39
转载至:http://www.cnblogs.com/a757956132/p/4309364.html
<property name="current_session_context_class">thread</property>
这个属性的作用:这样配置是本地jdbc事务配置,你通过getCurrentSession创建的session会绑定到当前线程
平时在单独使用hibernate的时候,习惯于配置属性
<property name="current_session_context_class">thread</property>
根据文档,这个是hibernate3.1以后的一个新扩展,目的在于可以让我们在某一个上下文环境(比如说当前线程)中可以通过SessionFactory.getCurrentSession()得到同一个session会话.
后来当我们把spring,hibernate整合的时候,在spring的主配置文件当中,我们也习惯于带入这样的配置
<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
,接下来在spring配置文件中,会使用spring2.x的声明式的方式来配置事务
<tx:advice id="txAdvice" transaction-manager="transactionManager">,<aop:pointcut,<aop:advisor等等配置指定哪些方法上由spring来管理hiberante的事务,这个时候我们试着运行一个类似于这样的方法
public void find() {
Session se = sf.getCurrentSession();
//此处不需要se.beginTransaction(),事务已经交由spring管理
Dept d = (Dept) se.get(Dept.class, new Long(12));
}
会得到一个异常: get is not valid without active transaction.
这个错误一般情况是因为由getCurrentSession得到的session中没有获得的transaction,我们一般要手动的调用se.beginTransaction(),来打开一个活动的事务.但是问题是,我们在spring的配置文件中不是已经通过aop,指定了此处由spring来管理事务吗,怎么还要手动处理事务?
答案:
<prop key="hibernate.current_session_context_class">thread</prop>改为
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>(默认配置)
参考:
1. hibernate文档:
使用 Hibernate 的大多数应用程序需要某种形式的“上下文相关的”会话,特定的会话在整个特定的上下文范围内始终有效。然而,对不同类型的应用程序而言,要为什么是组成这种“上下文”下一个定义通常是困难的;不同的上下文对“当前”这个概念定义了不同的范围
2.在hibernate中,thread,jta,manager的配置其实都是对应了3个hibernate的实现类
org.hibernate.context.ThreadLocalSessionContext
3.在sessionFactory配置文件中
1 0
- <property name="current_session_context_class">thread</property> 属性
- <property name="current_session_context_class">thread</property>
- <property name="hbm2ddl.auto">create</property>
- Struts2 - <s:property name="">
- property(属性)
- @property 属性
- Property 属性
- property属性
- @property属性
- @property属性
- 属性(Property)
- 属性property
- <property name="hibernate.hbm2ddl.auto">update</property>
- <property name="hbm2ddl.auto"></property> 里面值的含义
- spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />
- spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />
- invalid property name "style"?
- current_session_context_class thread
- leetcode-Longest Substring Without Repeating Characters-3
- opencv2.4.8 + win 10 配置教程
- 实现程序合并和分解
- Hadoop 2.0 HA高可用集群安装配置
- rsa-c++
- <property name="current_session_context_class">thread</property> 属性
- Android完全自定义控件并且实现监听事件
- python io模块
- JAVA学习笔记(一)
- Matlab批量预处理图片(1)
- javaee之Spring的练习
- Ubuntu下修改键盘映射
- cout输出流的执行顺序
- Session实现原理