[SSH] Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction
来源:互联网 发布:美颜摄像头软件下载 编辑:程序博客网 时间:2024/05/29 18:01
当执行到service层时,发生了如题的错误。
原因:
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配置文件中加上这一部分
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="import*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED"/> <!--这里如果觉着麻烦只要最后一行*就可以了,因为他会扫描所有的函数的 --> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <aop:pointcut id="serviceOperation" expression="execution(* com.hdu.service.impl..*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
或者如果不想配置配置文件
,请在service层上面加上一个@Transactional
这样service层就加入了事务列,然后在所有的dao层中的写操作加一行getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
像下面这样
public void doCreateUser(Student student) { getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO); this.getHibernateTemplate().save(student); }
- [SSH] Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction
- Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction
- Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition
- Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition
- Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definitio
- Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition
- Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition
- Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition
- 解决 Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from
- 解决 Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from
- 解决 Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from
- 解决 Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker
- Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly'
- 报错原因 (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker fr
- spring出现 Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marke问题
- Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly
- Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly
- Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly
- 一致性hash(Consistent Hashing)
- Neofetch带发行版 Logo 图像的系统信息显示工具发布啦!
- 组合24游戏程序
- Eclipse启动时,为什么需要寻找JVM(JRE)?
- 利用keytool/Openssl来生成服务端和客户端证书,根证书。
- [SSH] Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction
- shell 安装jdk
- JAXB入门
- overflow:hidden
- PL/SQL
- linux下创建用户
- leetcode 之 python篇1
- 海康相机的二次开发
- 5天时间让1000精准客源主动找上你