Out of the box ADF BC provides at least two options for assigning sequence values to ID fields:
DBSequence assigns temporary unique values to newly created entities and fetches the actual ID (assigned by a database trigger) by using
returning clause.
This works quite OK, but I had situations, where DBSequence misbehaved, specially in master-detail scenarios where you create parent and child record(s) in one transaction. Also, working with integers is simpler.
Using groovy expression like the one below gets rid of DBSequence, but introduces an extra round-trip at the time when a new row is created, which might not be the best idea performance-wise.
Using In-memory singleton sequencer
As an alternative, we can use an In-memory singleton sequencer initialized from DB sequence used across entities.
First, we need to define a
database sequence. Since we are not querying it for each and every value, we increment it in reasonable steps, let's say 100.
Next, we create a
singleton for serving sequences to our entities
And a
custom base EntityImpl exposing singleton to entity.
Don't forget to configure Business components to use custom base class in project properties!
Usage is very simple, just add the groovy expression below as attribute's Default value expression.
Some thoughts about this method
First of all, the singleton might not really be singleton. If you package the same class in multiple EARs, you will get more than one instance. Also, running on multiple servers will result in multiple instances. The instances can also die or are garbage collected before they serve all the sequences they reserve.
But none of that should be a problem. Remember, you can get gaps in sequences even in the database anyway, so you should not count on them being sequential. The singleton will server unique numbers regardless how many instances there actually are.
Download sample code
SequencerSampleApp
Add a comment