欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

org.springframework.dao.InvalidDataAccessApiUsageException 异常

程序员文章站 2022-04-12 20:59:16
...
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

在使用hibernate 的 HibernateTemplate的保存方法时出现该异常,
本人经过研究与查资料找到的两种解决办法如下:

[color=green]1、在调用保存方法前将flushMode改为auto[/color]


this.ht.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);


[color=green]2、修改web.xml中spring的过滤器:OpenSessionInViewFilter参数如下[/color]


<filter>
<filter-name>OpenSessionInViewFilter</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>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>