How long the life of a database connection ID?

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

How long the life of a database connection ID?

Post by Simon Knight » Sun Apr 13, 2014 11:04 am

Hi,

I have been reading material about using databases with Livecode and wonder what the best practice is with regards to the database connection ID. My final application will be running on a number of client computers all connected to the same local network. The application will read and write to a postgeSQL database located on the server that is running the LAN.

My internet reading has revealed two schools of thought or so I believe. The first creates/opens a database connection and uses it for the whole period the application is running i.e. many hours. The connection is only dropped when the application is quit. The second creates a connection for a specific use e.g. CREATE table and once the task is complete the connection is closed, obviously this is more complex but does it offer any advantages? Also does it effect how database cursors may be used?

Simon K
best wishes
Skids

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: How long the life of a database connection ID?

Post by bangkok » Sun Apr 13, 2014 6:18 pm

I put myself into the second category, thanks to my experience : good practice = open the connexion, query, close the connexion.

It's safer.

With the first method... all kind of problems can occur (and do occur on any networks) : time out, bad network leading to lost connexions etc.

And it's not more complicated. On the contrary.

Create 2 commands with short name like : dbstart, dbstop

they will open, and close the connexion, handle error message etc.

Between them, in your script, use the commands to send/execute SQL queries.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: How long the life of a database connection ID?

Post by Dixie » Sun Apr 13, 2014 8:02 pm

Like Bangkok.. I'm in the second' category too...:-)

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: How long the life of a database connection ID?

Post by Simon Knight » Sun Apr 13, 2014 11:02 pm

Thanks, I was leaning towards option 2 and your comments have been helpful.

Simon K
best wishes
Skids

Klaus
Posts: 14206
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How long the life of a database connection ID?

Post by Klaus » Mon Apr 14, 2014 11:42 am

Hi guys,

thanks for you insights, I will rethink my db strategies now :D



Best

Klaus

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: How long the life of a database connection ID?

Post by phaworth » Wed Apr 30, 2014 10:11 pm

I'll be the contrarian and go with option 1. Not doubting that communication errors can occur but errors are returned when that happens and you can script accordingly.

Some of this depends on your application. If all you are doing is querying or perhaps occasional db access during the life of the program, then maybe option 2 is OK although continually opening a db closing a db connection is time consuming. I've never quite figured out how you can sensibly deal with database cursors/recordsets and db locks in this scenario since those things disappear when you close the connection.

To me, continually opening/closing a db connection is somewhat similar to wrapping every instruction in a script in a try block just in case something goes wrong.

Pete

Post Reply