`

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

阅读更多
报错: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">
    <tx:attributes>
        <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
        <tx:method name="add*" propagation="REQUIRED"  rollback-for="Exception"/>
        <tx:method name="create*" propagation="REQUIRED" rollback-for="Exception"/>
        <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
        <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
        <tx:method name="merge*" propagation="REQUIRED" rollback-for="Exception"/>
        <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
        <tx:method name="remove*" propagation="REQUIRED" rollback-for="Exception"/>
        <tx:method name="put*" propagation="REQUIRED" rollback-for="Exception"/>
        <tx:method name="use*" propagation="REQUIRED" rollback-for="Exception"/>
       
        <tx:method name="get*" propagation="REQUIRED" read-only="true"  rollback-for="Exception"/>
        <tx:method name="count*" propagation="REQUIRED" read-only="true"  rollback-for="Exception"/>
        <tx:method name="find*" propagation="REQUIRED" read-only="true"  rollback-for="Exception"/>
        <tx:method name="list*" propagation="REQUIRED" read-only="true"  rollback-for="Exception"/>        
        <tx:method name="*" read-only="true"  rollback-for="Exception"/>
    </tx:attributes>
</tx:advice>


    <aop:config expose-proxy="true">       
        <aop:pointcut id="txPointcut" expression="execution(* com.xxx..service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>




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

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

当然也可以把read-only="true"去掉,担不推荐这种做法
3
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics