[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2067
  • Last Modified:

Delphi - How to test if a Table is locked?

I'm trying to get to grips with how to overhaul some Delphi code to become multi-user.
The program has a sequence of forms that need to be posted to a Paradox Table controlled by the BDE.
In essence I want to check if the table is locked and if not lock the table or record and then post and then remove my locks.

The Borland BDE API examples seem to be working with grids and rely on working out where the cursor is.
They don't appear to be appropriate and do I need anything that complex.

I have three issues that I will separate out into distinct EE questions.

How do I see if the table is locked?

If I use the crude code below I can get the BDE intercepting it first telling me that the table is locked by another user.
Also it always gives me the same answer. I am forcing the table into Edit mode to notionally lock it.

function IsLocked(TheDataSet: TDataSet): Boolean;
begin
   Result := False;
   try
     TheDataSet.Edit;
     TheDataSet.Cancel;
   Except
      Result := True;
   end;
   if Result = True then ShowMessage('Database Locked');
   if Result = False then ShowMessage('Database UnLocked');
end;

Questions:

1. How do I improve the code so it works and
2. Trap the error calls so they are handled more elegantly.

With thanks,

Ed
0
edhasted
Asked:
edhasted
  • 4
  • 3
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:

When working with BDE, you will find that this is a little cleaner as you don't have to enter/exit the edit state (you only test the state). The plus side is that you get to handle the errors without raising/trapping an exception to do it.

Requires DB, DBTable, BDE in the uses clause.

function IsRecordLocked(DataSet: TBDEDataSet): LongBool;
begin

  // Check for active dataset
  if DataSet.Active then
  begin
     // Check if record is locked, if an error occurs return false
     if (Bde.DbiIsRecordLocked(DataSet.Handle, result) <> 0) then result:=False;
  end
  else
     // Not active
     result:=False;

end;


Regards,
Russell


0
 
Russell LibbySoftware Engineer, Advisory Commented:

If you need to determine if the table itself is locked, you can check the number of write locks against the dataset, eg:

function GetTableLockCount(DataSet: TBDEDataSet): Word;
begin

  // Set default result
  result:=0;

  // Check active state
  if DataSet.Active then
  begin
     // Get number of write locks on the table
     Bde.DbiIsTableLocked(DataSet.Handle, Bde.dbiWRITELOCK, result);
  end;

end;

Russell
0
 
edhastedAuthor Commented:
Russell - if I use the function GetTableLockCount(DataSet: TBDEDataSet): I'm always getting a a result of 0 even after I have put the table inot "edit" state - from what I have read on EE previously that should lock the table?

I was trying to lock it anotherwas but looking at the BDE calls like the one to release the TableLocks they almost always mention a cursor which throws me. I'm not using a grid so the concept of the cursor is superfluos. Can you elaborate?

DbiRelTableLock (hCursor: hDBICur; bAll: Bool; eLockType: DBILockType): DBIResult stdcall;


many thanks,

Ed
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
edhastedAuthor Commented:
Another variant maybe of the same question in the function IsRecordLocked(DataSet: TBDEDataSet): LongBool;
How does it know which record to lock?


Ed
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Ed,

Entering an Edit state will place a Record lock on the cursor (to be explained in a sec), not a full table lock. Note, it is generally in bad taste to lock the table unless you plan on performing some maintenance and need exclusive access to the table. What you originally posted:

function IsLocked(TheDataSet: TDataSet): Boolean;
begin
   Result := False;
   try
     TheDataSet.Edit;
     TheDataSet.Cancel;
   Except
      Result := True;
   end;
   if Result = True then ShowMessage('Database Locked');
   if Result = False then ShowMessage('Database UnLocked');
end;

translates into a record lock check, which I provided, but without entering an edit state and testing for an exception.

function IsRecordLocked(DataSet: TBDEDataSet): LongBool;
begin

  // Check for active dataset
  if DataSet.Active then
  begin
     // Check if record is locked, if an error occurs return false
     if (Bde.DbiIsRecordLocked(DataSet.Handle, result) <> 0) then result:=False;
  end
  else
     // Not active
     result:=False;

end;

Regarding the BDE usage of "cursors", that is what the Handle property is on a BDE dataset decendant. It is an hDBICur handle. Please note, that a cursor (in DB related terms), is an open connection to a record set (table/sql statement/etc), where there is a notion of a current location which is the current record (if not BOF/EOF). To adjust the cursors location, one uses Dataset.First/Prev/Next/Last etc. To check for cursor "edges", one tests for DataSet.BOF/EOF. You get the point.... all those calls are cursor related.

As to the grid question/comment; the cursor has absolutely NOTHING to do with any controls, grid or otherwise. The cursor still has a current location, regardless of being bound to a control or not. Think of data bound controls as nothing more than a UI reflection of the cursors state (eg, active record, field values, cursor location, etc).

As to the question "How does it know which record to lock?"; It tests/locks the current record for the cursor, just like calls to DataSet.Edit/Delete/etc operate on the current record for the cursor.

Regards,
Russell



0
 
edhastedAuthor Commented:
Russell - thank you for such an expnasive answer which explained in longhand what was written in shorthand in the BDE help.

As always examples explain all.

With many thanks,

Ed
0
 
Russell LibbySoftware Engineer, Advisory Commented:
My pleasure, glad to have helped.

Regards,
Russell
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now