I am using a static librar开发者_JAVA技巧y; it has a function which uses the current time and creates a unique id, which is then inserted into my database. This number should be unique in my database table.
There are two processes running in parallel. Sometimes they simultaneously call this function, and the same number is generated. I get an integrity violation when this happens.
I am thinking to use the process id, the thread id, and the current time. Is this combination unique?
Platform: Windows XP
Use the database to generate them. How to do that depends on the database, but Postgres calls them sequences for an example.
The process/thread id will be unique if the programs are running simultaneously as the OS needs to differentiate them. But the system does reuse ids. So, for your situation, yes, its a good idea to add either process id or thread id into your marker, tho I don't think you'd need both.
On Windows, thread Ids are unique throughout the system. See this MSDN library article:
http://msdn.microsoft.com/en-us/library/ms686746%28v=VS.85%29.aspx
The CreateThread and CreateRemoteThread functions also return an identifier that uniquely identifies the thread throughout the system. A thread can use the GetCurrentThreadId function to get its own thread identifier. The identifiers are valid from the time the thread is created until the thread has been terminated. Note that no thread identifier will ever be 0.
The combination of process ID, thread ID and time is unfortunately not guaranteed to be unique. The OS may reuse process IDs and thread IDs once the threads and processes they referred to have terminated. Also, the user may set the clock back, so the same time occurs twice. As others have said, I'd ask the database for a unique ID. Oracle has sequences, MySQL has auto-increment columns, other databases have similar mechanisms.
Whilst the process id and thread id will be unique it would be better to use the database to generate the unique id for you (as R. Pate suggests) if only because you're potentially limiting your scalability unless you also include a unique machine id as well...
Though it's probably reasonably unlikely that one of your processes running on machine A will have the same process id and thread id as one of your processes running on machine B those are always the kinds of bugs that end up getting people out of bed at 4am to deal with the support call...
Well, adding process id and thread id could possibly lead to the same number
pid= 100, tid= 104 pid= 108, tid= 96
Not quite likely but possible.
So for near safe ids, you'll need at least a 64 bit ID field like
ULONG64 id = ((ULONG64)(pid&0xffff) << 48) | ((ULONG64)(tid&0xffff) << 32) | (timestamp & 0xffffffff);
(however, this still does not guarantee uniqueness as it assumes that thread ids don't overlap in a way with process ids that they neutralize 16-bit values, but I don't think I ever saw PIDs over 65536 and unless you are creating thousands of threads, the thread IDs will not wrap around in this value before the timestamp jumps).
精彩评论