您的位置:首页 > 运维架构

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

7、为了解决应用中的中文乱码问题,我们可以不用自己开发过滤器类,Spring为我们提供了一个,只需要配置一下即可:

characterEncodingFilter org.springframework.web.filter.Charact [color=brown][/color]erEncodingFilter encoding UTF-8 characterEncodingFilter /*

8、为了解决hibernate延迟加载的问题,使用Spring中提供的过滤器来解决,它能够让Session
在请求解释完成之后再关闭,配置方式如下:

hibernate session manager filter org.springframework.orm.hibernate3.support. OpenSessionInViewFilter hibernate session manager filter /*

9、因为OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。故进行insert、 update和delete操作时会产生异常: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. 因此需要采用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>

10、 部署应用程序,启动服务器,如果发现异常: java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V 这是由于整合时Jar包的冲突引起的。应将"Web应用程序/WEB-INF/lib/asm-2.2.3.jar"删除即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