Jump to content

How to give correct link to created instance?


elGringo

Recommended Posts

  • Administrators

That is very intersting moment here ! 

 

Initially I put FDConnection to MainForm or special unit for FDConnection that I called in Main form and connected to DB.

 

I use FireDAC and MySQL so I just put libmysql.dll to EXE folder and it is OK if I use Stand Alone Mode.

 

Yesterday I tried to convert my EXE to DLL  and started UniAPP on IIS - so UniAPP could not find libmysql.dll

I tried to put it to the folder where IIS start itself, but no effect, then to system32, also no effect,

 

Then I tried to use FDManager - who knows that is special component in FireDAC to set up persistent connections, but nevermind here - it should be used 1 per APP, so if to put FDManager and other FD components to MainForm - than it errors that 1 FD Manager should be used...

 

Than I put all FD components to Server Module - and it works - and now you say Never put it to Server Module ))) 

 

It is kind of quest for me.

 

ServerModule is created only once:

 

http://www.unigui.com/doc/online_help/server_module.htm

 

MainModule is created per session:

 

http://www.unigui.com/doc/online_help/main_module.htm

 

You can put FDManager on ServerModule, but FDConnection should be on MainModule.

Link to comment
Share on other sites

Now i feel quest is done !!! Farshad big thanks for response!

 

Tested just now - works. FDManager on ServerModule and FDConnection on MainModule e.g. ClientSide

 

But what I can't understand - why is it bad to do connection on Server Side? In my simple example it worked perfect, but if you say not to do that I believe there should be serious reason?

 

When connection on Server Side there is 1 connection per all App and when on client side each connection for each Session?

 

100 Session - 1 Connection in 1 case

 

100 Sessions - 100 Connections in 2 case

Link to comment
Share on other sites

Because in real scenario transaction is not a single SQL command but a set of commands.

 

1. InsertOrder

2.    InsertOrderElement

3.    InsertOrderElement

4.    InsertOrderElement

5. Commit or Rollback

 

If another transaction (connection) start during above it will be data integration crash.

Link to comment
Share on other sites

So, better not to check it))

 

Farshad, I also wanted to ask about exceptions raised in UniMainModule, concerning our example above

 

When I connect to DB from UniMainModule no exceptions raised even if they are...

 

When I connect to DB from MainForm exception raised and I can see them in browser

 

Is it normal situation??

Link to comment
Share on other sites

1. Put your connection on MainModule.

2. Set its pooling property to True.

3. Connect your DB, on MainModule Create event.

4. Close your connection on MainModule.Destroy event.

5. For leightweight considerations, put your DataSet + DataSource on your formes or frames (beter way create theme on the fly); they will be free automatically on destroy event.

 

 

And you will not have any problem.

  • Upvote 1
Link to comment
Share on other sites

  • Administrators

So, better not to check it))

 

Farshad, I also wanted to ask about exceptions raised in UniMainModule, concerning our example above

 

When I connect to DB from UniMainModule no exceptions raised even if they are...

 

When I connect to DB from MainForm exception raised and I can see them in browser

 

Is it normal situation??

 

What type of exception do you get? Answer totally depends on the specific error you receive.

Link to comment
Share on other sites

In several projects I use three db connections:

- one in the servermodule, just to grab the company name from the db to create the browser page title.

This connection is closed immediately.

 

- one in the loginmodule, to take care of login, and this is also closed after login.

- one in the mainmodule, which connects at create and loads setup values stored in the mainmodule

 

In one project I have hundreds of datasets with datasources spread on various forms, all linking back to the mainmodule connection.

This never seems to be an issue. But I have to set the mainmodule database connection for all datasets in the forms' create event,

to make sure the datasets get the right connection, when there are several available.

 

But in that project I also have about 70 datasets in the mainmodule, so I guess it's all about being able to maintain some order.

 

If I get an exception in the servermodule or the mainmodule, like with the db connection, there is no message - that is right.

 

But I can save the exception message to the db, if that connection works. To get an exception from the servermodule without

advanced logging, you can use that approach I guess. But as said above - do not try to share that connection. Not without

some very advanced stuff taking care of unique access, and that is probably not worth it anyway since you can do it from the mainmodule.

 

I tried to run some heavier routine processing from the servermodule, that is stuff needed for the whole db, like SMS sending.

 

But I found it to be unreliable, and built a service application instead for that.

 

It was also easier to maintain this as two separate applications, rather than also have other server processes in the unigui app.

 

