Administrators Farshad Mohajeri Posted November 29, 2016 Administrators Share Posted November 29, 2016 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 More sharing options...
elGringo Posted November 29, 2016 Author Share Posted November 29, 2016 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 More sharing options...
jaromir Posted November 29, 2016 Share Posted November 29, 2016 Because connection means transaction. You don't want to process all clients in one transaction, right ? Link to comment Share on other sites More sharing options...
elGringo Posted November 29, 2016 Author Share Posted November 29, 2016 why not?) Link to comment Share on other sites More sharing options...
elGringo Posted November 29, 2016 Author Share Posted November 29, 2016 I mean other side of this approach might be once "too many connections" from DBMS )) Link to comment Share on other sites More sharing options...
jaromir Posted November 29, 2016 Share Posted November 29, 2016 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 More sharing options...
elGringo Posted November 29, 2016 Author Share Posted November 29, 2016 jaromir - begining to understand what you mean - this all field for experiments So, i will use approach when 1 connection per Session. thank you all! Topic is finished! Link to comment Share on other sites More sharing options...
Administrators Farshad Mohajeri Posted November 29, 2016 Administrators Share Posted November 29, 2016 Transactions issue is only one of the concerns here. The main concern here is that under multi-threaded operations sharing one Connection between sessions will cause much more problem than you can imagine. Link to comment Share on other sites More sharing options...
elGringo Posted November 29, 2016 Author Share Posted November 29, 2016 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 More sharing options...
Abaksoft Posted November 29, 2016 Share Posted November 29, 2016 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. 1 Link to comment Share on other sites More sharing options...
elGringo Posted November 29, 2016 Author Share Posted November 29, 2016 Thank you, Abaksoft. I almost done this. Just Error Handling seems to be strange, I wrote above Link to comment Share on other sites More sharing options...
Administrators Farshad Mohajeri Posted November 30, 2016 Administrators Share Posted November 30, 2016 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 More sharing options...
Ron Posted December 7, 2016 Share Posted December 7, 2016 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. 1 Link to comment Share on other sites More sharing options...
Harry Rogers Posted December 7, 2016 Share Posted December 7, 2016 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 More sharing options...
elGringo Posted December 9, 2016 Author Share Posted December 9, 2016 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 More sharing options...
elGringo Posted December 9, 2016 Author Share Posted December 9, 2016 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 More sharing options...
Abaksoft Posted December 9, 2016 Share Posted December 9, 2016 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 More sharing options...
elGringo Posted December 9, 2016 Author Share Posted December 9, 2016 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 More sharing options...
elGringo Posted December 9, 2016 Author Share Posted December 9, 2016 AbakSoft, am I understand you correct that all I need to do is just to add to uses uDataBaseUnit and use already instanced instance? I did it so and it works Link to comment Share on other sites More sharing options...
Abaksoft Posted December 9, 2016 Share Posted December 9, 2016 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 More sharing options...
elGringo Posted December 9, 2016 Author Share Posted December 9, 2016 As i understand Farshad works on this project up to 10 years or more, so most of us just use basic things, so it is a big field for experiments 1 Link to comment Share on other sites More sharing options...
Skepsis IT Posted December 9, 2016 Share Posted December 9, 2016 Hi Another solution is to create a free data module that contains your queries, business logic etc, and create an instance of it from the form you need it by overriding the create constructor. 1 Link to comment Share on other sites More sharing options...
elGringo Posted December 9, 2016 Author Share Posted December 9, 2016 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now