Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

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

简介:


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  重写方法

<!--结束-->

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



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章