FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour ADO open/close Connection Question
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
ADO open/close Connection Question
Posted: Sun Dec 07, 2014 09:45 AM
Hello,

I'm using ADO and at the begin op my program I open a connection to the SQL-server.
This program is running for a long time, and sometimes I get a ''MySQL server has gone away'-error.
This is always when executing a command like 'INSERT INTO', not with recordsets. With recordsets, I allways create a new recordset when reading data.

That was the same problem that I had after resuming al laptop.
http://forums.fivetechsupport.com/viewtopic.php?f=3&t=28763&p=161537&hilit=connection+laptop#p161537

Now I was wondering if it would be beter to not open a connection at the beginning of the program and close in at the end,
but open a connecion only if i want to execute a SQL-command, like 'INSERT INTO',.. and than close it again.
For reading data I allways use a recordset, and than I also open it before reading.

Will the program become slower by allways opening and closing a connection? Are there other disadvantages doing this?
Are there other people do this also?
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: ADO open/close Connection Question
Posted: Sun Dec 07, 2014 11:22 AM

Marc,

I always open a recodset only when needed. Programs don't slow down. If you have to write a bunch of record then take care to open and close the recordset only once.

EMG

Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: ADO open/close Connection Question
Posted: Sun Dec 07, 2014 11:31 AM
Enrico,

I always open and close also when using a recordset, but if I want to inset records, or delete records, I don't use recordsets.
I use the
Code (fw): Select all Collapse
oSQL:=CreateObject("ADODB.Connection")
and then
Code (fw): Select all Collapse
oSQL:execute()
command.
Do you use also recordsets for adding and deleting records?
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: ADO open/close Connection Question
Posted: Sun Dec 07, 2014 12:25 PM

Marc,

If you don't use recordsets you still need to open and close connections.

EMG

Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: ADO open/close Connection Question
Posted: Sun Dec 07, 2014 01:16 PM
Enrico Maria Giordano wrote:Marc,

If you don't use recordsets you still need to open and close connections.

EMG


Enrico,

That was my question. Now I'm opening the connection at the beginning of the program, and close it at exit.
The problem is that sometimes when the program is a long time active, that the coenction is gone, and I was modering if it will be better instead of opening it at the beginning of the program, that I open the connectecion just before executing a SQL-command, and direct close it again.
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: ADO open/close Connection Question
Posted: Sun Dec 07, 2014 02:07 PM

Marc,

your best option is to open che connection just before the call to one or more Execute() methods and to close it at the end.

EMG

Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: ADO open/close Connection Question
Posted: Sun Dec 07, 2014 03:45 PM
Enrico Maria Giordano wrote:Marc,

your best option is to open che connection just before the call to one or more Execute() methods and to close it at the end.

EMG


Thanks Enrico,

Just one more question.
Is is the best that solution that 1 onlo use
oSQL:=CreateObject("ADODB.Connection")

at the beginning op the program, and then use
Code (fw): Select all Collapse
oSQL:Open()
oQry=oSQL:execute(.....)
oSQL:close()
....
oSQL:Open()
oQry=oSQL:execute(.....)
oSQL:close()
....
oSQL:Open()
oQry=oSQL:execute(.....)
oSQL:close()

oSQL:=nil


or always do
Code (fw): Select all Collapse
oSQL:=CreateObject("ADODB.Connection")
oSQL:Open()
oQry=oSQL:execute(.....)
oSQL:close()
oSQL:=nil
...
oSQL:=CreateObject("ADODB.Connection")
oSQL:Open()
oQry=oSQL:execute(.....)
oSQL:close()
oSQL:=nil
...
oSQL:=CreateObject("ADODB.Connection")
oSQL:Open()
oQry=oSQL:execute(.....)
oSQL:close()
oSQL:=nil
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: ADO open/close Connection Question
Posted: Sun Dec 07, 2014 04:10 PM

Marc,

you can safely use the first option (one only CreateObject()).

EMG

Continue the discussion