How to use JPA lock?
<p>Yes, I know maybe the question is very wide, but I don't understand how the JPA lock works.</p>

<p>I will put an example about how I'm using it:</p>

<pre><code>public Account deposit(int id, double quantity) {
    Account a = super.find(id);
    if (a == null) {
        return null;
    }
    try {
        em.lock(a, LockModeType.PESSIMISTIC_WRITE);
        a.setSaldo(a.getSaldo() + quantity);

        LOG.log(Level.INFO, " //\u00a0 Se ha depositado, saldo :{0}", a.getSaldo());
    } catch (Exception oe) {
        LOG.log(Level.INFO, oe.getMessage());
    }
    return a;
}
</code></pre>

<p>And you are thinking "so?"</p>

<p>Well, when I apply the lock  the next statement will update the record without a "merge" instruction, but when the lock is finished? , this means I can continue making changes  (ie a.setFOO(bar)) until the return statement is reached and will commit the changes? or do I need to make another lock? </p>

<p>AFAIK the pessimistic write lock will make a SELECT... FOR UPDATE , but since I don't have a "transaction.begin()" I have no idea about the scope of the lock. I'm injecting the Entity manager via </p>

<pre><code>@PersistenceContext(unitName = "PruebaConcurrenciaPU")
private EntityManager em; 
</code></pre>

<p>thanks!</p>