我用的是 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" />
