ITeye博客

spring+hibernate中Connection is read-only问题的产生原因与解决方法

oufua · 2014-09-14 12:40 · 行业应用 · Markdown

报错:org.hibernate.exception.GenericJDBCException: Connection is read-only. Queries leading to data modification are not allowed 

原因: 

spring-config.xml文件 <tx:advice id="txAdvice" transaction-manager="txManager">  

  1. <tx:attributes>
  2. <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
  3. <tx:method name="add*" propagation="REQUIRED"  rollback-for="Exception"/>
  4. <tx:method name="create*" propagation="REQUIRED" rollback-for="Exception"/>
  5. <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
  6. <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
  7. <tx:method name="merge*" propagation="REQUIRED" rollback-for="Exception"/>
  8. <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
  9. <tx:method name="remove*" propagation="REQUIRED" rollback-for="Exception"/>
  10. <tx:method name="put*" propagation="REQUIRED" rollback-for="Exception"/>
  11. <tx:method name="use*" propagation="REQUIRED" rollback-for="Exception"/>
  12. <tx:method name="get*" propagation="REQUIRED" read-only="true"  rollback-for="Exception"/>
  13. <tx:method name="count*" propagation="REQUIRED" read-only="true"  rollback-for="Exception"/>
  14. <tx:method name="find*" propagation="REQUIRED" read-only="true"  rollback-for="Exception"/>
  15. <tx:method name="list*" propagation="REQUIRED" read-only="true"  rollback-for="Exception"/>
  16. <tx:method name="*" read-only="true"  rollback-for="Exception"/>
  17. </tx:attributes>
  18. </tx:advice>
  19. <aop:config expose-proxy="true">
  20. <aop:pointcut id="txPointcut" expression="execution(* com.xxx..service..*.*(..))" />
  21. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
  22. </aop:config>

service里面有参与事务的方法没有按以上的规则来命名,而以上配置文件规定了数据库操作函数必须要以上面的字符串开头,否则的话就按照默认的配置,对数据库访问的权限为read-only。而我的service方法里面有对数据进行update的操作,所以就报了上面的错。 

解决办法:修改service里面方法的名字,改为符合配置文件的规则。 

当然也可以把read-only="true"去掉,担不推荐这种做法

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。