This discussion is archived
3 Replies Latest reply: Oct 14, 2013 6:48 AM by greybird RSS

Concurrent sequence updates

501863e4-e8fa-4a1a-bbd6-0d2c730c3f67 Newbie
Currently Being Moderated

Hi

 

Im getting overwhelming amounts of LockTimeoutExceptions when trying to get sequence numbers concurrently. Why is this happening? Do I need to acquire a write lock on the sequence before incrementing it? However, there is no LockMode argument on the Sequence API?

 

        final ExecutorService executorService = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 1000; i++) {

            executorService.execute(        new Runnable(){

                @Override

                public void run() {

                    final Transaction tx = env.beginTransaction(null, null);

                    SequenceConfig c = new SequenceConfig();

                    c.setAllowCreate(true);

                    final byte[] key = new byte[] {1};

                    try (Sequence seq = db.openSequence(tx, new DatabaseEntry(key), c)) {

                        System.out.println(seq.get(tx, 1));

                    }

                    tx.commit();

                }

            });

        }

 

Cheers,

-Kristoffer

  • 1. Re: Concurrent sequence updates
    greybird Expert
    Currently Being Moderated

    Hi Kristoffer,

     

    You should not get true deadlocks when getting Sequence values.  However, lock timeouts are not always deadlocks.  Timeouts also occur when operations take longer than the configured lock timeout, which is 500ms.  Because you are opening the sequence (which reads the record from the database) every time you get a value, this is going to very slow and with many threads doing it concurrently you may simply be exceeding the timeout.

     

    I recommend that you open the Sequence once, rather than every time you get a value, and try your test again.  If you still get timeout exceptions, try increasing the lock timeout -- see EnvironmentConfig.LOCK_TIMEOUT.  If you continue to get timeout exceptions, no matter how large you make the timeout value, then a true deadlock may be occurring and we should investigate.  In that case, please follow the FAQ (link below) to get more information on the timeout and post the results here.

    Oracle Berkeley DB Java Edition FAQ

     

    I also noticed that you're never closing the Sequence.  The close() method doesn't happen to do anything today, but it's good practice to call it, in case something changes in the future.

     

    --mark

  • 2. Re: Concurrent sequence updates
    501863e4-e8fa-4a1a-bbd6-0d2c730c3f67 Newbie
    Currently Being Moderated

    I see, will try to tweak the timeout a bit and keep the sequence open and see how it plays out.

     

    There may indeed be a deadlock situation hidden here also, so I was thinking of omitting transactions when dealing with sequences. But is there a risk that the same number will be returned twice if no transaction is provided to the get/open operation, or any other risks with doing so?

  • 3. Re: Concurrent sequence updates
    greybird Expert
    Currently Being Moderated

    The use of transactions can cause deadlocks, but only when multiple operations are performed in a single transaction, and performed in a different order in different transactions.  That's not the issue here.

     

    --mark

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points