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"去掉,担不推荐这种做法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值