Monday 11 February 2008

Database giving me a headache

I lost quite some time today only by trying to figure out how to make inserts into two tables having the same ID. The problem was that I am using some kind of inheritance for users. I am basically treating  Members, Chatrooms and Groups as some kind of users as all of them share some attributes. So I have a lookup table which records the users ID it's type and some common attributes. As a result of this, it was necessary to also create an entry into the Lookup table with the same User ID. The problem now was that in Oracle SQL it's almost impossible to efficiently insert into two tables having the same Attributes, especially with Sequences, as I haven't found any possibility to read out the current or following value nor avoid it being overwritten/changed between two inserts. The only way would be to read out the just written row, but how? Here I would possibly have to add another key or something else to quickly find this row. Also reading out the highest value userID would be inefficient, as I might have to run through thousends of rows and compare them. And again, how to guarantee that another Thread would't do the same? The Server is split into two parts which only communicate through the database. At the end of the day I decided to solfe the problem by generating random UserIDs and check whether this userID already exists. This allow me to add assure that the generated ID does not exist and that the same ID won't be used by another thread/process at the same time as the possibility is extremely low that the same ID will be produced by the generator. The next problem occured with Java. Programming in this language for such a long time, by accident I had to find out today, that Integers can not be set to be unsigend which reduced my number of usable chars down to less than 16bits. In this case I no made a difficult decision and change the format to chars from 0-9, a-z, A-Z.  I hope that there won't be anymore such suprises waiting for me.

No comments: