简体   繁体   English

如何在不生成代码的情况下使用 jooq 执行或批处理执行执行多个更新查询?

[英]How to execute multiple update queries using jooq execute or batch execute without code generation?

I am using JOOQ ( 3.10. 5 ) to update records in ORACLE table without jooq auto code generation in below ways我正在使用 JOOQ ( 3.10. 5 ) 来更新 ORACLE 表中的记录,而无需通过以下方式生成 jooq 自动代码

Approach 1- Using DSL execute by using plain SQL String方法 1- 使用普通 SQL 字符串使用 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 ");

Approach 2 - Using DSL batch by passing Query list方法 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();

But in both cases, it is just updating 1st record and then stop execution , keeps on running.但在这两种情况下,它只是更新第一条记录,然后停止执行,继续运行。

Below is the output for Approach -1下面是方法 -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 ?我应该如何使用 JOOQ 执行多个更新查询? or is there any better way to do this in JOOQ without code generation?或者有没有更好的方法在没有代码生成的情况下在 JOOQ 中做到这一点?

The problem you encountered你遇到的问题

The most likely reason for the problem you've encountered is that you have locked the row in another transaction (eg in a SQL editor?) and now your program is blocked right after printing the debug message, in the update.您遇到问题的最可能原因是您已将行锁定在另一个事务中(例如在 SQL 编辑器中?),现在您的程序在打印调试消息后立即被阻止,在更新中。

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.使用这两种方法,您都没有使用绑定变量,这对 Oracle 的游标缓存来说将是一个问题,从而产生游标缓存争用。 At the least, you should execute something like this:至少,您应该执行以下操作:

ctx.execute("update author set first_name = ? where id = ?", "updateTest-111111", 1);
ctx.execute("update author set first_name = ? where id = ?", "updateTest-2222222", 2);

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.在批处理情况下,您使用了 jOOQ 解析器来获取字符串的Query表示,但在您的情况下,这似乎是矫枉过正。 You can wrap any plain SQL string in a Query using DSLContext.query(String) .您可以使用DSLContext.query(String)将任何纯 SQL 字符串包装在Query中。

In that case, again, it would be better to use bind variables, eg like this:在这种情况下,再次使用绑定变量会更好,例如:

ctx.batch(query("update author set first_name = ? where id = ?"))
   .bind("updateTest-111", 1)
   .bind("updateTest-222", 2)
   .execute();

Or even just:甚至只是:

ctx.batch("update author set first_name = ? where id = ?")
   .bind("updateTest-111", 1)
   .bind("updateTest-222", 2)
   .execute();

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.虽然在您的特定情况下没有太多好处(至少您在这个问题中分享的部分),但总的来说它非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM