operations are not allowed in read-only mode
2012-06-27 18:04
316 查看
operations are not allowed in read-only mode
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="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>
(转自http://blog.163.com/littlefermat/blog/static/5977116720096281075177/)
浅谈Struts+Hibernate+Spring的整合方法
1、在工程中导入spring支持,导入的Jar包有:◆ Spring 2.0 Core Libraries
◆Spring 2.0 ORM/DAO/Hibernate3 Libraries
◆ Spring 2.0 AOP Libraries
◆ Spring 2.0 Web Libraries
2、在Spring配置文件中配置dataSource和S
1、在工程中导入spring支持,导入的Jar包有:
◆ Spring 2.0 Core Libraries
◆Spring 2.0 ORM/DAO/Hibernate3 Libraries
◆ Spring 2.0 AOP Libraries
◆ Spring 2.0 Web Libraries
2、在Spring配置文件中配置dataSource和SessionFactory,将hibernate配置与Spring配置整合在一起(可以删除hibernate.cfg.xml文件);
3、导入数据库源所要使用的Jar包,如:DBCP所用的JAR包(commons-pool.jar);
4、修改所有DAO的hibernate实现,因为Spring中提供了一个HibernateDAOSupport类,可以简化数据库的操作。使用所有DAO类都继承自该类;
5、将DAO采用依赖注入的方式注入到Biz中,再将Biz采用依赖注入的方式注入到Action中,在Spring配置文件中做相应配置;
6、将Spring与Struts集成:
1)在spring配置文件配置Action:将Biz注入到Action中;
2)修改Struts的配置文件:将Action的type属性修改为:org.springframework.web.struts.DelegatingActionProxy;
3)在web.xml文件中配置监听器以及web应用的初始化参数:
contextConfigLocation /WEB-INF/applicationContext.xml /WEB-INF/applicationContext-beans.xml org.springframework.web.context.ContextLoaderListener |
characterEncodingFilter org.springframework.web.filter.Charact [color=brown][/color]erEncodingFilter encoding UTF-8 characterEncodingFilter /* |
在请求解释完成之后再关闭,配置方式如下:
hibernate session manager filter org.springframework.orm.hibernate3.support. OpenSessionInViewFilter hibernate session manager filter /* |
mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. 因此需要采用spring的事务声明,使方法受transaction控制:
<!-- 配置事务管理器 --> <bean id="transactionManager" class=" org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置Advice(事务的传播特性) --> <tx:advice id=" txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name=" search*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置事务管理器应用的范围 --> <aop:config> <aop:pointcut id="affectMethods" expression_r_r_r=" execution(* edu.accp.dao.hibImpl.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="affectMethods"/> </aop:config> |
相关文章推荐
- Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly
- Write operations are not allowed in read-only mode (FlushMode.NEVER)
- Write operations are not allowed in read-only mode 只读模式下(FlushMode.NEVER/MANUAL)写操作不允
- Code Recyclebin Contact me QQ:149906878 Write operations are not allowed in read-only mode (FlushMo
- Write operations are not allowed in read-only mode
- 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' mar
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM
- Open Session In View(Write operations are not allowed in read-only mode)
- hibernate框架学习错误集锦-org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL)
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode
- Write operations are not allowed in read-only mode (FlushMode.MANUAL):
- Write operations are not allowed in read-only mode
- Write operations are not allowed in read-only mode (FlushMode.MANUAL)错误解决
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM
- 解决Hibernate Write operations are not allowed in read-only mode的方法
- 关于ssh里面的Write operations are not allowed in read-only mode (FlushMode.NEVER)
- Write operations are not allowed in read-only mode 只读模式下(FlushMode.NEVER/MANUAL)写操作不允
- openSessionInView,如果没有配置事务,会出现Write operations are not allowed in read-only mode。。。错误
- Write operations are not allowed in read-only mode
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM