如何使用JOOQ执行或批量执行执行多个更新查询,而无需代码生成?
我正在使用JOOQ(3.10。5)在没有JOOQ自动代码生成的Oracle表中更新记录
方法1-使用DSL执行DSL
dslContext.execute("update author set first_name = 'updateTest-111111' where id = 1 ");
logger.info("1st update Done ");
dslContext.execute("update author set first_name = 'updateTest-2222222' where id = 2 ");
logger.info("2nd update Done ");
方法2-使用DSL批处理通过传递查询列表,
List<Query> updateQueries = new ArrayList<>();
updateQueries.add(dslContext.parser().parseQuery("update author set first_name = 'updateTest-111' where id = 1 "));
updateQueries.add(dslContext.parser().parseQuery("update author set first_name = 'updateTest-222' where id = 2 "));
dslContext.batch(updateQueries).execute();
但在这两种情况下,它只是更新第一记录,然后停止执行,继续运行。
以下是进近-1 的输出,
2022-05-13 02:43:50.848 INFO 25524 --- [nio-9010-exec-1] org.jooq.Constants :
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@@@@@@ @@ @@ @@@@@@@@@@
@@@@@@@@@@ @@@@ @@ @@ @@@@@@@@@@
@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@ @@ @@ @@@@ @@@@@@@@@@
@@@@@@@@@@ @@ @@ @@@@ @@@@@@@@@@
@@@@@@@@@@ @@ @ @ @@@@@@@@@@
@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Thank you for using jOOQ 3.10.5
2022-05-13 02:43:50.922 WARN 25524 --- [nio-9010-exec-1] o.a.tomcat.jdbc.pool.ConnectionPool : minIdle is larger than maxActive, setting minIdle to: 5
2022-05-13 02:43:50.923 WARN 25524 --- [nio-9010-exec-1] o.a.tomcat.jdbc.pool.ConnectionPool : maxIdle is larger than maxActive, setting maxIdle to: 5
2022-05-13 02:43:52.670 INFO 25524 --- [nio-9010-exec-1] c.d.e.dao.ECRebootServiceDaoImpl : 1st update Done
因为完成了第1个更新后它停止了。
我应该如何使用JOOQ执行多个更新查询?还是没有代码生成的JOOQ中有更好的方法可以做到这一点?
I am using JOOQ ( 3.10. 5 ) to update records in ORACLE table without jooq auto code generation in below ways
Approach 1- Using DSL execute by using plain SQL String
dslContext.execute("update author set first_name = 'updateTest-111111' where id = 1 ");
logger.info("1st update Done ");
dslContext.execute("update author set first_name = 'updateTest-2222222' where id = 2 ");
logger.info("2nd update Done ");
Approach 2 - Using DSL batch by passing Query list
List<Query> updateQueries = new ArrayList<>();
updateQueries.add(dslContext.parser().parseQuery("update author set first_name = 'updateTest-111' where id = 1 "));
updateQueries.add(dslContext.parser().parseQuery("update author set first_name = 'updateTest-222' where id = 2 "));
dslContext.batch(updateQueries).execute();
But in both cases, it is just updating 1st record and then stop execution , keeps on running.
Below is the output for Approach -1
2022-05-13 02:43:50.848 INFO 25524 --- [nio-9010-exec-1] org.jooq.Constants :
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@@@@@@ @@ @@ @@@@@@@@@@
@@@@@@@@@@ @@@@ @@ @@ @@@@@@@@@@
@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@ @@ @@ @@@@ @@@@@@@@@@
@@@@@@@@@@ @@ @@ @@@@ @@@@@@@@@@
@@@@@@@@@@ @@ @ @ @@@@@@@@@@
@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Thank you for using jOOQ 3.10.5
2022-05-13 02:43:50.922 WARN 25524 --- [nio-9010-exec-1] o.a.tomcat.jdbc.pool.ConnectionPool : minIdle is larger than maxActive, setting minIdle to: 5
2022-05-13 02:43:50.923 WARN 25524 --- [nio-9010-exec-1] o.a.tomcat.jdbc.pool.ConnectionPool : maxIdle is larger than maxActive, setting maxIdle to: 5
2022-05-13 02:43:52.670 INFO 25524 --- [nio-9010-exec-1] c.d.e.dao.ECRebootServiceDaoImpl : 1st update Done
As you can see it stopped after 1st update Done.
How i should be executing multiple update queries using JOOQ ? or is there any better way to do this in JOOQ without code generation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的问题
最有可能遇到的问题的原因是,您已将行锁定在另一笔交易中(例如,在SQL编辑器中?),现在您的程序在更新中打印了调试消息后立即被阻止。
查询
关于您的两种方法的 ,您都不使用绑定变量,这将是Oracle的光标缓存的问题,从而产生光标缓存争议。至少,您应该执行类似的操作:
在批处理案例中,您已经使用JOOQ解析器获取字符串的
QUERY
表示您的字符串表示,但是就您而言,这似乎是过高的工作。您可以包裹任何在query
中使用dslcontext.query(string)
。同样,在这种情况下,最好使用绑定变量,例如这样:
甚至是:
关于代码生成
我建议您使用代码生成重新考虑。尽管在您的特定情况下没有太大的好处(至少您在这个问题中分享的部分),但通常非常有用。
The problem you encountered
The most likely reason for the problem you've encountered is that you have locked the row in another transaction (e.g. in a SQL editor?) and now your program is blocked right after printing the debug message, in the update.
Regarding your queries
With both of your approaches, you're not using bind variables, which will be a problem for Oracle's cursor cache, producing cursor cache contention. At the least, you should execute something like this:
In the batch case, you have used the jOOQ parser to get a
Query
representation of your string, but in your case, that seems to be overkill. You can wrap any plain SQL string in aQuery
usingDSLContext.query(String)
.In that case, again, it would be better to use bind variables, e.g. like this:
Or even just:
Regarding code generation
I suggest you reconsider using code generation. While there isn't much benefit in your particular case (at least the parts you've shared in this question), it is very useful in general.