开发者

Update n number of records using JPA with Optimistic locking

开发者 https://www.devze.com 2022-12-29 05:17 出处:网络
I have a table with a column called \"count\" and I want to run a cron job that will trigger a statement that performs a SQL like开发者_Python百科 this:

I have a table with a column called "count" and I want to run a cron job that will trigger a statement that performs a SQL like开发者_Python百科 this:

update summaryTable set count=4;

Note that there might be concurrent threads reading & changing the value for "count" when this cron job is triggered. The table has a version column to support Optimistic Locking.

What's an efficient way to update the count in JPA ?


Using JPA you can also perform updates using JPQL if you have an entity for that table. You can do this with the query.executeUpdate() function.

Something like: UPDATE SummaryTableEntity ent SET ent.count = 4 .

Since this is also tagged as EJB3 if your cron schedule is simple enough you could set up an EJB Timer which runs a method that performs the update. That way you are managing all your timed tasks in container and do not have to worry about external concurrency.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号