Sequence Block Pattern improvement

Discussions

EJB programming & troubleshooting: Sequence Block Pattern improvement

  1. Sequence Block Pattern improvement (1 messages)


    The Sequence Block pattern requires (in the most optimistic case) that the application server perform verified updates where it checks database contents before it commits. Do all app servers do this?

    There's a way to not require this, I think. Since most databases will happily lock a row when it has been updated so that concurrent transactions block on access to the row, simply code the bean such that it performs as its first operation the "update". Below is some pseudo code:


    update seed set seed=seed+1 where name='a';
    if error: // table doesn't exist
    create table seed (name varchar(255) primary key, seed int not null);
    if error: // table concurrently created?
    ignore and continue
    insert into seed values ('a', 1);
    if error: // row concurrently created?
    restart from beginning
    return 1;

    else if empty result: // zero rows affected
    insert into seed values ('a', 1);
    if error: // row concurrently created?
    restart from beginning
    return 1;

    else:
    select seed from seed where name='a';
    return seed;


    As such, one doesn't even need an EB and a SB; a SLSB can nicely and completely do all this. All this needs is a database that does row locking on update. Practically all databases worthy of consideration do this.

    What do you think?
  2. Oops, indentations disappeared. It should be apparently what the block structure is though.