0

This is a multiuser application (multithreaded) where various departments will access their own database.The database is SQLite and I am using FireDac.For each department I have assigned a separate ADConnection so I dont get any unexpected locks.

Which connection will be activated (active) depends solely on the number produced by the ADQuery3. This is done on MainForm Show because it needs to be this way (which gets shown after successfull login). I would like to be able to close every connection on FormClose but I run into some bad issues when multiusers use the same database and log in and out.So I would like to ask if this is the right programming logic I am doing or this could be done in a better way?

Also I have never used this many begin end else and I am wondering how to proceed with this?

I mean when I need to check the if the number of another department came up, like if DataModule1.ADQuery3.FieldByName('DEPARTMENT').AsString = '12' where does the next ELSE come up?

procedure TMainForm.FormShow(Sender: TObject);
begin
if DataModule1.ADQuery3.FieldByName('DEPARTMENT').AsString = '13'
then begin
try
  if DataModule1.1_CONNECTION.Connected = true then
    DataModule1.1_CONNECTION.Connected := False
  else
    DataModule1.1_CONNECTION.DriverName:= 'SQLite';

  DataModule1.1_CONNECTION.Params.Values['Database']:= ExtractFilePath(Application.ExeName)+ 'mydatabase.db';
  DataModule1.1_CONNECTION.Connected := true;
  DataModule1.ADTable1.TableName :='DEPT_13';
  DataModule1.DEPT_13.Active:=True;
  cxGrid1.ActiveLevel.GridView := DEPT_13;
except
  on E: Exception do  begin
    ShowMessage('There was an error... : ' + E.Message);
  end;
end;

end;

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
user3777264
  • 72
  • 1
  • 9
  • What are these "bad issues" that you run into? – Jerry Dodge Jun 28 '14 at 04:38
  • 4
    This kind of question about if-then-else together with the mentioned "multithreading" makes me shiver... – Uwe Raabe Jun 28 '14 at 07:26
  • 1
    What did you have in mind with that exception handling? To show up a message? Just remove the whole `try..except` and you will get a message ... – Sir Rufo Jun 28 '14 at 08:32
  • Instead of asking if the connection is connected and disconnect it in that case, simply use `DataModule1.1_CONNECTION.Connected := false;` – Sir Rufo Jun 28 '14 at 08:33
  • 2
    *SQlite* and *multiuser* with direct connections to the **database file** does not fit together. (1) SQlite is not for multiuser (2) you *should not* open a SQlite database file from a remote location – Sir Rufo Jun 28 '14 at 08:37
  • Yes,Sir Rufo, you are right...no need to ask. However I must disagree with you on SQLite. SQLite can be run without a problem from a remote (network) location with multiple users accessing it. Wrap every insert/update/delete in a transaction and works like a charm. – user3777264 Jun 28 '14 at 21:59
  • You *should* read carefully what I wrote: "you **should not**" instead of "you **must not**". There are some issues on file locking and remote locations that can corrupt the database (mentioned on the SQlite site) – Sir Rufo Jun 29 '14 at 03:41
  • 1
    _where does the next ELSE come up?_ : Please get your hands on a Delphi tutorial and learn how `begin..end` and `if...then...else`, work before writing 'multi-user' and 'multi-threaded' code. This is **highly recommended** if you want to avoid **bad issues**... – Vector Jun 29 '14 at 05:13

0 Answers0