During connection lost, how to close database connection

I'm using TidTCPServer OnExecute method. When client connect to server, I create Database connection with Unidac to PostGreSQL (connection ceated and handled inside onExecute).

Problem is when the connection to client is lost, the connection to database does'nt disappear. When cheking from PostGRE pgAdmin there is several connections more than what we have clients.

I have tried to put dbConnect.close or FreeandNil(dbConnect) or dbConnect.Free in try.. except.. end except block. But it looks like when the connection is lost, the thread stop/collaps and the database connection stays.

I'm using Delphi XE.
0
Utf
11/18/2011 8:28:33 PM
embarcadero.delphi.winsock 1874 articles. 2 followers. Follow

3 Replies
2579 Views

Similar Articles

[PageSpeed] 17

Tapani wrote:

> Problem is when the connection to client is lost, the connection to
> database does'nt disappear. When cheking from PostGRE pgAdmin
> there is several connections more than what we have clients.

Have you tried closing the database in the server's OnDisconnect event if 
it is still open?

--
Remy Lebeau (TeamB)
0
Remy
11/18/2011 10:40:32 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Tapani wrote:
> 
> > Problem is when the connection to client is lost, the connection to
> > database does'nt disappear. When cheking from PostGRE pgAdmin
> > there is several connections more than what we have clients.
> 
> Have you tried closing the database in the server's OnDisconnect event if 
> it is still open?
> 
> --
> Remy Lebeau (TeamB)



Parts from my source code:
In Main unit:

/--------------------------------
type
  TMainForm = class(TForm)
    GPRSTCPServer: TIdTCPServer;
....
....

procedure TMainForm.GPRSTCPServerExecute(AContext: TIdContext);
....

var
  // LiningDB_GPRS: TLiningDB;
  DB_Connection: TUniConnection;

....

begin

....
      DB_Connection := TUniConnection.Create(nil);
      DB_Connection.LoginPrompt := false;
      DB_Connection.ProviderName := 'PostgreSQL';
      DB_Connection.Database :=...
....


//-----------------

How can I close database connection in servers onDisconnect as the DB_Connection is defined inside the procedure GPRSTCPServerExecute?

 
Tapani.
0
Utf
11/19/2011 6:48:59 AM
To solve the problem, you can save the pid of a session after connect and kill this session using saved pid after reconnect. Here is a code example:
{code}
// saving session pid
UniQuery.SQL.Text := 'select pg_backend_pid()';
UniQuery.Open;
pid := UniQuery.Fields[0].AsString;
UniQuery.Close;

//killing session using saved pid
UniQuery.SQL.Text := 'select pg_terminate_backend(:pid)';
UniQuery.ParamByName('pid').AsString := pid;
UniQuery.Open;
ShowMessage('Was session killed? = ' + BoolToStr(UniQuery.Fields[0].AsString = 't', True));
UniQuery.Close;
{code}
--
Devart (www.devart.com) – intelligent database interaction solutions
0
Devart
3/30/2012 11:46:59 AM
Reply:

Similar Artilces:

Connection.close() not closing my connection?
My servlet calls con.close() within the finally {} of a try..catch block. Yet I notice that sa_conn_info still lists the connection. Shouldn't the connection disappear from the sa_conn_info list when I close it? And if the answer to that is "yes, it should", has anybody seen situations like mine where it *doesn't* close it? Thanks for your help, Kathleen My bad... through a series of really stupid coding mistakes, I managed to cause a SQLException to be thrown from within the finally {} block before the connection could be closed... and I was ignoring the ...

SFtp connect
The following Ftp connect to a sftp site has the right port/usrname/password [verified thru filezilla], but when I try to connect via TIDFTP -- I get "Connection Closed Gracefully" upon ftp->Connect();; The ftp properties are below the code. I have no other Indy objects on the form. It ocurred to me that I may need something for sftp. ftp->Connect(); if (ftp->Connected()) { ::Sleep(1); } object ftp: TIdFTP AutoLogin = True Host = 'ftp.dailyaccess.com' Password ='XXXXX' Port = 21 Username = 'charles.leggette' ProxySet...

