hibernate中session的管理方式
package loaderman.c_session; import loaderman.b_second_cache.Dept;
import loaderman.b_second_cache.Employee;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import org.junit.Test; public class App_session { private static SessionFactory sf;
static {
sf = new Configuration()
.configure()
.addClass(Dept.class)
.addClass(Employee.class) // 测试时候使用
.buildSessionFactory();
} @Test
public void testSession() throws Exception {
//openSession: 创建Session, 每次都会创建一个新的session
Session session1 = sf.openSession();
Session session2 = sf.openSession();
System.out.println(session1 == session2);
session1.close();
session2.close(); //getCurrentSession 创建或者获取session
// 线程的方式创建session
// 一定要配置:<property name="hibernate.current_session_context_class">thread</property>
Session session3 = sf.getCurrentSession();// 创建session,绑定到线程
Session session4 = sf.getCurrentSession();// 从当前访问线程获取session
System.out.println(session3 == session4); // 关闭 【以线程方式创建的session,可以不用关闭; 线程结束session自动关闭】
//session3.close();
//session4.close(); 报错,因为同一个session已经关闭了!
}
}
hibernate中session的管理方式的更多相关文章
- Hibernate中Session的get和load
hibernate中Session接口提供的get()和load()方法都是用来获取一个实体对象,在使用方式和查询性能上有一些区别.测试版本:hibernate 4.2.0. get Session接 ...
- hibernate中session的获取使用以及其他注意事项
hibernate中session的获取使用以及其他注意事项 前言:工作时,在同时使用Hibernate的getSession().getHibernateTemplate()获取Session后进行 ...
- hibernate中session
hibernate中的session是一级缓存,可以理解为进程级的缓存.在进程运行期间一直存在. session可以理解为一个可以操作数据库的对象 具体如何操作数据库? session中有方法, 如果 ...
- [转]Hibernate中Session的get和load
hibernate中Session接口提供的get()和load()方法都是用来获取一个实体对象,在使用方式和查询性能上有一些区别.测试版本:hibernate 4.2.0. get Session接 ...
- linux驱动程序之电源管理之新版linux系统设备架构中关于电源管理方式的变更
新版linux系统设备架构中关于电源管理方式的变更 based on linux-2.6.32 一.设备模型各数据结构中电源管理的部分 linux的设备模型通过诸多结构体来联合描述,如struct d ...
- Hibernate中Session与本地线程绑定
------------------siwuxie095 Hibernate 中 Session 与本地线程绑定 1.Session 类似于 JDBC 的连接 Connection 2.Session ...
- Tomcat中session的管理机制
1. 请求过程中的session操作: 简述:在请求过程中首先要解析请求中的sessionId信息,然后将sessionId存储到request的参数列表中.然后再从 request获取s ...
- hibernate下Session的获取方式
Session是应用程序与数据库之间的一个会话,其重要性不言而喻.初学Hibernate,使用SessionFactory,老老实实地打开事务,提交,回滚,关闭session. 1.直接通过Sessi ...
- hibernate中session的线程安全问题
Hibernate的基本特征是完成面向对象的程序设计语言到关系数据库的映射,在Hibernate中使用持久化对象PO(Persistent Object)完成持久化操作,对PO的操作必须在Sessio ...
随机推荐
- 如何将公式插入到word
平台:win10 x64+ office 2010+ Mathpix Snipping Tool +mathtype6.9b 直接安装就行,下载好了以后,要和word连接起来还需要下载一个插件,有 ...
- How to Change the Default Theme Appearance [editing with no theme]
Windows Live Writer has the ability to allow users to see their blog as it'd be posted as they are c ...
- 微信小程序html(wxml)传参
欢迎加入前端交流群交流知识:749539640 习惯了vue.angular用微信小程序有时候真感觉非人类..需要用data-xxx 先说下我们在vue.angular里事件传参 //html < ...
- 不重启linuxVMWare虚拟机添加虚拟光驱、硬盘
方法/步骤1: VMware找到要添加虚拟光驱位置,先右击点击设置,如图所示: 步骤2: 出现此界面之后点击添加: 步骤3: 直接点击下一步,如下图所示: 点击完成,天机硬盘就完毕了. 磁盘添加完成, ...
- LoadRunner(3)
一.性能测试的策略 重要的:基准测试.并发测试.在线综合场景测试 递增测试.极限测试... 1.基准测试:Benchmark Testing 含义:就是单用户测试,单用户.单测试点.执行n次: 作为后 ...
- gdb 使用finish命令(缩写fin)
gdb 使用finish命令(缩写fin) gdb 使用finish命令(缩写fin) gdb 使用finish命令(缩写fin) 跳出跟踪的函数
- No provider available from registry出错
dubbo+zookeeper进行分布式远程调用时No provider available from registry出错 查看dubbo服务:http://192.168.0.100:8080/d ...
- unity vulkan snapdragon profiler
your device does not match the hardware requirements of this application 遇到上面那个warning 跟了下 是vulkan c ...
- java双指针的简单理解
一.什么是双指针 双指针我所理解地是在遍历对象时,不是使用单个指针进行访问,而是使用两个相同方向或者相反方向的指针进行遍历,从而达到相应的目的. 在JAVA中并没有像C/C++指针地概念,所以这里所说 ...
- django nginx uwsgi 502 Gateway
前提:腾讯云服务器有个内网ip和外网ip 首先检查使用的端口是否正常可用 1.检查端口是否开放,在腾讯云控制台安全组查看 2.检查防火墙端口是否开放 systemctl start firewalld ...