博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用OpenSessionInViewFilter 的影响
阅读量:6313 次
发布时间:2019-06-22

本文共 4397 字,大约阅读时间需要 14 分钟。

hot3.png

测试的控制器方法:

@ResponseBody    @RequestMapping(value = "/testFlush/json", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)    public String json2(Model model, HttpServletRequest request, HttpServletResponse response            , int id) {//        Session session=this.houseEvaluateDao.getSessionFactory().getCurrentSession();//        session.setFlushMode(FlushMode.MANUAL);        HouseEvaluate houseEvaluate =  this.houseEvaluateDao.getVariable(id);//        session.setReadOnly(houseEvaluate,true);        Double score = new Random().nextDouble();        System.out.println("score :" + score);        houseEvaluate.setScore(score);        HouseEvaluate houseEvaluate22 = this.houseEvaluateDao.getVariable(id - 1);        houseEvaluate22.setScore(new Random().nextDouble());        this.houseEvaluateDao.update(houseEvaluate22);        return BaseResponseDto.jsonValue(houseEvaluate);    }

没有使用

OpenSessionInViewFilter

时,访问的sql:

Hibernate:     select        houseevalu0_.id as id1_32_0_,        houseevalu0_.createTime as createTi2_32_0_,        houseevalu0_.evaluateText as evaluate3_32_0_,        houseevalu0_.evaluateType as evaluate4_32_0_,        houseevalu0_.houseInfo_id as houseInf8_32_0_,        houseevalu0_.orderNo as orderNo5_32_0_,        houseevalu0_.score as score6_32_0_,        houseevalu0_.updateTime as updateTi7_32_0_,        houseevalu0_.userId as userId9_32_0_     from        t_houseEvaluate houseevalu0_     where        houseevalu0_.id=?03 三月 2018 12:12:17,460 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [1] as [INTEGER] - [22]Hibernate:     select        houseevalu0_.id as id1_32_0_,        houseevalu0_.createTime as createTi2_32_0_,        houseevalu0_.evaluateText as evaluate3_32_0_,        houseevalu0_.evaluateType as evaluate4_32_0_,        houseevalu0_.houseInfo_id as houseInf8_32_0_,        houseevalu0_.orderNo as orderNo5_32_0_,        houseevalu0_.score as score6_32_0_,        houseevalu0_.updateTime as updateTi7_32_0_,        houseevalu0_.userId as userId9_32_0_     from        t_houseEvaluate houseevalu0_     where        houseevalu0_.id=?03 三月 2018 12:12:18,007 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [1] as [INTEGER] - [21]Hibernate:     /* update        oa.entity.HouseEvaluate */ update            t_houseEvaluate         set            createTime=?,            evaluateText=?,            evaluateType=?,            houseInfo_id=?,            orderNo=?,            score=?,            updateTime=?,            userId=?         where            id=?03 三月 2018 12:12:18,615 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [1] as [VARCHAR] - [2017-10-15 12:03:20]03 三月 2018 12:12:18,623 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [2] as [VARCHAR] - [发布房源]03 三月 2018 12:12:18,624 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [3] as [INTEGER] - [1]03 三月 2018 12:12:18,633 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [4] as [INTEGER] - [144]03 三月 2018 12:12:18,634 TRACE org.hibernate.type.descriptor.sql.BasicBinder:69 - binding parameter [5] as [VARCHAR] - [null]03 三月 2018 12:12:18,641 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [6] as [DOUBLE] - [0.7026864201132653]03 三月 2018 12:12:18,645 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [7] as [VARCHAR] - [2017-10-15 12:03:20]03 三月 2018 12:12:18,649 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [8] as [INTEGER] - [92]03 三月 2018 12:12:18,654 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [9] as [INTEGER] - [21]

即查询两次,更新一次,这非常好理解,因为我只调用了一次update

 

使用OpenSessionInViewFilter 之后

并且设置

session.setFlushMode(FlushMode.MANUAL);

再次访问,会执行两次update,即也更新了houseEvaluate ,虽然没有显式地调用update

但是我的本意不想更新houseEvaluate,因为我没有手动调用update

怎么办呢?

解决方法:

1,查询对象时,默认采用只读方式

session.setDefaultReadOnly(true);

或者:

session.setReadOnly(entityObj, sessionParam.isReadonly());

2,对于要更新的对象,再修改之前,执行:

this.houseEvaluateDao.setReadOnly(houseEvaluate22,false);

即把persist 对象变为可写.

注意:

(1)过滤器OpenSessionInViewFilter 中依然设置setFlushMode(FlushMode.MANUAL);

(2)必须在对象属性修改之前调用setReadOnly(houseEvaluate22,false),否则无效,不会更新

(3)session的setDefaultReadOnly 值为true

java socket实现的简易的聊天工具demo 见

转载于:https://my.oschina.net/huangweiindex/blog/1628232

你可能感兴趣的文章
如何提高Ajax性能
查看>>
Android--自定义加载框
查看>>
LINUX下 lamp安装及配置
查看>>
BZOJ3105 [cqoi2013]新Nim游戏
查看>>
困惑的前置操作与后置操作
查看>>
SDNU 1269.整数序列(水题)
查看>>
BZOJ 2118 Dijkstra
查看>>
Go语言基础之结构体
查看>>
SpringCloud:Eureka Client项目搭建(Gradle项目)
查看>>
jqueryValidate
查看>>
ATL使用IE控件,并且屏蔽右键
查看>>
Jenkins
查看>>
linux下使用screen和ping命令对网络质量进行监控
查看>>
数据库设计技巧
查看>>
css定位概述
查看>>
C# 动态修改配置文件 (二)
查看>>
BOM:文档对象模型 --树模型
查看>>
我的Android进阶之旅------>WindowManager.LayoutParams介绍
查看>>
segment
查看>>
获取鼠标的原始移动值
查看>>