读书人

spring的openSessionInView一些注意要

发布时间: 2012-11-10 10:48:50 作者: rapoo

spring的openSessionInView一些注意要点
spring的openSessionInViewFilter能将session的生命周期延长到请求完成后结束。 解决了在hibernate的load方法中,并未把数据真正获取时就关闭了session,导致出现了异常的问题。

在web.xml中,openSessionInViewFilter要配载struts的过滤器之前才能起作用;另外还需要注意的是:若用到hibernateTemplate,但未声明事务边界,openSessionInView即默认认为事务是readyOnly,所以此时save数据会抛出异常:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition
我的解决办法是在service的set方法上加 @Transactional

读书人网 >编程

热点推荐