Icon View Incident Report

Serious Serious
Reported By: Ole Willy Tuv
Reported On: 2/11/2003
For: Version 3.21 Build 1
# 1324 Possible AV During Batch SQL INSERTs Within a Transaction

I'm getting an AV every here on my Windows XP system every time I'm running the following code. The AV happens at random times from 50000 to 927000 records. I've not been able to insert all 1000000 records yet.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    DataPath: string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses mmSystem, DBISAMTb;

const
  ROWS = 1000000;
  BATCH = 1000;
  KEY = 500000;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DataPath := ExtractFilePath(Application.ExeName);
  DBISAMTb.Session.PrivateDir := DataPath;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Q: TDBISAMQuery;
begin
  Q := TDBISAMQuery.Create(nil);
  try
    with Q do
    begin
      DatabaseName := DataPath;
      SQL.Clear;
      SQL.Add('drop table if exists Test;');
      SQL.Add(
        'create table Test (' +
        'Field1 char(10), ' +
        'Field2 char(25), ' +
        'primary key (Field1));');
      SQL.Add('create index ix_Field2 on Test (Field2);');
      ExecSQL;
    end;
  finally
    Q.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  DB: TDBISAMDatabase;
  Q: TDBISAMQuery;
  Start, Stop: Cardinal;
  Recs, i: Integer;
begin
  DB := TDBISAMDatabase.Create(nil);
  try
    with DB do
    begin
      DatabaseName := 'TmpDB';
      Directory := DataPath;
    end;
    Q := TDBISAMQuery.Create(nil);
    try
      with Q do
      begin
        DatabaseName := DB.DatabaseName;
        SQL.Text := 'insert into Test values(:Field1, :Field2);';
        Caption := 'Inserting records...';
        Start := TimeGetTime;
        Prepare;
        DB.StartTransaction;
        try
          for i := 1 to ROWS do
          begin
            Params[0].AsInteger := i;
            Params[1].AsString := Format(
              'Field 2 - row # %s', [Params[0].AsString]);
            ExecSQL;
            Recs := i;
            if (i mod BATCH = 0) then
            begin
              DB.Commit;
              DB.StartTransaction;
            end;
          end;
          DB.Commit;
        except
          DB.Rollback;
          raise;
        end;
        Stop := TimeGetTime;
        Caption := Format('%d records inserted in %s secs',
          [Recs, FormatFloat('#0.000', (Stop-Start)/1000)]);
      end;
    finally
      Q.Free;
    end;
  finally
    DB.Free;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  Q: TDBISAMQuery;
  Start, Stop: Cardinal;
begin
  Q := TDBISAMQuery.Create(nil);
  try
    with Q do
    begin
      DatabaseName := DataPath;
      SQL.Text := Format(
        'select * from Test where Field1 = ''%d''', [KEY]);
      Start := TimeGetTime;
      Active := True;
      Stop := TimeGetTime;
      Caption := Format('Select 1: %d records fetched in %s secs',
        [RecordCount, FormatFloat('#0.000', (Stop-Start)/1000)]);
    end;
  finally
    Q.Free;
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  Q: TDBISAMQuery;
  Start, Stop: Cardinal;
begin
  Q := TDBISAMQuery.Create(nil);
  try
    with Q do
    begin
      DatabaseName := DataPath;
      SQL.Text := Format(
        'select * from Test where Field1 like ''%%%d%%''', [KEY]);
      Start := TimeGetTime;
      Active := True;
      Stop := TimeGetTime;
      Caption := Format('Select 2: %d records fetched in %s secs',
        [RecordCount, FormatFloat('#0.000', (Stop-Start)/1000)]);
    end;
  finally
    Q.Free;
  end;
end;

end.



Resolution Resolution
Fixed Problem on 2/14/2003 in version 3.22 build 1
Image