<tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 让所有的方法都加入事务管理,为了提高效率,可以把一些查询之类的方法设置为只读的事务 --> <!-- method name=*, readonly=true表示所有的数据库操作都可以使用,但是只能是读取数据库。 例如有UserService的方法 listUsers, 获取所有用户,就没问题。 但是如果是UserService的方法delUser, 要在dao层删除用户。就会报错误如下: Connection is read-only. Queries leading to data modification are not allowed。 因此要添加下面的每一个add*,del*,update*等等。 分别给予访问数据库的权限。 --> <tx:method name="*" propagation="REQUIRED" read-only="true" /> <!-- 以下方法都是可能设计修改的方法,就无法设置为只读 --> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="clear*" propagation="REQUIRED" /> </tx:attributes> </tx:advice>
在上面的beans.xml配置中, 一开始没有配置add*,del*,update*等等。 就会在调用相应的方法时出错。
而获取数据则没有问题, 是因为有
<tx:method name="*" propagation="REQUIRED" read-only="true" />