执行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" />

 

posted @ 2015-12-29 14:11  ordinary01  阅读(12324)  评论(0编辑  收藏  举报