Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
File: org/springframework/orm/hibernate5/HibernateTemplate.java
Line number: 1,093
Stacktraces
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' marker from transaction definition.
解决方法1
涉及到数据库的读写都要添加 @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW )
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW )
@Override
public void save(User user) {
userDao.save(user);
}
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW )
@Override
public void update(User user) {
userDao.update(user);
}
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW )
@Override
public void remove(User user) {
userDao.remove(user);
}
解决方法2
在应用程序上下文中使用bean下面的HibernateTemplate。
添加<property name="checkWriteOperations" value="false">
<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="checkWriteOperations" value="false"></property>
</bean>
同时在类上要添加@Transactional
@Transactional
@Service
public class UserServiceImpl implements UserService
{
...
}
错误原因
OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。然后把该sessionFactory绑定到TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该sessionFactory的绑定,最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。
在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction(声明式的事务)保护的方法有写权限,没受保护的则没有。
原理分析
当使用Spring OpenSessionInViewFilter并尝试在Spring管理的事务之外执行持久性操作时,通常会看到该错误消息。
过滤器将会话设置为FlushMode.NEVER / MANUAL(取决于您使用的Spring和Hibernate的版本 - 它们大致相当)。
当Spring事务机制开始一个事务时,它将刷新模式更改为“COMMIT”。事务完成后,它会根据需要将其设置为NEVER / MANUAL。
如果你完全确定如果没有发生这种情况,那么下一个最可能的罪魁祸首就是非线程安全地使用Session。Hibernate Session必须只在一个线程中使用。如果它穿过线程之间,就会发生各种混乱。请注意,从Hibernate加载的实体可以保存对加载它的Session的引用,因此跨线程处理实体也可以导致从另一个线程访问Session。
Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.的更多相关文章
- spring整合问题分析之-Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
1.异常分析 Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into ...
- ssh中的 Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
这个错误我整理了 半天才发现问题的存在. 尝试了网上的很多办法,但是最后都没有达到效果. 包括这两种: 第一种: web.xml种的配置 <filter> <filter-name ...
- HTTP Status 500 - Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
如果在service类上面没有添加注解,出现异常 @Transactional
- spring整合之后运行报什么只读错误。Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
解决办法, 再大dao的实现类上添加注解: @Transactional(readOnly = false ) 不让它只读就行了
- 增删改查 报异常org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readO
可能是Spring配置文件 事务通知里面的方法 与实际方法不匹配 <tx:advice id="advice" transaction-manager="tran ...
- ssh整合思想 Spring分模块开发 crud参数传递 解决HTTP Status 500 - Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or(增加事务)
在Spring核心配置文件中没有增加事务方法,导致以上问题 Action类UserAction package com.swift.action; import com.opensymphony.xw ...
- Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnl
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read ...
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn
这个异常我在网上查看了很多资料,一般都说是hibernate的session问题,让重新两个方法,但是我以前用的时候没问题啊,所以一直找问题,终于这个bug让我找到了,就是因为我插入操作的时候用的是别 ...
- 解决Hibernate Write operations are not allowed in read-only mode的方法
错误信息: org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed i ...
随机推荐
- Hadoop--Unable to load native-hadoop library for your platform... using builtin-java classes where applicable Starting namenodes on [localhost]
Unable to load native-hadoop library for your platform... using builtin-java classes where applicabl ...
- java项目中登陆时记住密码
1.在登陆的时候记住密码,不知自动登陆: 2.登陆页面,填写用户名,密码,点击记住密码,下次进入登陆页面的时候,填写同样的用户名,密码自动填充(在不一次会话的情况下也就是说在不关闭浏览器的情况下): ...
- linux查看主机最后启动时间
1.使用who查看最后重启时间 who -b 2.使用last查看近几次重启时间 last reboot 第一条即为最后一次重启日期 last其实是查询用户的登录记录,reboot是一个伪用户:也就是 ...
- 逆袭之旅DAY30.XIA.集合
2018年7月26日 面试题:List和set的区别 ArrayList 遍历效率较高,但添加和删除较慢 遍历集合最高效的方法:迭代器 集合的遍历: 迭代器:Iterator 创建 为什么使用泛型: ...
- MAVEN 创建项目
使用archetype生成项目骨架 MAVEN 创建项目JAR 和 MAVEN创建项目WAR中是使用特定的acrchetype来进行创建项目,如果使用其他的archetype来创建项目或是使用 mvn ...
- 网口扫盲二:Mac与Phy组成原理的简单分析
1. general 下图是网口结构简图.网口由CPU.MAC和PHY三部分组成.DMA控制器通常属于CPU的一部分,用虚线放在这里是为了表示DMA控制器可能会参与到网口数据传输中. MAC(Medi ...
- JS 浮点型计算的精度问题 推荐的js 库 推荐的类库 Numeral.js 和 accounting.js
推荐的类库 Numeral.js 和 accounting.js 文章来自 http://www.css88.com/archives/7324#more-7324
- Spring Boot + thymeleaf 后台与页面(二)
Spring Boot推荐使用thymeleaf模板完成与页面的交互(已不支持JSP某些特性,不推荐JSP) 步骤 在一个Spring Boot Web项目基础上,也可以参考我前一篇文章建立的项目 1 ...
- Valgrind,内存调试工具
Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具 官网:http://valgrind.org/ 用户开发手册地址:http://valgrind.org/docs/manu ...
- 软件设计基础-C/S系统
在软件设计开发过程中,逐渐形成了一些针对特定应用领域的软件系统组织方式的惯用模式 如经典的C/S(client/server,客户/服务器)模式和B/S(browser/server,浏览器/服务器) ...