`
zuiyanwangyue
  • 浏览: 164279 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

Write operations are not allowed in read-only

阅读更多

HibernateDaoSupport的子类在保存实体时抛出InvalidDataAccessApiUsageException异常,异常堆栈如下:

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.
 at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1090)
 at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:629)
 at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
 at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:627)

在网上搜了一下,其中大多数文章又是提OpenSessionInViewFilter又是提OpenSessionInViewInterceptor的,大多云山雾罩、不知所云。

其实这个异常的提示还是很明确的:在只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许:把你的Session改成FlushMode.COMMIT/AUTO或者清除事务定义中的readOnly标记。

首先看一下我Spring的配置文件,为了减少篇幅,仅将与事务有关的一部分贴在下面:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!-- Transaction template for Managers, from:
		http://blog.exis.com/colin/archives/2004/07/31/concise-transaction-definitions-spring-11/ -->
	<bean id="txProxyTemplate" abstract="true" 
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<!--org.springframework.orm.hibernate3.HibernateTransactionManager的Bean实例-->
			<ref bean="gkgltransactionManager" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="remove*">PROPAGATION_REQUIRED</prop>
				<prop key="do*">PROPAGATION_SUPPORTS</prop>	
				<prop key="auto*">PROPAGATION_SUPPORTS</prop>
				<!--对于其它方法要求事务并且是只读的-->
				<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
			</props>
		</property>
	</bean>
	<!-- Generic manager that can be used to do basic CRUD operations on any objects -->
	<bean id="manager" parent="txProxyTemplate">
		<property name="target">
			<bean class="com.neuqsoft.base.service.impl.BaseManager">
				<property name="dao">
					<ref bean="gkgldao" />
				</property>
			</bean>
		</property>
	</bean>
</beans>

  对于清除readOnly标记是很简单的,只需把上述配置文件中txProxyTemplate Bean定义中的<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>中的readOnly及其前面的逗号去掉即可。

接下来我们讨论把Session改成FlushMode.COMMIT/AUTO,下面是HibernateTemplate中checkWriteOperationAllowed方法的源码:

protected void checkWriteOperationAllowed(Session session) throws InvalidDataAccessApiUsageException {
		if (isCheckWriteOperations() && getFlushMode() != FLUSH_EAGER &&
				session.getFlushMode().lessThan(FlushMode.COMMIT)) {
			throw new 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.");
		}
	}

 通过阅读这段代码我们不难看出,在Java代码中调用HibernateTemplate的save或者saveOrUpdate等涉及到写操作的方法之前需要把Session的刷新模式设置为FlushMode.COMMIT或更高的级别,或者把HibernateTemplate的刷新模式设置为FLUSH_EAGER,由于我们的Dao继承自HibernateDaoSupport,所以设置Session刷新模式的语句如下:getSession().setFlushMode(FlushMode.COMMIT);

而设置HibernateTemplate刷新模式的语句如下:

HibernateTemplate tmp=getHibernateTemplate();   
tmp.setFlushMode(HibernateTemplate.FLUSH_EAGER);  

 只要在调用HibernateTemplate涉及到写操作的方法之前正确设置了HibernateTemplate或者Session的刷新模式,则上述异常不会再抛出。

分享到:
评论
2 楼 zuiyanwangyue 2014-04-18  
tomastong 写道
楼主,这里还有一个更好的方法,配置事务的话,可能会更简单!
什么方法?
1 楼 tomastong 2014-03-31  
楼主,这里还有一个更好的方法,配置事务的话,可能会更简单!

相关推荐

    使用Spring引起的错误

    使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决

    BURNINTEST--硬件检测工具

    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 =======================...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - ADD TFlexEllipse.Center - read-only public property. - FIX Trial packages now supports Delphi 2007 and Turbo versions (no changes for commercial packages). - FIX TFlexPanel.DragOver wrongs call the...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    In particular, do not add new parameters to the end of the function just because they are new; place new input-only parameters before the output parameters. This is not a hard-and-fast rule. ...

    Mask 98 for PRwin98

    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, ...

    微软内部资料-SQL性能优化2

    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 ...

    微软内部资料-SQL性能优化3

    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 ...

    a project model for the FreeBSD Project.7z

    These are usually the most active developers who are willing to spend their time not only integrating their own code but integrating code submitted by the developers who do not have this privilege....

    数位板压力测试

    The interface publishes read-only information through a single information interface. Applications interact with the interface by setting up tablet contexts and consuming event packets. Applications ...

    吴建业作文观点汇总

    8. The traditional school is a place where children get training of the mind and character to produce self-control, habits of obedience as well as learn to write and read, 9. Lack adequate physical ...

    python3.6.5参考手册 chm

    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...

Global site tag (gtag.js) - Google Analytics