spring的openSessionInViewFilter能将session的生命周期延长到请求完成后结束。 解决了在hibernate的load方法中,并未把数据真正获取时就关闭了session,导致出现了异常的问题。
在web.xml中,openSessionInViewFilter要配载struts的过滤器之前才能起作用;另外还需要注意的是:若用到hibernateTemplate,但未声明事务边界,openSessionInView即默认认为事务是readyOnly,所以此时save数据会抛出异常:[color=red]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[/color]
[color=green]我的解决办法是在service的set方法上加 @Transactional [/color]
在web.xml中,openSessionInViewFilter要配载struts的过滤器之前才能起作用;另外还需要注意的是:若用到hibernateTemplate,但未声明事务边界,openSessionInView即默认认为事务是readyOnly,所以此时save数据会抛出异常:[color=red]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[/color]
[color=green]我的解决办法是在service的set方法上加 @Transactional [/color]