English 中文(简体)
一个人应该如何处理javax.persistence.OptimisticLockException?
原标题:How should one handle a javax.persistence.OptimisticLockException?

我对JPA很新,如果表达不清楚请原谅。

Basically I want to prevent concurrent modifications by using Optimistic Locking. I ve added the @Version attribute to my entity class.

I need to know if this algorithm on handling OptimisticLockException is sound. I m going to use the Execute Around Idiom like so:

interface UpdateUnitOfWork 
{
    doUpdate( User user ); /* may throw javax.persistence.PersistenceException */
}

public boolean exec( EntityManager em, String userid, UpdateUnitOfWork work)
{
    User u = em.find( User, userid );
    if( u == null ) 
        return;

    try
    {
        work.doUpdate( u );
        return true;
    }
    catch( OptimisticLockException ole )
    {
        return false;
    }
}

public static void main(..) throws Exception
{
    EntityManagerFactory emf = ...;
    EntityManager em = null;

    try
    {
        em = emf.createEntityManager();

        UpdateUnitOfWork uow = new UpdateUnitOfWork() {
            public doUpdate( User user )
            {
                user.setAge( 34 );
            }
        };

        boolean success = exec( em, "petit", uow );
        if( success )
            return;

        // retry 2nd time
        success = exec( em, "petit", uow );
        if( success )
            return;

        // retry 3rd time
        success = exec( em, "petit", uow );
        if( success )
            return;
    }
    finally
    {
        em.close();
    }
}

我有一个问题,你如何决定何时停止重试?

最佳回答

我所提出的问题是:您如何决定何时停止重试?

在我看来,当在同一时间修改同一对象是个例外情况时,乐观锁应该被使用。

现在,如果出现这种情况,且如果处理是手动的,我会警告用户修改不能保存并要求他重新保存更改。

如果该过程是自动化的,实施自动重试机制可能是有意义的,但我不会多次重试超过3或5次,这取决于处理时间(我会使用递归调用来实现此操作)。如果自动化进程在并发访问问题上连续失败5次,那么很可能与另一个自动化进程竞争,并且它们要么不在独立的数据块上工作(这对于并行化是不好的),要么策略不正确。在两种情况下,更多重试不是正确的解决方案。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签