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

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

<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" />
<pre name="code" class="html"><context:component-scan base-package="com.eq3z" />
  <!-- 配置事務管理器 -->
  <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
  </bean>
  <!-- 配置事務傳播特性 -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="save*" propagation="REQUIRED"/>
      <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="delete*" propagation="REQUIRED"/>
      <tx:method name="*" read-only="true"/>
    </tx:attributes>
  </tx:advice>
  <!-- 定義使用事務管理的方法 -->
  <aop:config>
    <aop:pointcut id="managerMethod" expression="execution(* com.service.*.*(..))"/>
    <aop:advisor  pointcut-ref="managerMethod" advice-ref="txAdvice"/>
  </aop:config>

解決辦法:修改刪除的時候方法 命名爲 updateXXX 就好了。 或者
<tx:method name="*" propagation="REQUIRED" read-only="true" />
把 read-only 這個去掉。

發佈了28 篇原創文章 · 獲贊 41 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章