Impact of using OpenSessionInViewFilter

Test controller method:

 @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);
    }

No use

OpenSessionInViewFilter

The sql accessed:

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 March 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 March 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 March 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 March 2018 12:12:18,623 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [2] as [VARCHAR] - [Publishing housing sources]
03 March 2018 12:12:18,624 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [3] as [INTEGER] - [1]
03 March 2018 12:12:18,633 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [4] as [INTEGER] - [144]
03 March 2018 12:12:18,634 TRACE org.hibernate.type.descriptor.sql.BasicBinder:69 - binding parameter [5] as [VARCHAR] - [null]
03 March 2018 12:12:18,641 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [6] as [DOUBLE] - [0.7026864201132653]
03 March 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 March 2018 12:12:18,649 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [8] as [INTEGER] - [92]
03 March 2018 12:12:18,654 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [9] as [INTEGER] - [21]

That is, query twice and update once, which is very understandable, because I only call update once

 

After using OpenSessionInViewFilter

And set up

session.setFlushMode(FlushMode.MANUAL);

Once again, update will be performed twice, that is, houseEvaluate will also be updated, although update is not called explicitly

But I didn't want to update houseEvaluate because I didn't call update manually

What shall I do?

resolvent:

1. When querying objects, read-only mode is used by default

session.setDefaultReadOnly(true);

Or:

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

2. For the object to be updated, execute the following before modifying:

this.houseEvaluateDao.setReadOnly(houseEvaluate22,false);

That is, to make the persist object writable

Be careful:

(1) setFlushMode(FlushMode.MANUAL) is still set in the filter opensessioninview filter;

(2) setReadOnly(houseEvaluate22,false) must be called before the object property is modified, otherwise it will not be updated.

(3) setDefaultReadOnly value of session is true

See demo, a simple chat tool implemented by java socket

https://gitee.com/kunlunsoft/inetAdress_socket.git

Tags: Hibernate SQL Session JSON

Posted on Sat, 04 Apr 2020 03:32:01 -0700 by konigwolf