Need to Call db.connect twice in Postgres

Not sure what I did, but I have some code that suddenly requires me to call db.Connect two times in order to connect.

This code works:

If db.Connect then If db.Connect then Return True End If End If

This code fails to connect:

If db.Connect then Return True End If

Obviously, I did something to cause this to occur because I have not encountered this problem previously, but I can’t figure out why this would occur.

If I closed the database, this would re-establish the connection, so why wouldn’t the first db.connect work?

And, why would the first db.connect return true and not actually be connected?

The example itself doesn’t make sense. If the outer condition is false, the inner condition would never be called regardless if it’s a Return or another connect.

I take it this isn’t the actual code pulled from your app?

It was just a quick work around to try and troubleshoot this problem.

The code originally just said:

Return db.Connect

But I was running correct code in db.SQLSelect and returning a Nil recordset even though db.connect returned True.

I tried connecting twice in a row and suddenly all the code runs perfectly again (not sure what made me try this but it worked for some strange reason).

So it’s not that the first Connect is returning false, it’s that the SQL statements after it don’t work unless you “connect” a second time?

Have you checked for db errors at each step to see if that offers a clue?

Will do.

I tried this:

If me.Connect then If app.myDbase.Error then msgbox app.myDbase.ErrorMessage Else Return True End If Else Return False End If

which gives me the message: An unknown error has occurred.

Strangely enough, this code works:

If me.Connect then If app.myDbase.Error then Return me.Connect Else Return True End If Else Return False End If

Please note: this is a method (isAvailable) belonging to a custom database class “myDbase” which I call before running code to make sure there is a connection to the database.

If me.Connect then If app.myDbase.Error then

You are checking the error message only if me.connect succeeds. Not sure why you would want to.

Why not:

If app.myDbase.connect=false then
If app.myDbase.Error then

I suspect “me” and “app.myDbase” are two different objects.

me.connect is returning true, but me.error is also returning true.

I am guessing the error must have occurred prior to calling me.connect.

Turns out I had a thread that when run would get data and then close the database.

I guess it is a bad practice to close the database in a thread.

Better to open a new connection for the thread, the let it close and go away when it’s done.

Did not even realize that was possible.

It worked perfectly. Thanks!