openSession()和getCurrecntSession()之间的区别
来源:互联网 发布:c定义二维数组下标越界 编辑:程序博客网 时间:2024/05/29 13:18
一、session的相关内容
首先总结一下Session,他是应用程序与数据库之间交互操作的一个单线程对象,他是hibernate运作的核心,它的主要作用是为持久化对象提供一系列的功能,持久化对象只有在session的管理下才能进行持久化操作。【session不是线程安全的】
创建SessionFactory之后就可以通过sessionFactory创建session,创建session有俩种方法。
- sessionFactory.openSession();
- sessionFactory.getCurrentSession();
区别如下:
- .采用openSession()方法,是由sessionFactory直接创建一个session实例,在使用结束之后,需要调用close()方法进行手动关闭。
- getCurrentSession()方法创建的session会绑定到当前线程中,在事务提交或回滚后,会自动关闭。
二、hibernate中的事务管理
在Hibernate中,可以通过代码来操作管理事务:
try{Transaction tx = session.beginTransaction();//开启事务....//执行持久化操作tx.commit();//提交事务}catch(Exception e) {tx.rollback();//事务回滚}
也可以通过配置文件对事务进行配置。配置文件中,可以使用本地事务或全局事务,还可以设置事务的隔离级别。
<!--使用本地事务--><property name="hibernate.current_session_context_class">thread</property><!--使用全局事务--><property name="hibernate.current_session_context_class">jta</property><!--设置事务的隔离级别--><property name="hibernate.connection.isolation">2</property>
事务的隔离级别:
- 1——读操作未提交
- 2——读操作已提交
- 3——可重复读
- 4——可序列化
本地事务和全局事务是hibernate对事务管理的俩种方式,本地事务是针对数据库进行操作,也就是只针对一个事务性资源进行操作。全局事务是由服务器进行管理的事务,他需要使用JTA,并且可以夸数据库。
三、开发中出现过的问题
使用s2sh框架时,采用注解的方式进行事务管理,出现了一个错
•No Session found for current thread
错误原因:没有为当前的线程提供Thread.通过寻找,发现可能是缺少了对事务的配置
于是修改了hibernate.cfg.xml
<session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> // <property name="hibernate.current_session_context_class">thread</property> </session-factory>配置session绑定到某一个运行环境,例如从同一个线程中用getCurrentSession()取得的session是同一个,当前没有session就自动创建
一个。
如果将hibernate的配置放进到applicationContext.xml中,则修改为:
<property name="hibernateProperties"><props> <!-- 指定数据库方言 --><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop><!-- 显示Hibernate持久化操作所生成的SQL --><prop key="hibernate.show_sql">true</prop><!-- 将SQL脚本进行格式化后再输出 --><prop key="hibernate.format_sql">true</prop></props></property>
配置好这个以为没问题了,运行了一下,又出现了一个新的错误:
createQuery is not valid without active transaction //总是说不能在没有活动的事务中执行
没有解决,谁来救我
1 0
- openSession()和getCurrecntSession()之间的区别
- openSession和getCurrentSession之间的区别
- openSession 和getCurrentSession的区别
- openSession 和currentSession的区别
- getCurrentSession()和openSession()的区别
- getCurrentSession 和 openSession的区别
- openSession 和getCurrentSession的区别
- OpenSession和GetCurrentSession的区别
- openSession 和getCurrentSession的区别
- OpenSession()和getCurrentSession的联系和区别
- hibernate 的openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernat中getCurrentSession 和openSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- openSession和getCurrentSession的区别 thred jta
- Hibernate 中 openSession()和getCurrentSession() 的区别
- sessionFactory.getCurrent()和sessionFactory.openSession()的区别
- 并查集(易爆物,LA 3644)
- 设计模式——工厂方法模式
- 抽象类与接口的区别
- jquery ajax实例及乱码问题
- 高性能前端优化的十四条准则
- openSession()和getCurrecntSession()之间的区别
- 操作系统与网络实现 之十九(戊)
- Android储存小型数据到本地
- JVM原理和优化
- char *p = NULL,int *p = NULL的区别
- SSH连接本地虚拟机失败解决方法
- map Javabean 互转
- nodeJS process
- 动态创建二维数组