how to close database connection, connection pool issue
I have create a asp.net 2.0 application with mysql using the .net/connector 5.1. Most of my pages use a ObjectDataSource control and a Repeater Control. I am calling Close() on both the reader and DB connection but it appears that the connections are not closing and the connection pool gets filled up and I get an error. Does anyone know why the connections aren't closing?  Heres the function that the ObjectDataSource calls:Function myFunc(ByVal subCat As String, ByVal sortBy As String) Dim connStr As String = ConfigurationManager.ConnectionStrings("connector").Co...

Database Connection???How to connect a Database in an other server?
How to connect a Database(.mdb) in an other server? Can I use the connection string like below? mycon.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=http://www.****.com/***/mydb.mdb" You can not. That simple. Access is file based. No a client/server architecture.Thomas TomiczekPowerNodes ApS(Microsoft MVP C#/.NET)---Building a Website? Try the PowerNodes CMS - http://www.powernodes.com/ Correction: you can. Just make sure you can reach the acces db file - through a file share that is bound as drive. Not that you should. Performance will be horrib...

difference between connection.close and command.connection.close
hello, What is the difference between odbcconnection.close and odbccommand.connection.close? I am making connection once in my application in global.asax and opening connection at application_start event and closing it at application_end event. I am accessing connection through "global.myconnection" in webforms. NOw in the webforms,do I have to mycommand.connection.open before comand.executereader?? Does mycommand.connection.close , closes the odbcconnection that I make in gloabl.asax? It is confusing, because on the first function executing on webform, when I myc...

underlying connection was closed: The connection was closed unexpectedly
Hi ,   I am working with wcf and linq  and i am facing a problem"The underlying connection was closed: The connection was closed unexpectedly. " when i am trying to return my linq query to the client.I tried for changing the code in web.config for client and server.like   Client :   <behaviors> <endpointBehaviors> <behavior name="debuggingBehaviour"> <dataContractSerializer maxItemsInObjectGraph="2147483646" />                 &nb...

not connected when I am connected
What do I need to change on my 'puter when the icon in the toolbar shows and tells me I am connected and the error message tells me I am not and I can not go to web pages, get or send emails. I have to close down and restart to get connected again. This only happens if I am on line and leave the 'puter for an hour or so. As long as I am using it I don't have a problem. This just started about a month ago. I am on very slow dialup, DSL and Cable not in my area, thanks you SBC/ATT tks Elaine wrote: > What do I need to change on my 'puter when the icon in ...

database connection lost -> fdquerys closed
When there is a temporary loose of connection, all fdquerys become close. Is there a property I can change to avoid that? I seem unable to find it. Basically I want them to work like clientdatasets do, caching data until reconnection. Using "offline" would be awkward because we would have to call it one by one, and moreover it doesn't seem to do what I want. TIA angel sanchez wrote: > Basically I want them to work like clientdatasets do, caching data > until reconnection. Using "offline" would be awkward because we would > have to call it one by on...

Wifi connection and database connection
What is the solution to recovering the connection to a remote database (i.e. in a wifi environment due to loss of coverage or in case the machine goes into standby)? Could we check for specific SQLDBCodes? Do we need to destroy the SQLCA or is there a workaround? ...

Switching Connections or Closing All Connections
I have an access database on a different host and unfortunately not able to have it on my own web server and I was curious if there is a way to close all connections to it.  I have tried throwing bad queries to it to throw errors and the database will still not unlock.  I am tired of having to wait for 30 minutes to maybe see it unlock.  It's my first time without being able to control the web server that it's on... Normally I can just shutdown the application via server.  Now I have to use a hosting site and I need to find some efficient way of shutting down the ...

