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.
个人解决方法:
1.spring的配置文件一定要写,找到applicationContext-core.xml并打开,其中<tx:/>标签很重要,可能需手动配
<!-- 配置事务的传播特性 ,指定事务管理器-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置详细的事务语义 -->
<tx:attributes>
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
2.spring中的包的路径一定要写对,下面的红色部分不能写错。
<!-- 哪些类的哪些方法参与事务 -->
<aop:config>
<!--配置切入点,匹配com.company.biz.service.impl包下所有的类的所有方法的执行-->
<aop:pointcut id="allServiceMethod"
expression_r_r_r="execution(* com.company.biz.service.impl.*.*(..))" />
<!-- 指定在txAdvice切入点应用txAdvice事务切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" />
</aop:config>
在用openSessionInView时,如果采用默认的事务管理(就是在spring配置文件里没有配置事务管理),在调用 HibernateDaoSupport类里的getHibernateTemplate()里的增删改方法时,会抛出 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.这个错.
第一次看到这个错,肯定会觉得纳闷,它要你修改session的模式,或者移除readOnly marker.但是你从来都没有设置session的模式.
原因:spring2.0里面的OpenSessionInViewFilter的getSession方法中会对session的flushMode设定一个默认为NEVER的值,而这个值在hibernate3.0似乎是不能理解的,当然就不行了,所以报错就要你修改session的模式.
解决方法:
方法一.,就是继承OpenSessionInViewFilter类,然后重写这个方法,加句 this.setFlushMode(FlushMode.AUTO);或者干脆把session里面直接扔个FlushMode.AUTO,然后再重写一个叫closeSession的方法,记住一定要重写,因为增加了flushMode以后要调用session.flush()才可以正常提交数据,其实重写closeSession就是为了加1句session.flush(),然后下面调用super.closeSession()方法就行了。这种方法是我在网上找的一网友的解决方法,不过我不推荐这种方法
方法二:在spring配置文件里配置自己的事务,
<bean
>
<property ref="sessionFactory" />
</bean>
<tx:advice >
<tx:attributes>
<tx:method propagation="REQUIRED" />
<tx:method propagation="REQUIRED" />
<tx:method read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor
pointcut="execution(* cn.xg.service.impl.*.*(..))"
advice-ref="txAdvice" />
</aop:config>
分享到:
相关推荐
### 使用Spring引起的错误:Write operations are not allowed in read-only mode (FlushMode.NEVER) #### 问题背景 在使用Spring框架时,特别是在与Hibernate结合的情况下,可能会遇到“Write operations are not...
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 ...
`web.xml`配置文件中的`OpenSessionInViewFilter`是为了在HTTP请求的生命周期内保持一个Hibernate的Session,这样可以在视图渲染过程中执行数据库操作,防止“Write operations are not allowed in read-only mode”...
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 ...
Windows 98 and ME are not supported in BurnInTest version 5.3 and above. Use a version of BurnInTest prior to 5.2 for compatibility with W98 and ME. Windows 95 and Windows NT =======================...
When True then in ftmPanning mode will be shown only cached image of the initial client area. All parts outside will paints of the TFlexPanel.Brush. When returning from ftmPanning mode the complete ...
Because device drivers operate at DPC/dispatch level (covered in lesson 2), and page faults are not allowed at this level or above, most device drivers use non-paged pool to assure that they do not ...
Note: You are not allowed to distribute the PRWIN98.INI configuration file if it contains registered user details. However, please feel free to distribute the original, unregistered shareware files, ...
Consistency and isolation are the most important in describing SQL Server’s locking model. It is up to the application to define what consistency means, and isolation in some form is needed to ...
The following are all the settings which are different in either the production ; or development versions of the INIs with respect to PHP's default behavior. ; Please see the actual settings later in...
Modern web browsers render pages faster when they are in their standards mode than when they are in their backwards compatible mode. 适应未来网页浏览器: When you use defined standards and valid...
Notable changes in Python 3.6.5 What’s New In Python 3.5 Summary – Release highlights New Features PEP 492 - Coroutines with async and await syntax PEP 465 - A dedicated infix operator for matrix...