public String save() throws Live365Exception {
if (client.getId() == null) {
client.setCreator(getSessionUser().getUser());
client.setCreatedDate(new Date());
clientService.create(client);
} else {
Client updateClient = null;
updateClient = clientService.findById(client.getId());
updateClient.copyFrom(client);
updateClient.setUpdatedDate(new Date());
clientService.update(updateClient);
clientService.evictObj(updateClient);
}
return list();
}
save()和list()共处一个session,如果在持久化后不清缓存则返回列表页时还是显示未更新前的数据
用了OpenSessionInViewFilter之后,程序开发确实少了很多问题。一个直接的优点就是,不用再去关心lazy-loading的问题,因为在一个Request的处理周期内,Session都是Open的。 但是,"Great Charge means great responsibility",更大的方便也就意味着更大的责任。此Filter运用时也要非常小心。 从OpenSessionInViewFilter的文档中,我们可以看到下面一段话:
/*Quote */
WARNING: Applying this filter to existing logic can cause issues that have not appeared before, through the use of a single Hibernate Session for the processing of an entire request. In particular, the reassociation of persistent objects with a Hibernate Session has to occur at the very beginning of request processing, to avoid clashes with already loaded instances of the same objects.
Alternatively, turn this filter into deferred close mode, by specifying "singleSession"="false": It will not use a single session per request then, but rather let each data access operation or transaction use its own session (like without Open Session in View). Each of those sessions will be registered for deferred close, though, actually processed at request completion.
A single session per request allows for most efficient first-level caching, but can cause side effects, for example on saveOrUpdate or if continuing after a rolled-back transaction. The deferred close strategy is as safe as no Open Session in View in that respect, while still allowing for lazy loading in views (but not providing a first-level cache for the entire request).
Looks up the SessionFactory in Spring's root web application context. Supports a "sessionFactoryBeanName" filter init-param in web.xml; the default bean name is "sessionFactory". Looks up the SessionFactory on each request, to avoid initialization order issues (when using ContextLoaderServlet, the root application context will get initialized after this filter).
NOTE: This filter will by default not flush the Hibernate Session, as it assumes to be used in combination with service layer transactions that care for the flushing, or HibernateAccessors with flushMode FLUSH_EAGER. If you want this filter to flush after completed request processing, override closeSession and invoke flush on the Session before closing it. Additionally, you will also need to override getSession() to return a Session in a flush mode other than the default FlushMode.NEVER. Note that getSession and closeSession will just be invoked in single session mode!
在Hibernate的论坛里面,我也看到了类似的话,
// Quote An alternative solution that you don't need to implement yourself is to use the Spring Franmework's OpenSessionInViewFilter class. You simply define a servlet filter mapping for the filter and it takes care of opening a Session when a request comes in and closing it when the response is generated. In fact, it will even bind the Session to the current thread and Spring provides a static SessionFactoryUtils.getSession() method that intelligently returns the bound Session or a new one (optionally). We use this pattern in our Struts application and it works like a charm. You can reliably get the one Session even if you use jsp:include and you can pass around your Hibernate POJOs without worrying about lazy initialization exceptions anywhere. However, be aware that having a single Session open for an entire request/response cycle can result in some unexpected behavior that did not happen before. We encountered this ourselves, but dealt with it by using the JUnit setUp() and tearDown() methods to mimic the OpenSessionInViewFilter behavior in our unit tests and solving the quirks from there. Single Session虽然可以最大限度的使用Session中的一级缓存,然而,却可能带来很多的副作用。我在项目中也碰到了这样的问题(saveOrUpdate之后的查询仍旧返回保存前的值)
/**Quote From Pro Hibernate 3*/
■Caution The analogy between a Hibernate Session and a JDBC connection only goes so far. One important difference is that if a Hibernate Session throws an exception of any sort, you should discard it and obtain a new one. This prevents data in the session’s cache from becoming inconsistent with the database.
分享到:
相关推荐
"Greatis Ultimate Pack Full Source" 是一个集合了Greatis公司一系列完整源代码的资源包,主要面向开发者和软件工程师。这个包可能包含了Greatis公司的各种软件开发工具、库、框架或者示例代码,旨在帮助开发者理解...
The Great CEO Within The Great CEO Within The Great CEO Within
There are so many parallels between building a great sports team and building a corporate one, not the least of which that great culture makes amazing things possible. Great Teams by Don Yaeger ...
《Great Photo:打造卓越的摄影后期处理体验》 在摄影艺术的世界里,后期处理与拍摄同样重要,甚至有时更能决定一张照片的最终效果。而提到专业级别的后期处理工具,我们不能忽视德国的“Great Photo”软件。这款...
资源分类:Python库 所属语言:Python 资源全名:great_expectations-0.13.31.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
《Python库Airflow-Provider-Great-Expectations详解》 在Python的世界里,库是开发者的重要工具,它们提供了丰富的功能,让开发工作更加高效。本文将深入探讨名为`airflow-provider-great-expectations`的Python库...
《Greatis Ultimate Pack 3.87 + D3-DXE:深入解析Delphi开发工具集》 Greatis Ultimate Pack 3.87 + D3-DXE是一款专为Delphi开发者设计的强大工具集,旨在提高开发效率,优化代码质量和增强应用程序功能。这个7z...
《PyPI上的Great Expectations库详解与应用》 在Python编程世界中,PyPI(Python Package Index)是Python开发者获取和分享开源软件包的主要平台。本文将深入探讨PyPI上的一款名为“great_expectations”的库,它在...
This means that APIs aren’t just an IT solution. They involve every aspect of the business. They require a multi-discipline, cross-functional team to deliver APIs that meet the goals of both ...
"Write Great Code: Thinking Low-Level, Writing High-Level Volume 2" 《Write Great Code》第二卷的主要内容是介绍如何编写高效的代码,即使是高级语言也需要了解底层的实现机制。这本书教会读者如何思考低级、...
中职英语基础模块下册Unit The Great WallPPT学习教案.pptx
"GreatCode"是一款专注于C/C++代码格式化、整理和美化的工具,它为程序员提供了一个高效、便捷的方式来优化他们的源代码。在编程过程中,保持代码的整洁和规范至关重要,因为这不仅影响代码的可读性和可维护性,还...
【标题】"The-Great-Suspender_v6.21" 指的是一个特定版本的谷歌浏览器插件,The Great Suspender。这个插件专为优化浏览器性能而设计,尤其是针对那些长时间打开但未被使用的选项卡。在现代网络环境中,浏览器可能...
Great Wall Construction Algorithm(GWC算法)是一种新颖的元启发式算法,它的灵感来源于中国古代的伟大建筑——长城。这种算法利用长城的特征,如蜿蜒曲折、坚固耐用,来模拟搜索过程,寻找全局最优解。 在MATLAB...
《Python库Great Expectations详解与应用》 Great Expectations是一个强大的Python库,专注于数据质量检查和数据治理。在数据科学和分析领域,确保数据的质量是至关重要的,因为错误或不准确的数据可能导致误导性的...
"GREAT格力特VF-9系列变频器说明书" 变频器是通过控制电机的速度来实现节能和提高生产效率的一种装置。VF-9系列变频器是GREAT格力特公司生产的一款高性能变频器,具有强大的功能和高效的性能。在本说明书中,我们将...
【标题】"The Great Suspender 7.0.109_0.crx" 是一个扩展程序,专为谷歌浏览器(Google Chrome)设计。这款扩展的主要功能是提高浏览器的性能和效率,特别是在处理大量打开标签页时。"7.0.109_0" 表示这是该扩展的...
《Jim Collins - Good To Great》一书探讨了企业如何从优秀转变为卓越的深层次原因与实践路径,基于对多家公司的深入研究,提炼出了实现这一飞跃的关键因素。以下是对该书中核心知识点的详细阐述: ### 重要知识点...
重装系统后,是否不识别U盘等USB口,是否桌面图标特别大而且打开软件后整个屏幕装不下软件页面。。。。。。那就下载这个资源吧!长城电脑专用(进入blos的键为F11),包你解决这些问题,可能其他品牌电脑均适用!...