mybatis诡异问题,执行某个mapper文件的增删改sql语句都会报错。如何解决?

weixin_38974328 2019-06-14 05:58:49


ssm项目,数据库是postgresSql 。

原本准备为某个实体类型,新增一个操作功能,在对应的server类中写好方法,dao中写好接口,然后写上对应的mapper,也就是UserMapper。然后在junit进行测试,运行后,控制台先打印sql语句,然后就没有了响应就结束了。我第一反应可能是sql语句写的有点问题,我直接把控制台里打印的sql语句复制到navicat上运行,能运行,有效果,没有异常……,于是在那个测试的junit上使用trycatch语句,看看有什么异常信息,运行之后发现异常出现了,调到最上面,看到如下信息
### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: cannot execute UPDATE in a read-only transaction
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: insert语句或delete语句或uddate语句
### Cause: org.postgresql.util.PSQLException: ERROR: cannot execute UPDATE in a read-only transaction

The error occurred while setting parameters, The error may involve defaultParameterMap这两个我认为应该没有关系,因为控制台打印的sql语句是没问题的。主要还是read-only transaction?这个好像跟数据库的权限配置有关联。但是我记得我没设置过我的数据库的只读相关的权限配置。在UserMapper文件中,我再写一个无参的mapper接口,试着换成一个简单的操作语句试了一下,也会出现如上错误
<update id="testUpdate">
update user set name='sad' where id=5555


</update>
或者
<delete id="testUpdate">
delete fom user where id =123456
</delete>
UserMapper文件里,执行select查询操作mybatis能正常执行,有返回数据,如果执行增删改(insert,delete,update)操作会出该异常错误,但是用tk.mapper执行对该实体(User)的增删改操作也没有报错……

然后用如上操作,又去测试了其他mapper一个文件(另一个实体的dao),没有如上的错误……,然后我又把UserMapper里的sql语句和相应的抽象方法放到其他的mapper文件以及对应mapper接口文件内容中,然后用junit测试执行,没有出错,能正常该mapper文件里的sql语句……。

总之这个错误异常出现的规律是。只要执行UserMapper文件的增删改操作相关sql语句的才会出现的错误,如果把UserMapper里的操作语句和相应抽象方法放到其他mapper文件和相应接口文件里,然后执行从UserMapper文件里放进其他mapper文件里的sql语句,不会报错,能正常执行。呵呵呵,这什么鬼啊,好诡异,有人碰见过该问题的程序员吗?请问知道这种情况是怎么回事吗?怎么解决,或者有什么思路?


以下是某junit测试用例打印的异常的全部信息

### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: cannot execute UPDATE in a read-only transaction
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: update sys_user set name='sad' where id=5555
### Cause: org.postgresql.util.PSQLException: ERROR: cannot execute UPDATE in a read-only transaction
; uncategorized SQLException for SQL []; SQL state [25006]; error code [0]; ERROR: cannot execute UPDATE in a read-only transaction; nested exception is org.postgresql.util.PSQLException: ERROR: cannot execute UPDATE in a read-only transaction
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy28.update(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:294)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy42.testUpdate(Unknown Source)
at com.xxx.server.UserServer.testUpdate(UserServer.java:37)
at com.xxx.server.UserServer$$FastClassBySpringCGLIB$$1c6ce357.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at com.xxx.server.UserServer$$EnhancerBySpringCGLIB$$bcc3d122.testUpdate(<generated>)
at com.test.modules.UserTest.testUpdate(UserTest.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:39)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source)
at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
at java.util.Iterator.forEachRemaining(Unknown Source)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.util.stream.ReferencePipeline.forEach(Unknown Source)
at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:79)
at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:70)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.postgresql.util.PSQLException: ERROR: cannot execute UPDATE in a read-only transaction
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161)
at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:155)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
at com.sun.proxy.$Proxy51.execute(Unknown Source)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
... 60 more


...全文
1097 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_45013817 2020-07-10
  • 打赏
  • 举报
回复
我遇到的是这种情况: 是因为在 service 方法里面有调用feign接口 由于调用feign接口超时 导致整个事务超时;
freekaiQaQ 2019-06-17
  • 打赏
  • 举报
回复
配置文件中对事务的配置是不是只读了? 你尝试着在 报错的方法调用上加上 @Transactional(readOnly = false) 注解看看,
睡觉唱国歌 2019-06-15
  • 打赏
  • 举报
回复
ERROR: cannot execute UPDATE in a read-only transaction。楼上说对的
咸哼酒家 2019-06-15
  • 打赏
  • 举报
回复
只读模式,不能新增修改只能读取
参考

set session default_transaction_read_only=off
weixin_38974328 2019-06-15
  • 打赏
  • 举报
回复
引用 2 楼 咸哼酒家 的回复:
只读模式,不能新增修改只能读取
参考

set session default_transaction_read_only=off


不可能是只读问题吧,这里的情况是,UserMapper里的增删改sql语句放到Navicat的都能执行,放到其他mapper文件mybatis也能执行,从该实体server类中使用tk.mapper增删改也能执行。唯独从UserMapper执行就会报错

也试过改mapper文件名称,接口名称路径没变的情况下,还是有这个现象
立刀流 2019-06-14
  • 打赏
  • 举报
回复
Usermapper改个名儿看看,,,

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