Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

portable sequence

Thread view: 
Drazen Gemic - 24 Nov 2007 15:36 GMT
Hi !

I need to have a portable global sequence generator that works
regardless of database engine used. For example, Postgres have
sequence, mssql doesn't, but I need sequence for both. I tried to
create one, uzing SERIALIZABLE transactions, but under the increased
load it started to generate deadlocks.

The sequence table, 'uniseq' in this example, contains just one row
with one column ('next_id'), and that column is a sequence value, a
globaly unique number (BIGINT).

If the table is empty, new row is inserted, with sequence number = 1.
Otherwise sequence number is increased and stored.

What should I do to avoid deadlocks ?

Here is the code:

public static Long getUniqId(AdbWrapper dbx)
  {
  boolean flag=true;
  dbx.startTran("SERIALIZABLE"); // transaction start
  Long num=(Long) dbx.getField("select next_id from uniseq");
  if(num == null)
     {
     num=new Long(1);
     flag=(dbx.update("insert into uniseq values (1)") > 0);
     }
  else
     {
     num=new Long(1+num.longValue());
     flag=(dbx.update("update uniseq set next_id="+num) > 0);
     }
  dbx.endTran(flag);   // transaction end
  //
  return num;
  }
Arne Vajhøj - 24 Nov 2007 15:49 GMT
> I need to have a portable global sequence generator that works
> regardless of database engine used. For example, Postgres have
[quoted text clipped - 32 lines]
>    return num;
>    }

With a high transaction isolation level you need to be prepared to
retry in case of deadlocks/timeouts.

If you switch to Scott Ambler high-low approach, then:
- performance will be much better
- deadlocks/timeouts will me rare
(but you can get holes in your sequence though)

Arne
Lew - 24 Nov 2007 16:21 GMT
>> I need to have a portable global sequence generator that works
>> regardless of database engine used. For example, Postgres have
[quoted text clipped - 40 lines]
> - deadlocks/timeouts will me rare
> (but you can get holes in your sequence though)

I prefer to use the built-in sequence mechanism, and just translate to the
appropriate dialect if I need to port to a new RDBMS.

Signature

Lew

Drazen Gemic - 24 Nov 2007 18:45 GMT
> >> I need to have a portable global sequence generator that works
> >> regardless of database engine used. For example, Postgres have
[quoted text clipped - 43 lines]
> I prefer to use the built-in sequence mechanism, and just translate to the
> appropriate dialect if I need to port to a new RDBMS.

If there is one. MS SQL 2000 does not have it.

I am thinking about asking my clients to standardize on Postgres.
Majority of them don't have true DBA anyway, and there are problems
because
of that

DG
Lew - 24 Nov 2007 19:52 GMT
> I am thinking about asking my clients to standardize on Postgres.
> Majority of them don't have true DBA anyway, and there are problems
> because of that

Those problems will not go away by using Postgres.

You can't cure lack of a DBA by switching DBMSes.

Signature

Lew

Drazen Gemic - 24 Nov 2007 20:23 GMT
> > I am thinking about asking my clients to standardize on Postgres.
> > Majority of them don't have true DBA anyway, and there are problems
[quoted text clipped - 3 lines]
>
> You can't cure lack of a DBA by switching DBMSes.

Yes, but I can support Postgres.

DG
Lew - 24 Nov 2007 21:14 GMT
Lew wrote:
>> You can't cure lack of a DBA by switching DBMSes.

> Yes, but I can support Postgres.

A key fact.  In other words, you have a PG DBA.  That makes all the difference.

PG is a great product.

Signature

Lew

Drazen Gemic - 25 Nov 2007 22:36 GMT
> Lew wrote:
> >> You can't cure lack of a DBA by switching DBMSes.
> > Yes, but I can support Postgres.
>
> A key fact.  In other words, you have a PG DBA.  That makes all the difference.

Exactly.

> PG is a great product.

Exactly, again.

DG
Arne Vajhøj - 25 Nov 2007 18:22 GMT
>> With a high transaction isolation level you need to be prepared to
>> retry in case of deadlocks/timeouts.
[quoted text clipped - 6 lines]
> I prefer to use the built-in sequence mechanism, and just translate to
> the appropriate dialect if I need to port to a new RDBMS.

You will be more portable and get better performance by going the
other route.

You will also need to write some code and have to live with holes
in the sequence.

Arne
Drazen Gemic - 25 Nov 2007 22:40 GMT
> You will be more portable and get better performance by going the
> other route.
>
> You will also need to write some code and have to live with holes
> in the sequence.

I haven't been able to find an article or example you were referring
to.

What do you mean by holes ? Do you mean increment greater than 1 or
possibility
to get duplicate ID ?

DG
Arne Vajhøj - 25 Nov 2007 23:00 GMT
> I haven't been able to find an article or example you were referring
> to.

http://fox.wikis.com/wc.dll?Wiki~HighLowKeyGeneration~SoftwareEng
http://www.theserverside.com/patterns/thread.tss?thread_id=4228
http://www.agiledata.org/essays/keys.html
etc.

> What do you mean by holes ? Do you mean increment greater than 1 or
> possibility
> to get duplicate ID ?

Increments larger than 1.

Arne
Drazen Gemic - 26 Nov 2007 17:48 GMT
I've taken a look, it is elegant, but I don't think I am going to
use it, because there is something more  simple. I know how many
keys I need per session, so I coud request a continuous block of 10 or
20 keys
in single operation.

That will significantly decrease the load, and I can still check for
deadlocks.

ID is used to match response to requests sent to another, remote
application. Looking at the logs I suspect that load is generated
when impatient users repeatedly click while waiting for the response
from remote host, so I am going to add Thread.sleep() call to the
operation.

DG
Lew - 25 Nov 2007 23:13 GMT
Arne Vajhøj wrote:
>> With a high transaction isolation level you need to be prepared to
>> retry in case of deadlocks/timeouts.
[quoted text clipped - 3 lines]
>> - deadlocks/timeouts will me rare
>> (but you can get holes in your sequence though)

> I haven't been able to find an article or example you were referring
> to.

Ten minutes or so of searching for "Scott Ambler database sequence" and
following links through Mr. Ambler's site found me
<http://www.agiledata.org/essays/keys.html#SurrogateKeyImplementation>
along with a ton of other useful information.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.