acquire a database lock to prevent race conditions in duplicate checking
[freeside.git] / FS / FS / svc_acct.pm
index f66c715..ded796f 100644 (file)
@@ -245,7 +245,14 @@ sub insert {
     $self->svcpart($cust_svc->svcpart);
   }
 
-  #new duplicate username checking
+  #new duplicate username/username@domain/uid checking
+
+  #this is Pg-specific.  what to do for mysql etc?
+  # ( mysql LOCK TABLES certainly isn't equivalent or useful here :/ )
+  warn "$me locking svc_acct table for duplicate search" if $DEBUG;
+  dbh->do("LOCK TABLE svc_acct IN SHARE ROW EXCLUSIVE MODE")
+    or die dbh->errstr;
+  warn "$me acquired svc_acct table lock for duplicate search" if $DEBUG;
 
   my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
   unless ( $part_svc ) {
@@ -272,8 +279,7 @@ sub insert {
     foreach my $part_export ( $part_svc->part_export ) {
 
       #this will catch to the same exact export
-      my @svcparts = map { $_->svcpart }
-        qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
+      my @svcparts = map { $_->svcpart } $part_export->export_svc;
 
       #this will catch to exports w/same exporthost+type ???
       #my @other_part_export = qsearch('part_export', {