So now I rather create other server applications in addition if specialized server processes are needed,

rather than trying to put it all in unigui, even if it may seem tempting because of the servermodule being there.

 

I was trying to run things with timers etc., but the servermodule requires the app to be loaded once before it kicks in, so that

was another thing to consider. I guess for some simple stuff you can do it from the servermodule, if you are aware of the

limitations. I am not aware of them all, so there is probably some reason for precaution.

  • Upvote 1
Link to comment
Share on other sites

I run some lightweight processes from the servermodule -  I call external processes with a few parameters derived from the servermodule with a timer. So far this has not presented any issues. One Ttable derivative on the server module filtered at intervals using a timer on the servermodule - no other processes/sessions access it.

Link to comment
Share on other sites

Continuing discussion...

 

As it become clear from above - connection should live in MainModule and work during the session. But as for me i dislike trash in code, so for DataBase connection and queries for simple project I just create an Instanse of special TDataBaseUnit which is usually made from TDataModule.

 

Concerning our question here - which ways would be right?

 

 - just to put connection to MainModule and forget the problem

 

 -to put connection to Free DataModule - create an instance in MainModule and use it everywhere through property like MainModule.DataBaseModule.Connect

 

 - last case seems to be interesting for me - Application DataModule - as I understood from this forum - it lives during the session

but it is not clear - if i should create the instance or it is created automatically? And if it is created automatically then how to call it from any unit?

 

For example lets have this code - created automatically when i choose create Application DataModule - I added only 1 method Connect

unit uDataBaseUnit;

interface

uses
  SysUtils, Classes;

type
  TDataBaseUnit = class(TDataModule)
  private
    { Private declarations }
  public
    { Public declarations }

    procedure Connect;

  end;

function DataBaseUnit: TDataBaseUnit;

implementation

{$R *.dfm}

uses
  UniGUIVars, uniGUIMainModule, MainModule;

function DataBaseUnit: TDataBaseUnit;
begin
  Result := TDataBaseUnit(UniMainModule.GetModuleInstance(TDataBaseUnit));
end;

{ TDataBaseUnit }

procedure TDataBaseUnit.Connect;
begin
// some connection Code
end;

initialization
  RegisterModuleClass(TDataBaseUnit);

end.

I can't understand should I create instance of this or not in MainModule for example? And if not - then how to call this from any Unit?

 

Also this part of code

function DataBaseUnit: TDataBaseUnit;
begin
  Result := TDataBaseUnit(UniMainModule.GetModuleInstance(TDataBaseUnit));
end;

What does it mean?

Link to comment
Share on other sites

As I understand we can use Application DataModule as UniMainModule in sense that it also lives a session.

 

I just added to uses of Main 

uDataBaseUnit

and connected to DataBase like

DataBaseUnit.Connect

So I even didn't create an instanse of it... Is it correct approach?

Link to comment
Share on other sites

Hello elgringo,

- At the begining of my using unigui, I writed my first project wich only used an unigui Datamodule containing DBconnection and CRUD procedures (MainModule was empty) : all works fine at this day.

---------------------------------

So, you have nothing to do. No need to link (instance) your function DataBaseUnit to MainModule.

Your uDataBaseUnit is already instanced as TDataModule (like MainModule)

---------------------------------

 

- After, when i discovered the good practice, i am using MainModule for connecting my DBConnection.

 

- For beautifull code, you can put only your DBConnection component on MainModule and all your CRUD procedures on a separated uniDatamodule (a true one not a free datamodule).

Regards.

Link to comment
Share on other sites

Thanks AbakSoft! Unigui just beautiful in variety of options. I'm just trying to understand the borders of it all.

 

For me, for the moment, this is the discovery that when we choose Application Form or Application DataModule that we don't need to think of memory management.

 

I was coding as usual Delphi coder but UniGUI gives new options as I can see.

Link to comment
Share on other sites

Yes. It's correct as far as your uDadaBaseUnit is a TDataModule (like MainModule).

But steel a question : why Farshad created this MainModule ?

I think, it's the First unit wich is loaded when you start a session. So it has a priority again other uniDataModule.

Farshad can give here more détails :)

Best Regards.

Link to comment
Share on other sites

Yes, i think so, I see 3 possibilities for connection that I described above, but Application Data Modules seems to me most convenient if it is done well inside framework.

Free DataModule as i can see - traditional approach with manual memory management.

The same must be with Forms - Application Form and Free Form.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...