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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM