persistence@glassfish.java.net

Re: Database Lock

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Wed, 06 Jan 2010 16:37:13 -0800

Hi,

I don't understand what you are trying to achieve here. as you find Role
with id 1 and then updates the whole table using bulk update without a
where clause. Assuming you want to ensure that role with id 1 is locked,
you should do following
             role = em.find(Role.class, "1");
             em.lock(role, LockModeType.WRITE);
             em.flush() --> At this point it is locked for him a
parallel transaction can not modify it.
             ....

Thanks,
Mitesh

On 12/31/2009 1:50 AM, mthalis wrote:
> I want to handle concurrency in order to do it I use optimistic locking, I'm
> new to locking below show my code. Is this code enough for handle
> concurrency or is this code write?
>
>
> @TransactionAttribute(TransactionAttributeType.REQUIRED)
> public void udateRole() {
> Role role;
> try {
> role = em.find(Role.class, "1");
> em.lock(role, LockModeType.WRITE);
> Query query = em.createQuery("UPDATE Role s SET s.roleName =
> :roleName");
> query.setParameter("roleName", "6");
> query.executeUpdate();
> } catch (Exception e) {
> System.out.println(e);
> }
> }
>
> please give you're precious comments.
>