Update n number of records using JPA with Optimistic locking
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.
精彩评论