Myeclipse生成的session是否需要手动关闭?

问题描述

看了几篇关于session关闭的文章,还是不能确定,初学hibernate3 不知道session是否需要手动关闭,没有用spring和其它的托管,web应用程序中myeclipse生成的dao中的方法如下: 不知道是否需要在save方法里或查询的方法里加上finally{ HibernateSessionFactory.closeSession();}如果不需要关闭的话,具体原因是什么呢?public void save(Custinfo transientInstance) {log.debug("saving WxGzjg instance");try {Transaction tran = null;tran = (Transaction) getSession().beginTransaction();getSession().save(transientInstance);log.debug("save successful");tran.commit();} catch (RuntimeException re) {log.error("save failed", re);throw re;}}我用myeclipse6.5,hibernate3.0生成的SessionFactory 类如下:package com.soft.hibernate;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.cfg.Configuration;/** * Configures and provides access to Hibernate sessions, tied to the * current thread of execution. Follows the Thread Local Session * pattern, see {@link http://hibernate.org/42.html }. */public class HibernateSessionFactory { /** * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file is * in the default package. Use #setConfigFile() to update * the location of the configuration file for the current session. */ private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION; private static final Log log = LogFactory.getLog(HibernateSessionFactory.class); static { try {configuration.configure(configFile);sessionFactory = configuration.buildSessionFactory();} catch (Exception e) {log.info("Error Creating SessionFactory");System.err.println("Error Creating SessionFactory");e.printStackTrace();} } private HibernateSessionFactory() { }/** * Returns the ThreadLocal Session instance. Lazy initialize * the <code>SessionFactory</code> if needed. * * @return Session * @throws HibernateException */ public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get();if (session == null || !session.isOpen()) {if (sessionFactory == null) {rebuildSessionFactory();}session = (sessionFactory != null) ? sessionFactory.openSession(): null;threadLocal.set(session);} return session; }/** * Rebuild hibernate session factory * */public static void rebuildSessionFactory() {try {configuration.configure(configFile);sessionFactory = configuration.buildSessionFactory();} catch (Exception e) {log.info("Error Creating SessionFactory");System.err.println("%%%% Error Creating SessionFactory %%%%");e.printStackTrace();}}/** * Close the single hibernate session instance. * * @throws HibernateException */ public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } }/** * return session factory * */public static org.hibernate.SessionFactory getSessionFactory() {return sessionFactory;}/** * return session factory * *session factory will be rebuilded in the next call */public static void setConfigFile(String configFile) {HibernateSessionFactory.configFile = configFile;sessionFactory = null;}/** * return hibernate configuration * */public static Configuration getConfiguration() {return configuration;}}

解决方案

当然需要关闭session.不关闭,服务器的资源会被严重浪费.如果没有用到hibernate的lazy,那么就在查询结束的时候就关闭session如果用到了lazy那么就用filter的方式关闭.个人建议用完一个session就关闭一个session.像你这样刚接触hibernate,可能对于lzay还不太明白.你可以先学会用会hibernate,过段时间再去深入学习,用好hibernate,尤其是性能的优化.
解决方案二:
在配置文件中可以调节session的有效时间,超过这个时间就会自动关闭,默认是30分钟。你可以根据你的实际情况来配置自动关闭的时间。
解决方案三:
如果你没用spring的话,可以选择过滤器关闭session,但是这样jsp页面就没办法重用这个session了。你可以写个request监听器,在request销毁的时候关闭Session这样可以实现hibernat的延迟价值。在jsp页面也可以用到session

时间: 2024-09-18 18:41:45

Myeclipse生成的session是否需要手动关闭?的相关文章

IE11无法使用除非每次都手动关闭所有加载项如何解决

  IE11无法使用,除非使用命令并关闭加载项打开("C:Program FilesInternet Exploreriexplore.exe" -extoff),否则输入任何网址都没有反应,打开设置菜单也只有3个项目可选,internet选项都不能选. 已经在IE选项中手动关闭了所有的加载项. 通常这种情况可能是不兼容的加载项导致的,可以重置IE恢复到初始设置处理:关闭所有Internet Explorer窗口.打开"运行",在搜索框内输入inetcpl.cpl,

如何自动或手动关闭未响应程序

本文介绍Windows XP/Vista/Win7中设置关闭未响应程序方法,尽管Windows 7在程序容错性方面做了很大改进,但未响应程序及死机情况还是会经常遇到,这类问题各有原因,系统.软件和硬件都有责任,遇到未响应程序时,耐心先等待一下,实在不行就想办法咔嚓掉,但要注意先备份好数据,因为结束进程还可能导致系统死机.自动重启等现象,尤其是些跟硬件关联的程序. 一.如何自动关闭未响应程序 自动关闭未响应程序是系统自动完成的,由于Vista/Windows 7在程序执行效率的缘故,很多未响应的程

java-通过myeclipse生成的webservice代码,访问超时

问题描述 通过myeclipse生成的webservice代码,访问超时 目前遇到一个问题,通过Myeclipse生成的webservice客户端代码(好像是jax-ws),如果访问的url不存在,就一直卡着,很久才报超时异常,有没有什么办法可以设置超时时间 public ServiceCD() { super( CD_WSDL_LOCATION, new QName( "http://xx.com/basic/atomic/operation.CD", "CD")

adview-Unity 3d Android广告如何实现手动关闭啊?

问题描述 Unity 3d Android广告如何实现手动关闭啊? 大家好,我是做android得,最近Unity那个小组项目做完了,要植入到android平台中,我要在他们得游戏页面加个广告条,我在android中写好得代码,运行得时候,功能都能实现,可以实现浏览广告页面和手动关闭.可是在跟Unity结合得时候,游戏界面可以看到广告条,但是没有手动关闭得叉叉.也不能浏览广告页面?这是为什么呢?而在android跟Unity结合得时候, android得代码我也只是修改了一个地方,就是把andr

CruiseControl 使用Ant启动tomcat后 cc为何不继续执行,手动关闭tomcat窗口,cc继续轮询下一次构建

问题描述 因为项目需要做了一个CI服务器,使用的是CruiseContol+SVN+Ant,在CC的<schedule>中我配置了两个ant的脚本,一个是执行build的脚本,一个是执行release的脚本,build的时间间隔目前设置为3分钟,release是每天往web服务器上将build.xml打出的最新war包部署一次,代码如下:<schedule interval="180"><ant anthome="apache-ant-1.7.0

Eclipse/Myeclipse生成serialVersionUID方法

serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 如果你修改代码重新部署后出现序列化错误,可以考虑给相应的类增加serialVersionUID字段.      一般来说有两种生成方式: 一个是默认的1L,比如:private static final long serialVersionUID = 1L; 一个是根据类名.接口名.成员方法及属性等来生成一个64位的哈希字段,比如:private static final long

windows7中如何自动或手动关闭未响应程序

一.如何自动关闭未响应程序 自动关闭未响应程序是系统自动完成的,由于Vista/Windows 7在程序执行效率的缘故,很多未响应的程序其实并没死,而完全是因为操作系统的调度引起的"暂停"状态,尤其是很多电脑老手,鼠标操作太快,程序运行和切换已经完全超出操作系统的响应速度,那样很容易造成暂时的假死状态. 比如系统繁忙时运行某些程序.光驱由于光盘质量问题的频繁读写等等情况,一般只需要耐心等待一下,程序就会恢复状态,而在恢复之前,系统则是一直报告未响应的程序(因为线程或进程超时).所以还是

connection-java web 项目很多Connection 未手动关闭,怎么办

问题描述 java web 项目很多Connection 未手动关闭,怎么办 如题 有很多地方,一个个找太麻烦,有没有方便快捷的方法,或者好的合理的方式? 给处理掉未关闭的数据库链接,mysql 解决方案 可以利用java的invocationHandler写个代理,通过代理执行操作,最后关闭connection. 解决方案二: 只能用搜索一个一个处理吧,Connection最好是在哪里创建就在那里关闭.如果楼主有好的方法麻烦贴出来哈!之前也想到过这个问题. 解决方案三: 用插件检查代码,最好的

win7如何手动关闭操作系统提升效率

  ghost win7 64位旗舰版下载后,大家在优化的过程中可能还需要手动进行一些操作,才能让系统运营的更为顺畅.今天就向各位介绍一些相关的小知识,让各位能通过手动操作提高自己的系统的效率,让自己能更好的使用Windows7系统. 1.打开控制面板,在里面找到操作中心选项,接着选中里面的更改操作中心设置; 2.将里面勾选的全部给去掉,接着把将客户体验改善计划关闭了; 3.在控制面板里面找到程序和功能选项,然后再找到"打开或关闭windows功能"选项,接着把"远程差分压缩