+  # Lock both the table where the new number is stored and the range
+  # table. The storage table has to be locked first in order to
+  # prevent deadlocks as the legacy code in SL/TransNumber.pm locks it
+  # first, too.
+
+  # For the storage table we have to use a full lock in order to
+  # prevent insertion of new entries while this routine is still
+  # working. For the range table we only need a row-level lock,
+  # therefore we're re-loading the row.
+  $self->db->dbh->do("LOCK " . $self->meta->table) || die $self->db->dbh->errstr;
+
+  my ($query_in_use, $bind_vals_in_use) = Rose::DB::Object::QueryBuilder::build_select(
+    dbh                  => $self->db->dbh,
+    select               => $number_column,
+    tables               => [ $self->meta->table ],
+    columns              => { $self->meta->table => [ $self->meta->column_names ] },
+    query_is_sql         => 1,
+    %conditions_for_in_use,
+  );
+
+  my @numbers        = do { no warnings 'once'; SL::DBUtils::selectall_array_query($::form, $self->db->dbh, $query_in_use, @{ $bind_vals_in_use || [] }) };
+  my %numbers_in_use = map { ( $_ => 1 ) } @numbers;
+
+  my $range_table    = ($business ? $business : SL::DB::Default->get)->load(for_update => 1);