commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jörg Schaible <Joerg.Schai...@Elsag-Solutions.com>
Subject [id] Fooled by SessionIdGenerator
Date Fri, 12 Nov 2004 15:24:40 GMT
Hi folks,

in my current application (a kind of plugin - the plugin is just called from time to time)
I was fooled by the SessionIdGenerator. I chose this one instead of a UUID generator because
of a length limitation of my unique keys and since I had a lengthly discussion with Phil about
the reliable uniqueness of the keys. Now, the length limitation was introduced later in the
project and I just switched from a VersionFourGenerator to SessionIdGenerator. Unfortunately
we had to detect in the current test phase, that it happened sometimes, that obviously two
identifiers had the same value.

I tracked it down to a unit test:

    public final void testIdentityGenerator() {
        final Set set = new HashSet();
        for(int i = 0; i < 10000; ++i) {
            final IdentifierGenerator idGenerator = new SessionIdGenerator();
            set.add(idGenerator.nextIdentifier().toString());
        }
        assertEquals(10000, set.size());
    }

this works for the VersionFourGenerator without problem. Looking at the source everything
got clear: The SessionIdGenerator expects to be *himself* the singleton, while the VersionFourGenerator
uses a singleton internally. To make this work, a simple change is necessary: Make the internally
used Random object a static. Otherwise there's always a good chance, that two Random objects
get initialized in the same millisecond fraction.

Regards,
Jörg

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message