如何使用JOOQ执行或批量执行执行多个更新查询,而无需代码生成?

发布于 2025-01-28 13:18:03 字数 2277 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦 2025-02-04 13:18:03

您遇到的问题

最有可能遇到的问题的原因是,您已将行锁定在另一笔交易中(例如,在SQL编辑器中?),现在您的程序在更新中打印了调试消息后立即被阻止。

查询

关于您的两种方法的 ,您都不使用绑定变量,这将是Oracle的光标缓存的问题,从而产生光标缓存争议。至少,您应该执行类似的操作:

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

在批处理案例中,您已经使用JOOQ解析器获取字符串的QUERY表示您的字符串表示,但是就您而言,这似乎是过高的工作。您可以包裹任何query中使用 dslcontext.query(string)

同样,在这种情况下,最好使用绑定变量,例如这样:

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

甚至是:

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

关于代码生成

我建议您使用代码生成重新考虑。尽管在您的特定情况下没有太大的好处(至少您在这个问题中分享的部分),但通常非常有用。

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:

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. You can wrap any plain SQL string in a Query using DSLContext.query(String).

In that case, again, it would be better to use bind variables, e.g. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文