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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:


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.


在使用hibernate模板对数据库进行读写操作时,有时会遇到删除、写入操作受限,对于这种异常,可以多种办法解决,

首先你测试先一下是添加受限,还是删除受限,如果添加和删除有一个可以成功,那么错误可能是:


第一)种错误,没有给对应方法配置权限,

<props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="do*">PROPAGATION_REQUIRED</prop>
    <prop key="recv*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>


第二)种错误,在Dao层中添加,删除方法错误,请测试查看异常


如果添加和删除都不成功,那么很有可能就是权限问题了,请继续看,以下办法可以解决

1)第一种在Dao层更改 session.setFlushMode(FlushMode.AUTO);

    操作如下:

//注入  hibernateTemplate
private HibernateTemplate hibernateTemplate;
//生成get(),set()方法
 public HibernateTemplate getHibernateTemplate()
 {
  return hibernateTemplate;
 }
 public void setHibernateTemplate(HibernateTemplate hibernateTemplate)
 {
  this.hibernateTemplate = hibernateTemplate;
 }
/**
  * 根据id删除记录
  * @param id
  */
 @Transactional("txManager")
 public int removeUser(Integer id)
 {
  // 1.参数检测
  if (index != null && hibernateTemplate != null&& hibernateTemplate.getSessionFactory() != null)
  {
   // 2.取得sission
   Session session = hibernateTemplate.getSessionFactory()
     .getCurrentSession();
   if (session != null)
   {
    //2.1 设置session属性
    session.setFlushMode(FlushMode.AUTO);
    // 2.2删除该id的对象
    Query query = session.createQuery("from User  where id ="
      + id);
    Comment comment = null;
    //3 非空检查
    if (query != null)
    {
     if (query.list().size() > 0)
     {
      user = (Comment) (query.list().get(0));
      // 4.1删除该对象
      hibernateTemplate.delete(user);
      // 4.2清理缓存
      session.flush();
     //返回一个表示,成功返回1,失败返回0
      return 1;
     }
    }
   }
  }
  return 0;
 }


2 ) 第二种在配置文件web.xml和applicationContext.xml中更改

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
 <display-name>Doshare</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <filter>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <filter-class>
 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter    
  </filter-class>
  <init-param>
   <param-name>sessionFactoryBeanName</param-name>
   <param-value>sessionFactory</param-value>
  </init-param>
  <init-param>
   <param-name>singleSession</param-name>
   <param-value>true</param-value>
  </init-param>
  <init-param>
   <param-name>flushMode</param-name>
   <param-value>AUTO </param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <jsp-config>
  <jsp-property-group>
   <display-name>JSPConfiguration</display-name>
   <url-pattern>*.htm</url-pattern>
   <el-ignored>true</el-ignored>
   <page-encoding>UTF-8</page-encoding>
   <scripting-invalid>false</scripting-invalid>
  </jsp-property-group>
  <jsp-property-group>
   <display-name>JSPConfiguration</display-name>
   <url-pattern>*.jsp</url-pattern>
   <el-ignored>false</el-ignored>
   <page-encoding>UTF-8</page-encoding>
   <scripting-invalid>false</scripting-invalid>
  </jsp-property-group>
 </jsp-config>
 <!-- Struts2 核心过滤器 -->
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- Spring 監聽器 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
</web-app>


applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd">
 <!-- 指定C3P0连接池 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass" value="org.mariadb.jdbc.Driver"></property>
  <property name="jdbcUrl"
   value="jdbc:mysql://localhost:3308/test?userUnicode=true&characterEncoding=UTF-8"></property>
<!--数据库用户名和密码-->
  <property name="user" value="root"></property>
  <property name="password" value="123456"></property>
<!--最大连接数-->
  <property name="maxPoolSize" value="500"></property>
<!--最小连接数-->
  <property name="minPoolSize" value="5"></property>
  <!-- 保持MySQL连接 -->
  <property name="preferredTestQuery" value="SELECT 1"></property>
 </bean>
 <!-- 使用Spring管理Hibernate -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <!-- Hibernate的属性 -->
  <property name="hibernateProperties">
   <props>
    <!-- 连接池自动重连 -->
    <prop key="hibernate.connection.autoReconnectForPools">true</prop>
    <!-- 连接编码 -->
    <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
    <!-- 方言 -->
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <!-- showsq -->
    <prop key="hibernate.show_sql">true</prop>
    <!-- 避免JDBC出错,仅限于MySQL -->
    <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
    <!-- 自动创建表 -->
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
    </prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
     <value>com/doshr/xmen/app/server/bean/Comment.hbm.xml</value>
    <value>com/doshr/xmen/app/server/bean/WeChatFriend.hbm.xml</value>
    <value>com/doshr/xmen/app/server/bean/Address.hbm.xml</value>
    <value>com/doshr/xmen/app/server/bean/Propertyes.hbm.xml</value> 
   </list>
  </property>
 </bean>
 <!-- 配置Spring的Hibernate模板 -->
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- 配置Spring事务管理器 -->
 <bean id="txManager"
  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- 配置事務攔截器 -->
 <bean id="txInterceptor"
  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
   <ref bean="txManager" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="do*">PROPAGATION_REQUIRED</prop>
    <prop key="recv*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
 <!-- 事务代理 -->
 <bean
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   <property name="interceptorNames">
   <list>
    <value>txInterceptor</value>
   </list>
  </property> 
   <property name="beanNames">
   <list>
    <value>*Dao</value>
    <value>*Service</value>
   </list>
  </property> 
 </bean>
</beans>


3)第三种写一个过滤器,让类(继承)extends
  org.springframework.orm.hibernate4.support.OpenSessionInViewFilter  重写方法

<!--结束-->

有问题的或者异常的,请说明信息,一起讨论


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
C语言
[Error] ‘for‘ loop initial declarations are only allowed in C99 or C11 mode 解决方法
[Error] ‘for’ loop initial declarations are only allowed in C99 or C11 mode [Note] use option -std=c99,-std=gnu99,-std=c11 or-std=gnu11 to compile your code
1187 0
[Error] ‘for‘ loop initial declarations are only allowed in C99 or C11 mode 解决方法
|
C语言
[Error] ‘for‘ loop initial declarations are only allowed in C99 mode
[Error] ‘for‘ loop initial declarations are only allowed in C99 mode
291 0
[Error] ‘for‘ loop initial declarations are only allowed in C99 mode
is transfer = C ( only read dynamically) not supported in one order scenario
is transfer = C ( only read dynamically) not supported in one order scenario
102 0
is transfer = C ( only read dynamically) not supported in one order scenario
transfer = C only read dynamically - why it fails to work
transfer = C only read dynamically - why it fails to work
104 0
transfer = C only read dynamically - why it fails to work
|
关系型数据库
### avoid read-on-write
### avoid read-on-write 什么是 "read-on-write" problem? 在我们使用最常见的buffer write 中 "read-on-write" 问题指的是当我需要进行小于4k 大小buffer write 的时候, 需要先将数据所在的page 从disk 中读取出放入到page cache, 在page cache 中修改好, 然后再将
1436 0
|
关系型数据库 MySQL Java
Connection is read-only. Queries leading to data modification are not allowed
看了下mysql-connector-5.1.40版本中,如果设置failoverReadOnly=true (即默认值,参考链接),当mysql连接failover时,会根据jdbc连接串将当前连接的readOnly值设置为true (第8行代码) 1 2 3 4 ...
3358 0