From: Mark Martin <[EMAIL PROTECTED]> > I'm trying to maintain a unique key in an Oracle Table while working on the > table using DBI. the unique key is just a no. read in from a txt file and > incremented for each record - supposedly. In fact I'm getting unique > constraint violation. Can anybody tell me what is wrong with this code,(the > statement handler inserts into the table) > cheers, > Mark > > if (condition){ > open (IN,$uniquefile) or die "Cannot Open $uniquefile \n"; > $uniquemarker=<IN>; > close(IN); > $uniquemarker++; > > $sth1->execute($uniquemarker,$var1,$var2) or die "Can't execute SQL > statement: $DBI::errstr\n"; > } > > open (OUT,">$uniquefile"); > print OUT "$uniquemarker"; > > $dbh1->disconnect(); > exit;
What if two instances of this code run at the same time? Before the first writes the incremented uniqueidentifier into the file, hte second may have already read the old value! While you could implement this correctly using locking it's better to leave this to the database. You might either keep the last unique id in a helper table (Do the locking correctly!) or use Sequences. The second solution is much better, please look up "sequence" in Oracle docs. (Sorry, can't give you exact syntax. Don't have Oracle by hand and it's about 2 years since I used it last time.) Jenda =========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ========== There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain. I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]