Write operations are not allowed in read-only mode

成功解决InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMod 因为Jpa的EntityManageFactory会与Hibernate的SessionFactory造成事务冲突,所以需要在启动类上加上下面这个注解。 阅读详情

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

在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛session already closed Exception; Open Session In View提供了一种简便的方法,较好地解决了lazy loading问题.

它有两种配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具体参看SpringSide),功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。

Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。

 

OpenSessionInViewInterceptor配置

 

  1.  

    <beans>

     

  2.  

    <bean name="openSessionInViewInterceptor"

     

  3.  

    class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">

     

  4.  

    <property name="sessionFactory">

     

  5.  

    <ref bean="sessionFactory"/>

     

  6.  

    </property>

     

  7.  

    </bean>

     

  8.  

    <bean id="urlMapping"

     

  9.  

    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

     

  10.  

    <property name="interceptors">

     

  11.  

    <list>

     

  12.  

    <ref bean="openSessionInViewInterceptor"/>

     

  13.  

    </list>

     

  14.  

    </property>

     

  15.  

    <property name="mappings">

     

  16.  

    ...

     

  17.  

    </property>

     

  18.  

    </bean>

     

  19.  

    ...

     

  20.  

    </beans>

     

 

 

OpenSessionInViewFilter配置

 

  1.  

    <web-app>

     

  2.  

    ...

     

  3.  

    <filter>

     

  4.  

    <filter-name>hibernateFilter</filter-name>

     

  5.  

    <filter-class>

     

  6.  

    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

     

  7.  

    </filter-class>

     

  8.  

    <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->

     

  9.  

    <init-param>

     

  10.  

    <param-name>singleSession</param-name>

     

  11.  

    <param-value>true</param-value>

     

  12.  

    </init-param>

     

  13.  

    </filter>

     

  14.  

    ...

     

  15.  

    <filter-mapping>

     

  16.  

    <filter-name>hibernateFilter</filter-name>

     

  17.  

    <url-pattern>*.do</url-pattern>

     

  18.  

    </filter-mapping>

     

  19.  

    ...

     

  20.  

    </web-app>

     

 

很多人在使用OpenSessionInView过程中提及一个错误:

 

  1.  

    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations

     

  2.  

    are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into

     

  3.  

    FlushMode.AUTO or remove 'readOnly' marker from transaction definition

     

 

看看OpenSessionInViewFilter里的几个方法

 

  1.  

    protected void doFilterInternal(HttpServletRequest request,

    HttpServletResponse response,FilterChain filterChain)

    throws ServletException, IOException {

     SessionFactory sessionFactory = lookupSessionFactory();

     logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");

     Session session = getSession(sessionFactory);

     TransactionSynchronizationManager.bindResource(

      sessionFactory, new SessionHolder(session));

     try {

      filterChain.doFilter(request, response);

     }

     finally {

     TransactionSynchronizationManager.unbindResource(sessionFactory);

     logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");

     closeSession(session, sessionFactory);

     }

    }

     

     

  2.  

    protected Session getSession(SessionFactory sessionFactory)

    throws DataAccessResourceFailureException {

     Session session = SessionFactoryUtils.getSession(sessionFactory, true);

     session.setFlushMode(FlushMode.NEVER);

     return session;

    }

     

  3.  

    protected void closeSession(Session session, SessionFactory sessionFactory)

    throws CleanupFailureDataAccessException {

     SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory);

    }

 

可以看到OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。然后把该sessionFactory绑定到 TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该 sessionFactory的绑定,最后closeSessionIfNecessary根据该 session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。

 

  1.  

    public static void closeSessionIfNecessary(Session session, SessionFactory sessionFactory)

     

  2.  

    throws CleanupFailureDataAccessException {

     

  3.  

    if (session == null ||

    TransactionSynchronizationManager.hasResource(sessionFactory)) {

     

  4.  

    return;

     

  5.  

    }

     

  6.  

    logger.debug("Closing Hibernate session");

     

  7.  

    try {

     

  8.  

    session.close();

     

  9.  

    }

     

  10.  

    catch (JDBCException ex) {

     

  11.  

    // SQLException underneath

     

  12.  

    throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex.getSQLException());

     

  13.  

    }

     

  14.  

    catch (HibernateException ex) {

     

  15.  

    throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex);

     

  16.  

    }

     

  17.  

    }

     

 

也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有 insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction保护的方法有写权限,没受保护的则没有。

采用spring的事务声明,使方法受transaction控制

 

  1.  

    <bean id="baseTransaction"

    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

    abstract="true">

    <property name="transactionManager" ref="transactionManager"/>

    <property name="proxyTargetClass" value="true"/>

    <property name="transactionAttributes">

    <props>

    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>

    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>

    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>

    <prop key="save*">PROPAGATION_REQUIRED</prop>

    <prop key="add*">PROPAGATION_REQUIRED</prop>

    <prop key="update*">PROPAGATION_REQUIRED</prop>

    <prop key="remove*">PROPAGATION_REQUIRED</prop>

    </props>

    </property>

    </bean>

     

  2. <bean id="userService" parent="baseTransaction">

     

    <property name="target">

    <bean class="com.phopesoft.security.service.impl.UserServiceImpl"/>

    </property>

    </bean>

 

对于上例,则以save,add,update,remove开头的方法拥有可写的事务,如果当前有某个方法,如命名为importExcel(),则因没有transaction而没有写权限,这时若方法内有insert,update,delete操作的话,则需要手动设置flush model为Flush.AUTO,如

 

  1.  

    session.setFlushMode(FlushMode.AUTO);

     

  2.  

    session.save(user);

     

  3.  

    session.flush();

     

 

尽 管Open Session In View看起来还不错,其实副作用不少。看回上面OpenSessionInViewFilter的doFilterInternal方法代码,这个方法实际上是被父类的doFilter调用的,因此,我们可以大约了解的OpenSessionInViewFilter调用流程: request(请求)->open session并开始transaction->controller->View(Jsp)->结束transaction并 close session.

一切看起来很正确,尤其是在本地开发测试的时候没出现问题,但试想下如果流程中的某一步被阻塞的话,那在这期间connection就一直被占用而不释放。最有可能被阻塞的就是在写Jsp这步,一方面可能是页面内容大,response.write的时间长,另一方面可能是网速慢,服务器与用户间传输时间久。当大量这样的情况出现时,就有连接池连接不足,造成页面假死现象。

Open Session In View是个双刃剑,放在公网上内容多流量大的网站请慎用。

文章出自:http://blog.sina.com.cn/s/blog_5dc12c490100crr5.html

 

 

 

 

其他参考网站;

 

http://topic.youkuaiyun.com/u/20080605/23/d336d826-39d4-475c-b460-f4ff01f676b1.html

多模态大模型助力AI人工智能领域的智能决策 我们的目的是深入了解多模态大模型是怎样在AI人工智能领域帮助做出智能决策的。范围涵盖了多模态大模型的基本概念、工作原理、在不同场景下的应用,以及未来的发展方向等。接下来,我们会先介绍多模态大模型和智能决策的核心概念,再讲讲它们之间的关系。然后说说多模态大模型在智能决策中的算法原理和操作步骤,用数学公式来解释一下。接着通过一个项目实战案例让大家更直观地感受。之后看看多模态大模型在实际中的应用场景,推荐一些学习的工具和资源。最后探讨未来的发展趋势和挑战,进行总结并提出一些思考题。多模态大模型。 阅读详情

相关推荐

【人工智能】人工智能基础概念、流派、发展史

人工智能初衷就是让计算机能够像人类一样具有自我思考、自我学习、自我决策的能力。依据百度百科上对人工智能定义:人工智能是研究、开发用于模拟、扩展和延伸人的智能的理论、方法、技术及应用系统的一门新的技术学科。人工智能是研究使计算机来模拟人的某些思维过程和智能行为的学科,主要包括计算机实现智能的原理、制造类似于人脑智能的计算机,使计算机能实现更高层次的应用。

WEL测试 1876

Write operations are not allowed in read-only mode (FlushMode.MANUAL) 的解决办法

问题描述:    近期根据需求架构了一个小型项目,由于是公司内部使用,没有考虑三高(即高并发、高可用、高扩展),所以使用了SSH(Spring\Struts\Hibernate)进行开发的,主体架构完成后,进行的第一个操作就遇到了一个问题,内容如下:org.springframework.dao.InvalidDataAccessApiUsageException: Write operation...

Spring Boot-Common On With You 8974

肖特基二极管选型参数,结构原理,工艺与注意问题总结

肖特基二极管(Schottky Diode)是一种特殊的二极管,也被称为肖特基势垒二极管。它是利用金属与半导体接触形成的金属-半导体结原理制成的。与传统的PN结二极管相比,肖特基二极管具有更低的正向压降、更高的开关速度、更高的反向恢复时间以及更好的温度稳定性等特点。本文对肖特基二极管的工作原理,结构特点,工艺流程,选型参数及设计注意事项进行总结。

fydar的博客 812

工作笔记-Spring5的坑:Write operations are not allowed in read-only mode (FlushMode.MANUAL)

最近项目把Hibernate和Spring升级到了5,记录出现的常见问题。 在对数据源进行写操作(插入、更新、删除)的时候,遇到这样的报错 org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUA...

马路上开飞机 4468

一篇把 Transport 做到可复制的实战指南:从 CTS 底层表到 LSMW、Variant、Layout、TABU、Query、SAPscript 全对象搬运

本文深入解析SAP系统中的Transport机制,将其视为可审计、可追溯的变更流水线。文章从CTS/Transport Organizer通用方法论和具体对象搬运手法两个层面展开,详细介绍了LSMW项目、程序Variant、屏幕/ALV Layout、表数据、ABAP Query、SAPscript和SO10标准文本等对象的传输策略。通过真实案例说明,代码对象往往能被正确传输,但"运行体验"和"业务配置"却常被遗漏,导致QAS测试正常而PRD环境异常。文章提供了一套可

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 167

Rockchip开发系列 - 3.4.GPIO扩展芯片AW95xx和PCA953X(2)

Rockchip开发系列 - 3.3.GPIO扩展芯片AW95xx和PCA953X(2)

fulinux的博客 999

Open Session In View(Write operations are not allowed in read-only mode)

 使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决:       在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loa

unicorn的专栏 1134

【华为OD机试】贪吃蛇(python, java, c++, js)

贪吃蛇是一个经典游戏,蛇的身体由若干方格连接而成,身体随蛇头移动。蛇头触碰到食物时,蛇的长度会增加一格。蛇头和身体的任一方格或者游戏版图边界碰撞时,游戏结束。 下面让我们来完成贪吃蛇游戏的模拟。 给定一个NM的数组arr,代表NM个方格组成的版图,贪吃蛇每次移动一个方格。 若arr[i][j] == H,表示该方格为贪吃蛇的起始位置; 若arr[i][j] == F,表示该方格为食物, 若arr[i][j] == E,表示该方格为空格。 贪吃蛇初始长度为1,初始移动方向为向左。

A_D_I_D_A_S的博客 540

错误:Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL)

这几天,碰到个错误: org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or rem

吴静的blog 1万+

滑块逆向及流程分析

本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容、敏感网址、数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关!

pyzzd的博客 8004

智能家居系统设计:基于MATLAB的智能家居系统设计和仿真,包括智能家居控制、家庭能源管理和家庭安全监控

基于 MATLAB 的智能家居系统设计方法可以有效地解决各类智能家居问题。

走向CTO的路上... 1775

Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL)

在使用Spring+Hibernate或Spring+Hibernate+Struts进行增删改时会提示: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL) 分析原因如下: 在使用Spring+Hibernate或Spring+Hibernate+Struts的时候,会开启OpenSessi

JavaQ 3050

修复Oracle MySQL Server 安全漏洞(CVE-2023-0464)

修复Oracle MySQL Server 安全漏洞(CVE-2023-0464)

路~可以走过 3682

Code Recyclebin Contact me QQ:149906878 Write operations are not allowed in read-only mode (FlushMo

方法一: 遇到这个情况,我猜: 1、你一定使用的spring+hibernate 2、你一定在applicationContext.xml文件中写了事务配置如下: Xml代码       tx:advice id="txAdvice" transaction-manager="transactionManager">          tx:attributes>

pingpinggaga的博客 669

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Write operations are not allowed in read-only mode (FlushMode.MANUAL)

报错信息: 严重: Exception occurred during processing request: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker ...

java_laowang的博客 497

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into

Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AU org.springframework.dao.InvalidDataAccessApiUsageException: Write operatio

jazywoo_在路上 959

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Write operations are not allowed in read-only mode (FlushMode.NEVER/

今天在做ssh项目开发的时候遇到一个问题,保存数据的时候报错: Write operations are not allowed in read-only mode (FlushMode.NEVER/ 后参考这篇文章才知道出了什么问题:http://www.aichengxu.com/view/37412 原来在Spring事务管理中save类的方法都没有read-only=true,但是我...

weixin_34102807的博客 160

Write operations are not allowed in read-only mode错误解决

最近在配置 Structs, spring 和hibernate整合的问题: 开启OpenSessionInViewFilter来阻止延迟加载的错误的时候抛出了这个异常:     org.springframework.dao.InvalidDataAccessApiUsageException错误 但是在我们开启OpenSessionInViewFilter这个过滤器的时候Fl

qi183472492的博客 644

Write operations are not allowed in read-only mode怎么解决

HTTP Status 500 - 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.type...

qq_41749929的博客 347

Spring Write operations are not allowed in read-only mode 错误

使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决:        在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loadin...

点点知识小屋 287
上一篇: org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER)
下一篇: 9款CSS菜单生成器/含在线CSS导航菜单生成和CSS菜单生成软件
sukyle
博客等级 码龄18年 41粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值