How do connect your page to your database connection?
How do connect your page to your database connection? I have created a page, default.aspx. I have created a database connection to an Access database. I see the connection in the little database window on the right hand side of WebMatrix--okay, now how to I get my page, default.aspx, to connect to the connection? Web.config file? If so, how do I config the config file using the WYSIWYG interface of WebMatirx? Or if there is another way to set this up? I know how to do this in Dreamweaver MX, but WebMatrix is still baffling to me. How much of it truly is a WYSIWYG and how much...

How can I change my connection to a different database...it is not connecting to the database I created.
My data is connected to the ASPNETDB.MDF database. Is this a must that I have it connected to this database or can I have it connected to a database that I created. You can connect to different database such as sql or other access files. All you have to do is to define which connection string to use within your web.config file. If you want to make sure that your database is prepared to be used, use features such as ASPNET_REGSQL.EXE at VS Command Prompt. Hope this helps. Jae.jae.lee@jaeleeandco.com I'm in the Army...here in Georgia. Is it cold there.. Hi Jae,    &nb...

superreview granted: [Bug 338243] Enabling TLS for new IMAP connections doesn't close old connections : [Attachment 237619] close cached connections when socket type changes
Scott MacGregor <mscott@mozilla.org> has granted David Bienvenu <bienvenu@nventure.com>'s request for superreview: Bug 338243: Enabling TLS for new IMAP connections doesn't close old connections https://bugzilla.mozilla.org/show_bug.cgi?id=338243 Attachment 237619: close cached connections when socket type changes https://bugzilla.mozilla.org/attachment.cgi?id=237619&action=edit ...

superreview requested: [Bug 338243] Enabling TLS for new IMAP connections doesn't close old connections : [Attachment 237619] close cached connections when socket type changes
David Bienvenu <bienvenu@nventure.com> has asked Scott MacGregor <mscott@mozilla.org> for superreview: Bug 338243: Enabling TLS for new IMAP connections doesn't close old connections https://bugzilla.mozilla.org/show_bug.cgi?id=338243 Attachment 237619: close cached connections when socket type changes https://bugzilla.mozilla.org/attachment.cgi?id=237619&action=edit ...

Web resources about - During connection lost, how to close database connection - embarcadero.delphi.winsock

Connection - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

iMedia Connection: Interactive Marketing News, Features, Podcasts and Video - iMediaConnection.com
High-quality data, if not used properly, can still lead marketers to make bad decisions. Consider these common ways that numbers are used to ...

HTTP persistent connection - Wikipedia, the free encyclopedia
... tacked on to an existing protocol. If the browser supports keep-alive, it adds an additional header to the request: Following this, the connection ...

CareerSonar Turns Facebook Friends Into Job Connections
Looking for a job ? Among your Facebook friends lies the potential for employment. CareerSonar , a new service, brings together a person’s connections ...

Half of data connections by top 500 Android apps are 'covert' with no effect on user experience
Researchers at the Massachusetts Institute of Technology have discovered that half of the communications connections established by the top free ...

The Direct Connection
... And the people reading my words respond back to me with words of their own. The technology that allows this is powerful but this direct connection ...

Don’t Let Great Content Fall on Deaf Ears: Know your Connection Strategy
... via employee newsletter when it should have come through small-group manager meetings All of these and more are the result of a faulty connection ...

Be Goofy, Smile At Your Baby: Protection Begins with Connection, Pediatrician Says
VideoPart 3 of a series Andrew Garner wears silly ties and finds it easy to make a goofy face. These are valuable tools for charming the babies ...

Mali arrests 2 in connection with deadly hotel attack
Mali arrests 2 in connection with deadly hotel attack

Get ready for live-streamed extreme sports: Seattle startup Sioeye adds 4G connection to action cam
The history of people doing crazy stunts on camera is full of notable achievements. Motorcycle daredevil Evel Knievel jumped over whatever he ...

Resources last updated: 11/27/2015 2:43:22 PM