• 执行update操作的话,就会报“Connection is read-only. Queries leading to data modification are not allowed”的异常。


    我用的是 spring + springmvc + mybatis +mysql、

    <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="modify*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />
                <tx:method name="remove*" propagation="REQUIRED" />
                <tx:method name="select*" read-only="true" /> 
                <tx:method name="query*" read-only="true" />
                <tx:method name="get*" read-only="true" />
            </tx:attributes>
        </tx:advice>

     让所有的方法都加入事务管理,为了提高效率,可以把一些查询之类的方法设置为只读的事务

    <!-- method name=*, readonly=true表示所有的数据库操作都可以使用,但是只能是读取数据库

    但是如果是UserService的方法delUser, 要在dao层删除用户。就会报错误如下:

    Connection is read-only. Queries leading to data modification are not allowed。 

    出现这个问题的原因是默认事务只有只读权限,因此要添加下面的每一个add*,del*,update*等等。 分别给予访问数据库的权限。

    还有就是在service层中在只有只读权限的方法中调用操作数据库的方法也会报错Connection is read-only. Queries leading to data modification are not allowed。 

    例如  

    1 public String getXX(){
    2            server.updateXX(obj);
    3 }

     例如在get方法中调用了update方法但是get只有只读权限,所以需要修改方法名或者将配置文件修改为

        <tx:method name="get*" read-only="false" />
  • 相关阅读:
    P3373 【模板】线段树 2
    P3372 【模板】线段树 1
    P3368 【模板】树状数组 2
    P3374 【模板】树状数组 1
    P1004 方格取数
    P1880 [NOI1995]石子合并
    UOJ#152盘子序列
    P1886 滑动窗口
    P1440 求m区间内的最小值
    二进制中1的个数
  • 原文地址:https://www.cnblogs.com/dashuai01/p/5085560.html
Copyright © 2020-2023  润新知