[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

追查原因,查看切片事务配置如下:

[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

查看源码:

初始化阶段-Step1:解析到xml配置,在RuleBasedTransactionAttribute中设置query*开发的方法的readOnly值为true

[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

save等方法设置为fase:

[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

运行阶段-Step1:(切片事务运行的逻辑大概简单说下:就是切点方法执行前会打开一个connection放在TransactionSynchronizationManager的threadLocal变量中,切点方法中第一个dao执行前则会创建应该session同样放在TransactionSynchronizationManager的threadLocal变量中,【TransactionSynchronizationManager】是一个比较核心的类,值得投入时间好好翻一下的,这个不展开讲了)

获取上面设置的readonly值

[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

运行阶段-Step2:在切点方法执行前建立connection,此时根据配置设置连接只读:

[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

并把只读状态设置进TransactionSynchronizationManager的threadLocal变量中,如下:

[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

进而到>>

[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

进而到>>

[Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

运行阶段-Step3:具体在运行时,connection进行数据库操作,如果这个连接是只读,就会报开头的错误。

解决方案:

本问题所在的业务场景是:查询一个核心的表,但是需求是需要记录这一次查询操作,这个方法是以query*开头,切片事务配置是connection只读,所以报了上面的错误。

最终的解决方法是,修改了方法名称,改为:saveLogAfterQuery*, 这样本问题得到了解决。

以上。

上一篇:SessionId


下一篇:C#温故而知新系列 -- 闭包