Hibernate3.3用户手册摘要-1-辅助类,session
1.1.6. 启动和辅助类
是时候来加载和储存一些 Event
对象了,但首先我们得编写一些基础的代码以完成设置。我们必须启动 Hibernate,此过程包括创建一个全局的 SessoinFactory
,并把它储存在应用程序代码容易访问的地方。SessionFactory
可以创建并打开新的 Session
。一个 Session
代表一个单线程的单元操作,org.hibernate.SessionFactory
则是个线程安全的全局对象,只需要被实例化一次。
我们将创建一个 HibernateUtil
辅助类(helper class)来负责启动 Hibernate 和更方便地操作 org.hibernate.SessionFactory
。让我们来看一下它的实现:
package org.hibernate.tutorial.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
把这段代码保存为 src/main/java/org/hibernate/tutorial/util/HibernateUtil.java
。
这个类不但在它的静态初始化过程(仅当加载这个类的时候被 JVM 执行一次)中产生全局的 org.hibernate.SessionFactory
,而且隐藏了它使用了静态 singleton 的事实。它也可能在应用程序服务器中的 JNDI 查找 org.hibernate.SessionFactory
。
如果你在配置文件中给 org.hibernate.SessionFactory
一个名字,在 它创建后,Hibernate 会试着把它绑定到 JNDI。要完全避免这样的代码,你也可以使用 JMX 部署,让具有 JMX 能力的容器来实例化 HibernateService
并把它绑定到 JNDI。这些高级可选项在后面的章节中会讨论到。
再次编译这个应用程序应该不会有问题。最后我们需要配置一个日志(logging)系统 — Hibernate 使用通用日志接口,允许你在 Log4j 和 JDK 1.4 日志之间进行选择。多数开发者更喜欢 Log4j:从 Hibernate 的发布包中(它在 etc/
目录下)拷贝 log4j.properties
到你的 src
目录,与 hibernate.cfg.xml
放在一起。看一下配置示例,如果你希望看到更加详细的输出信息,你可以修改配置。默认情况下,只有 Hibernate 的启动信息才会显示在标准输出上。
示例的基本框架完成了 — 现在我们可以用 Hibernate 来做些真正的工作。
2.5. Contextual sessions sessions的上下文
Most applications using Hibernate need some form of "contextual" session, where a given session is in effect throughout the scope of a given context. However, across applications the definition of what constitutes a context is typically different; different contexts define different scopes to the notion of current. Applications using Hibernate prior to version 3.0 tended to utilize either home-grown
ThreadLocal
-based contextual sessions, helper classes such asHibernateUtil
, or utilized third-party frameworks, such as Spring or Pico, which provided proxy/interception-based contextual sessions.
使用Hibernate的大多数应用程序需要某种形式的“上下文相关的”session,特定的session在整个特定的上下文范围内始终有效。大多数应用在使用Hibernate时,需要一些方式来获取可以贯穿给定作用域环境的"上下文"session。然而,对不同类型的应用程序而言,要为什么是组成这种“上下文”下一个定义通常是困难的;不同的上下文对“当前”这个概念定义了不同的范围。然而对贯穿应用的定义常常不尽相同,不同的上下文的定义导致了对应不同的作用域。3.0版本之前,使用原生自行编写的ThreadLocal的上下文session,或者使用HibernateUtil这样的工具类,或者使用Spring、Pico这样的第三方框架(他们提供了基于代理/拦截的上下文session)
Starting with version 3.0.1, Hibernate added the
SessionFactory.getCurrentSession()
method. Initially, this assumed usage ofJTA
transactions, where theJTA
transaction defined both the scope and context of a current session. Given the maturity of the numerous stand-aloneJTA TransactionManager
implementations, most, if not all, applications should be usingJTA
transaction management, whether or not they are deployed into aJ2EE
container. Based on that, theJTA
-based contextual sessions are all you need to use.
从Hibernate3.01开始,Hibernate新增了SessionFactory.getCurrentSession()
方法。最初它假定被运用到JTA的事务处理(JTA事务中支持了上下文和当前session两种。)它给了许多JTA事务的实现一个成熟的解决方案。无论是否部署于一个J2EE容器,大部分应用应该使用JTA事务管理。所以,基于JTA的上下文session可以满足你的所有需求。Hibernate开发团队坚信,因为有好几个独立的JTA TransactionManager实现了稳定可用,不论是否被部署到一个J2EE容器中,大多数(假若不是所有的)应用程序都应该采用JTA事务管理。基于这一点,采用JTA的上下文相关session可以满足你一切需要。
However, as of version 3.1, the processing behind
SessionFactory.getCurrentSession()
is now pluggable. To that end, a new extension interface,org.hibernate.context.CurrentSessionContext
, and a new configuration parameter,hibernate.current_session_context_class
, have been added to allow pluggability of the scope and context of defining current sessions.
3.1版本中的SessionFactory.getCurrentSession()是由用户选用的。为此,延伸了一个新的接口
org.hibernate.context.CurrentSessionContext。它是一种新的configuration参数。配置
hibernate.current_session_context_class后就可以使得
更好的是,从3.1开始,SessionFactory.getCurrentSession()的后台实现是可拔插的。因此,我们引入了新的扩展接口(org.hibernate.context.CurrentSessionContext)和新的配置参数(hibernate.current_session_context_class),以便对什么是“当前session”的范围和上下文 (scopeand context)的定义进行拔插。
请参阅 org.hibernate.context.CurrentSessionContext
接口的 ,那里有关于它的契约的详细讨论。它定义了单一的方法,currentSession()
,特定的实现用它来负责跟踪当前的上下文相关的会话。Hibernate 内置了此接口的三种实现:
org.hibernate.context.JTASessionContext
:当前会话根据JTA
来跟踪和界定。这和以前的仅支持 JTA 的方法是完全一样的。详情请参阅 Javadoc。org.hibernate.context.ThreadLocalSessionContext
:当前会话通过当前执行的线程来跟踪和界定。详情也请参阅 Javadoc。org.hibernate.context.ManagedSessionContext
:当前会话通过当前执行的线程来跟踪和界定。但是,你需要负责使用这个类的静态方法将Session
实例绑定、或者取消绑定,它并不会打开(open)、flush 或者关闭(close)任何Session
。
The first two implementations provide a "one session - one database transaction" programming model. This is also known and used as session-per-request. The beginning and end of a Hibernate session is defined by the duration of a database transaction. If you use programmatic transaction demarcation in plain JSE without JTA, you are advised to use the Hibernate
Transaction
API to hide the underlying transaction system from your code. If you use JTA, you can utilize the JTA interfaces to demarcate transactions. If you execute in an EJB container that supports CMT, transaction boundaries are defined declaratively and you do not need any transaction or session demarcation operations in your code. Refer to 第 13 章 事务和并发 for more information and code examples.
这两种实现都提供了“每数据库事务对应一个session”的编程模型,也称作每次请求一个session。Hibernate session的起始和终结由数据库事务的生存来控制。假若你采用自行编写代码来管理事务(比如,在纯粹的J2SE,或者JTA/UserTransaction/BMT),建议你使用Hibernate Transaction API来把底层事务实现从你的代码中隐藏掉。
如果你在支持CMT的EJB容器中执行,事务边界是声明式定义的,你不需要在代码中进行任何事务或session管理操作。请参阅第 11 章 事务和并发一节来阅读更多的内容和示例代码。
hibernate.current_session_context_class 配置参数定义了应该采用哪个org.hibernate.context.CurrentSessionContext实现。注意,为了向下兼容,如果未
配置此参数,但是存在org.hibernate.transaction.TransactionManagerLookup的配置,Hibernate会采用org.hibernate.context.JTASessionContext。一般而言,此参数的值指明了要使用的实现类的全名,但那两个内置的实现可以使用简写,即"jta"和"thread"。
Hibernate3.3用户手册摘要-1-辅助类,session的更多相关文章
- Hibernate3回顾-5-简单介绍Hibernate session对数据的增删改查
5. Hibernate对数据的增删改查 5.1Hibernate加载数据 两种:get().load() 一. Session.get(Class arg0, Serializable arg1)方 ...
- Java——Cookie与Session
Cookie通过客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身份. 1.Cookie 1.1概念及使用方法 Cookie实际上是一小段文本信息.客户端请求服务器,如果服务 ...
- NHibernate系列文章十七:NHibernate Session管理(附程序下载)
摘要 NHibernate的Session的管理涉及到NHibernate的两个最重要的对象ISessionFactory和ISession.ISessionFactory的生成非常消耗资源,通常都在 ...
- 跨应用Session共享
摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术.本文将详细讨论session的工作机制并且对在Java ...
- Session简介
摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术.本文将详细讨论session的工作机制并且对在Java ...
- session和cookie详解2
http 之session和cookie http://www.cnblogs.com/ForEverKissing/archive/2008/05/23/1205503.html Session简介 ...
- session深入解读
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:尽管session机制在web应用程序中被採用已经非常长时间了.可是仍然有非常多人不清楚 ...
- 详解session
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp30 一.术语session 在我的经验里,session这个词被滥用的程度 ...
- 再续session和cookie (网络整理)
摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术.本文将详细讨论session的工作机制并且对在Java ...
随机推荐
- highcharts 的使用实例:待写
http://www.hcharts.cn/demo/index.php 方法一:在Axis(包括xAxis和yAxis)有一个属性tickInterval,number类型,表示间隔,也就是间隔多少 ...
- iOS开发主要参考文档(转载)
Objective-C,语言的系统详细资料.这是做iOS开发的前题与基础.https://developer.apple.com/library/ios/#documentation/Cocoa/Co ...
- 本地日志数据实时接入到hadoop集群的数据接入方案
1. 概述 本手册主要介绍了,一个将传统数据接入到Hadoop集群的数据接入方案和实施方法.供数据接入和集群运维人员参考. 1.1. 整体方案 Flume作为日志收集工具,监控一个文件目录或者一个文 ...
- 如何用cufflinks 拼出一个理想的注释文件
后记: cufflinks安装: 下载安装包, 不要下载source code ,直接下载binary. Source code Linux x86_64 binary http://cu ...
- (进阶篇)浅谈COOKIE和SESSION关系和区别
COOKIE介绍 cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PHP,您能够创建并取回 coo ...
- 用 GitHub 来部署静态网页 ꒰・◡・๑꒱
http://segmentfault.com/a/1190000002765287 在尝试过用 GitHub 部署静态 HTML 网页后,觉得其实挺容易的,这里简单说说如何用 GitHub 来完成部 ...
- MySQL Cluster在线备份和恢复
备份方式 一般MySQL数据库都是用mysqldump命令进行数据备份,其生成的文件实际上是创建对象和导入对象的sql语句. 在MySQL Cluster集群上,可以在管理节点上使用start bac ...
- OpenCV: Canny边缘检测算法原理及其VC实现详解(转载)
原文地址:http://blog.csdn.net/likezhaobin/article/details/6892176 原文地址:http://blog.csdn.net/likezhaobin/ ...
- ZOJ 1201 Inversion
原题链接 题目大意:给一个数组{ A1,A2,…,An } ,要求生成另一个数组B1,B2,…,Bn,使得Bi表示的是在数组A中排在i前面大于i的数字的个数.题目的输入是数组A(字母P表示)或者数组B ...
- C++ Primer : 第十二章 : 文本查询程序
C++ Primer书上这个例子讲的很不错,写写帮助自己理解标准库和智能指针. .h 文件内容 #include <fstream> #include <iostream> # ...