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

末蓝、 2021-06-24 14:34 1526阅读 0赞
  1. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  2. <tx:attributes>
  3. <tx:method name="*" propagation="REQUIRED" read-only="true" />
  4. </tx:attributes>
  5. </tx:advice>

如上面配置,使用了spring的声明式事务管理数据库的事务,让所有的方法都加入事务管理,为了提高效率,可以把一些查询之类的方法设置为只读的事务。

例如有UserService的方法 listUsers, 获取所有用户,就没问题。但是如果是UserService的方法delUser, 要在dao层删除用户。就会报错误如下:
Connection is read-only. Queries leading to data modification are not allowed。

因此要添加下面的每一个add*,del*,update*等等。 分别给予访问数据库的权限。

  1. <tx:method name="add*" propagation="REQUIRED" />
  2. <tx:method name="del*" propagation="REQUIRED" />
  3. <tx:method name="update*" propagation="REQUIRED" />
  4. <tx:method name="save*" propagation="REQUIRED" />
  5. <tx:method name="create*" propagation="REQUIRED" />
  6. <tx:method name="clear*" propagation="REQUIRED" />
  7. <tx:method name="*" propagation="REQUIRED" read-only="true" />

这种配置,对于增删改操作是非只读事务,但是对于查询等方法或者不需要事务的方法(*),设置成只读的事务。

发表评论

表情:
评论列表 (有 0 条评论,1526人围观)

还没有评论,来说两句吧...

相关阅读