Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于乐观锁插件的问题 #27

Closed
kevin82008 opened this issue Mar 30, 2017 · 11 comments
Closed

关于乐观锁插件的问题 #27

kevin82008 opened this issue Mar 30, 2017 · 11 comments

Comments

@kevin82008
Copy link

  1. 如果@Version注解是在父类上,是不管用的
  2. 如果原来的update语句里已经有 version=? 不会重置version的值(用插件默认生成的代码就有,因为要求有@Version注解,所以update的时候会有 version = )
  3. version版本号增加的时候,逻辑有问题, 0 变成 01 1变成11,(变成字符串相加了)
@yangyang0507
Copy link
Member

多谢反馈,我们会尽快调整!

@yuxiaobin
Copy link
Contributor

这个问题在git.oschina上dev分支已修复,将在下个版本解决

@kevin82008
Copy link
Author

2.0.6-jdk8 里面,在同一个会话里,多次调用同一个语句,还是会异常。
比如原始语句是 update xx set version = ? and xx = ? where id = ?
处理后的语句是 update xx set version = ? and xx = ? where version = ? and id = ?
第一次调用时,前面的version的值会+1,后面的version会添加一个参数;
第二次调用时,不会处理(因为prepare在同一个会话里根据sql有缓存,第二次调用直接取缓存,不会调用插件再处理)
应该还是需要在executor的地方拦截,prepare不行。

@yuxiaobin
Copy link
Contributor

非常感谢,已经着手查看该问题了

@kevin82008
Copy link
Author

提个思路吧,还是拦截StatementHandler,但是换成拦截getBoundSql这个方法,
处理sql的时候,不能添加额外的参数,版本号的值需要直接设置到sql语句里面去。
比如原来有 set version = ? 就改成 set version = ? + 1,没有,就改成 set version = version + 1
条件里如果原来有 version = ? 就不处理,没有就处理成 version = 具体的值(具体的根据version的类型在versionHandler里处理)
getBoundSql会在prepare之前处理,应该可以这样处理。

@kevin82008
Copy link
Author

顺便提一句,如果@Version注解是在父类上,最新版本还是不管用的

@qmdx
Copy link
Member

qmdx commented Apr 16, 2017

@kevin82008 抱歉! 请等一等我们的正式版 2.0.6

@qmdx qmdx closed this as completed Apr 16, 2017
@yuxiaobin
Copy link
Contributor

@kevin82008 Mybatis的plugin仅支持:
MyBatis allows you to intercept calls to at certain points within the execution of a mapped statement. By default, MyBatis allows plug-ins to intercept method calls of:

Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)

http://www.mybatis.org/mybatis-3/configuration.html#plugins

@kevin82008
Copy link
Author

我试过,可以拦截getBoundSql方法,不过官方声明上确实没有

@yuxiaobin
Copy link
Contributor

@kevin82008 这么神奇,不过不是很推荐,万一官方版本升级了,去掉了这个,那不就GG了

@huayanYu
Copy link
Member

@kevin82008 我试过拦截getBoundSql,确实可以. 但是普通模式就不能用了.
这个插件主要是我在负责,确实没什么好的思路了. 在Executor上拦截有很多问题的.
如果你要用重用模式不然手动复制一下自己拦截getBoundSql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants