`

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

阅读更多

2009-11-28

昨天发现了一个问题:我的项目:

sturts1.2 hibernate3 spring2.5

使用注解 

问题描述:先调用查询的方法,然后更新数据的时候报错:

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.
	org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1186)
	org.springframework.orm.hibernate3.HibernateTemplate$14.doInHibernate(HibernateTemplate.java:718)
	org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
	org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
	org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:716)
	org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:712)
	dao.base.GenericDaoImpl.update(GenericDaoImpl.java:30)
	dao.Imp.WorkLogDaoImp.updateWorkLog(WorkLogDaoImp.java:44)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	java.lang.reflect.Method.invoke(Unknown Source)
	org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
	org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
	org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
	org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
	org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
	org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
	$Proxy6.updateWorkLog(Unknown Source)
	service.Imp.WorkLogServiceImp.updateWorklog(WorkLogServiceImp.java:37)
	struts.action.DayWorklog.updateWorkLog(DayWorklog.java:175)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	java.lang.reflect.Method.invoke(Unknown Source)
	org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
	org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
	org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)
	org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
	org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
	org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
	org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)


我想这个问题很多人都遇到了

看看源程序:

web.xml中

-----------------------------------------------------------------------

<!-- 使用spring解决hibernate因session关闭导致的延迟加载例外问题。 -->
  <filter>
     <filter-name>OpenSessionInViewFilter</filter-name>
     <filter-class>filter.OpenSessionFilter</filter-class>
  </filter>
  <filter-mapping>
     <filter-name>OpenSessionInViewFilter</filter-name>
     <url-pattern>*.do</url-pattern>
  </filter-mapping>

-----------------------------------------------------------------------

action 中

------------------------------------------

public ActionForward updateWorkLog(ActionMapping mapping, ActionForm form, HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  PoolForm poolform = (PoolForm)form;
  String time = poolform.getTime();
  String fun = poolform.getFunction();
  Integer id = poolform.getId();

//这里有查询 调用了dao类的方法
  Worklog wl = workLogService.findByid(id);
  if(fun.equals("turn"))
  {
   
   request.setAttribute("log", wl);
   request.setAttribute("nowtime", time);
   return mapping.findForward("update");
  }else
  {
   String begin = poolform.getBegin();
   String end  = poolform.getEnd();
   String content = poolform.getWorkcontent();
   String object = poolform.getObject();
   String scjd = request.getParameter("scjd"+id);
   
   wl.setBegin(begin);
   wl.setEnd(end);
   wl.setObject(object);
   wl.setScjd(Integer.parseInt(scjd));
   wl.setWorkcontent(content);
   wl.setDay(time);
  //这里有更新方法 调用了dao的方法;
   workLogService.updateWorklog(wl);
   request.setAttribute("nowtime", time);
   return mapping.findForward("addsus");
  }
  
 }

-------------------------------------------------

daoimp中

-------------------------------------

//查询方法

public Worklog findbyid(int id) {
  this.getHibernateTemplate().setCacheQueries(true);
  Object object = this.getObjectByCondition(" u.id="+id);
  if(object !=null)
  {
   Worklog log = (Worklog)object;
   return log;
  }else
  {
   throw new IllegalArgumentException("没有日志 worklogdaoimp -->findbyid方法");   
  }
  
 }

//更新的方法 使用了注解;
 @Transactional(readOnly=false,propagation=Propagation.REQUIRED)
 public void updateWorkLog(Worklog wl) {
  this.update(wl);
  
 }

-------------------------------

报错:分析原因:

参考;http://www.iteye.com/topic/32001

http://www.iteye.com/topic/33507

解决方法:

web.xml中

  <!-- 使用spring解决hibernatesession关闭导致的延迟加载例外问题。 -->

  <filter>

     <filter-name>OpenSessionInViewFilter</filter-name>

//自己写了一个过滤器 继承了opensessioninviewfilter覆盖了他的方法;

     <filter-class>filter.OpenSessionFilter</filter-class>

  </filter>

  <filter-mapping>

     <filter-name>OpenSessionInViewFilter</filter-name>

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

  </filter-mapping>

 

过滤器中:

package filter;

 

import org.hibernate.FlushMode;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.dao.DataAccessResourceFailureException;

import org.springframework.orm.hibernate3.SessionFactoryUtils;

import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;

 

public class OpenSessionFilter extends OpenSessionInViewFilter{

       protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {

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

              FlushMode flushMode = getFlushMode();

              if (flushMode != null) {

//将flshmode的方法改为auto

                     session.setFlushMode(flushMode.AUTO);

              }

              return session;

       }

      

}

问题解决;

end

 

0
0
分享到:
评论

相关推荐

    使用Spring引起的错误

    ### 使用Spring引起的错误:Write operations are not allowed in read-only mode (FlushMode.NEVER) #### 问题背景 在使用Spring框架时,特别是在与Hibernate结合的情况下,可能会遇到“Write operations are not...

    cors-filter-1.7.jar,cors-filter-2.5.jar,cors-filter-2.10.jar

    &lt;param-name&gt;cors.allowed.origins&lt;/param-name&gt; &lt;param-value&gt;*&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;cors.allowed.methods&lt;/param-name&gt; &lt;param-value&gt;GET,POST,HEAD,OPTIONS&lt;/param-...

    springmodules-cache.xsd&springmodules-ehcache.xsd.rar

    xml/ns/javaee":deferred-syntax-allowed-as-literal, "http://java.sun.com/xml/ns/javaee":trim-directive-whitespaces, "http://java.sun.com/xml/ns/javaee":default-content-type, ...

    cors-filter-1.7.jar,java-property-utils-1.9.jar

    &lt;param-name&gt;cors.allowed.origins&lt;/param-name&gt; &lt;param-value&gt;*&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;cors.allowed.methods&lt;/param-name&gt; &lt;param-value&gt;GET,POST,HEAD,OPTIONS,PUT,DELETE...

    Tomcat解决跨域的两个jar包java-property-utils-1.9.jar和cors-filter-1.7.jar

    allowed.headers=Content-Type, Authorization, X-Requested-With allow.credentials=true exposed.headers=X-Total-Count, X-Custom-Header ``` 这里,`allowed.origins`指定了允许跨域的源,`allowed.methods...

    HTTP错误大全

    **405 Method Not Allowed** - 请求行中指定的方法不被允许使用于请求相应的资源。 **406 Not Acceptable** - 服务器生成的响应没有满足客户端的MIME类型需求。 **407 Proxy Authentication Required** - 和401...

    cors-filter-1.7.jar spring解决跨域问题 java

    String allowedOrigins = props.getProperty("cors.allowed.origins"); ``` 总结: Spring框架提供了多种解决跨域问题的方法,包括注解和过滤器。`cors-filter-1.7.jar`是一个非Spring的CORS过滤器,可以单独或与...

    解决Tomcat跨域的jar包,java-property-utils-1.9.jar

    &lt;param-name&gt;cors.allowed.origins&lt;/param-name&gt; &lt;param-value&gt;*&lt;/param-value&gt; &lt;/init-param&gt; &lt;!-- 更多配置选项,如:允许的HTTP方法、允许的头部、是否允许证书等 --&gt; &lt;/filter&gt; &lt;filter-mapping&gt; ...

    解决fatal:remote error:You can’t push to git://github.com/username/*.git问题的办法

    解决fatal:remote error:You can’t push to git://github.com/username/*.git问题的办法 今天Git push的时候 fatal:remote error: You can't push to git://github....看来我是没有权限push啊。 解决方法: ...接

    golang.org/x下的包集合

    https://golang.org/x/下的相关的包国内无法下载,这个可以直接将解压后的golang.org-master/x目录下到包放到GOPATH目录下的golang.org/x路径下面,执行go install 包名称就行了

    net55-r8168-8.045a-napi.x86_64.rar

    2015-01-16: Version 8.039.01: Changed driver code to not use IP checksum hardware offload (caused various issues with networking in VMs) 2015-01-05: Version 8.039.00: Original version

    webgoat-standalone-7.1-SNAPSHOT-exec.jar

    Prerequisites: Java VM 1.8 Open a command shell/window, browse to where you ...Using the --help option will show the allowed command line arguments. 更多查看: https://github.com/WebGoat/WebGoat

    struts2.5.16升级过程中遇到问题详述

    &lt;global-allowed-methods&gt;regex:.*&lt;/global-allowed-methods&gt; &lt;!-- 添加具体的Action配置 --&gt; &lt;/package&gt; &lt;/struts&gt; ``` - **解释**: - `struts.enable.DynamicMethodInvocation`:设置为true启用动态方法调用...

    sciter-sdk-4.0.3.5348

    NOTE: sciter.dlls, sciter.exe and notes.exe Windows executables in SDK are digitally signed now. On MacOS you may need to run SDK executables directly from console as they are not signed too.

    QT 下载:Download from your IP address is not allowed

    Download from your IP address is not allowed 百度网盘永久连接: QT下载: qt-opensource-linux-x64-5.8.0.run: 链接:https://pan.baidu.com/s/1sQ3tqPaWdDnmhBYAc_XR7g qt-opensource-linux-x64-5.13.1....

    sciter-sdk-4.0.0.7

    NOTE: Windows executables in SDK are not signed so you may encounter security warnings on W8 and W10. On MacOS you may need to run SDK executables directly from console as they are not signed too.

    nrpe-2.15.tar.gz

    3. **配置** - `./configure` 4. **编译** - `make` 5. **安装** - `sudo make install` 6. **配置NRPE** - 根据需求编辑`/etc/nrpe.cfg`配置文件。 7. **启动NRPE服务** - 使用`/etc/init.d/nrpe start`或系统对应...

    PyPI 官网下载 | aws-cdk.aws-cloudfront-origins-1.97.0.tar.gz

    "allowed_methods": cloudfront.AllowedMethods.ALLOW_ALL, "cached_methods": cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS, "default_ttl": core.Duration.minutes(5), }, ) ``` 在上面的例子中,我们...

    cors-filter-1.7 + java-property-utils-1.10.zip

    - `cors.allowed.headers`:允许的请求头部,默认为Accept、Accept-Language、Content-Type、Last-Event-ID。 - `cors.exposed.headers`:服务器可以向浏览器暴露的响应头部。 - `cors.support.credentials`:是否...

    基础设施即代码(IAC),Zalando Postgres Operator UI 入门.doc

    1. **启用负载均衡器**:这允许外部访问到 PostgreSQL 集群,相关的设置文档链接是:https://postgres-operator.readthedocs.io/en/latest/administrator/#load-balancers-and-allowed-ip-ranges。 2. **卷大小**:...

Global site tag (gtag.js) - Google Analytics