Hibernate session FlushMode浅析

xiaoxiao2021-03-01  46

说明FlushMode有五种属性1NEVEL已经废弃了,被MANUAL取代了2 MANUALspring3.x中的opensessioninviewfilter已经将默认的FlushMode设置为MANUAL了;如果FlushMode是MANUAL或NEVEL,在操作过程中hibernate会将事务设置为readonly,所以在增加、删除或修改操作过程中会出现如下错误org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition;解决办法网上有很多;a 配置事务,spring会读取事务中的各种配置来覆盖hibernate的session中的FlushMode;b 先编程式修改FlushMode,比如session.setFlushMode(FlushMode.AUTO); 这样hibernate就会自动去除readonly限制;c 直接修改opensessioninviewfilter过滤器的配置,配置过滤器的时候配置<filter><filter-name>openSession</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class><init-param><param-name>flushMode</param-name><param-value>AUTO</param-value></init-param></filter>3 AUTO设置成auto之后,当程序进行查询、提交事务或者调用session.flush()的时候,都会使缓存和数据库进行同步,也就是刷新数据库4 COMMIT提交事务或者session.flush()时,刷新数据库;查询不刷新4 COMMIT提交事务或者session.flush()时,刷新数据库;查询不刷新5 ALWAYS每次进行查询、提交事务、session.flush()的时候都会刷数据库这里需要说一下和AUTO的区别,当hibernate缓存中的对象被改动之后,会被标记为脏数据(即与数据库不同步了)。当session设置为FlushMode.AUTO时,hibernate在进行查询的时候会判断缓存中的数据是否为脏数据,是则刷数据库,不是则不刷,而always是直接刷新,不进行任何判断。很显然auto比always要高效得多。

转自:http://hi.baidu.com/ljmybfq/item/b751951e237557fb64eabf5d 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-4199952.html

最新回复(0)