What exactly does IsConnected do?

What exactly does IsConnected do?

I ask because since we started using If Not db.IsConnected Then db.Connect in our code, our database seems to start getting

Can’t connect to MySQL server on ‘…’ (10060)

errors after a short period of time.

We can’t be sure (it would be hard to explain why), but it seems that these messages only started appearing when we started using this new IsConnected feature.

Before using this new feature, we only used db.Connect after a minimum timeout at important points in the code. Back then, we didn’t have any 10060 errors.

In the past you would call Connect, then another call to see if the dbase opened/connected successfully. Now Connect both opens the connection and returns a boolean that tells you whether or not it was successful.

Are you talking about Connect or IsConnected?

You seem to be confusing API 1.0 and API 2.0 database methods.

In API 1.0 Database.Connect returned a Boolean and developers were responsible for checking the ErrorCode. Now, in API 2.0, Database.Connect does not return a Boolean, and will raise a DatabaseException with the error information.

The IsConnected property is new in 2024r2.

1 Like

Oops! Yes, I’m using ‘Connect’ and evaluating the returned Bool, in 2024 r4.2

…I have a lot of old code to fix, thanks for chiming in on this @Tim_Parnell

AFAIK, this is not 100% correct. It will indeed return a Boolean but raise no exception.

Since API 2:
Call db.Connected // Will NOT raise an exception
db.Connect // Will raise an exception (if something goes wrong)

You’re mixing APIs. API 1.0 still exists, but Connect was not renamed the way most API 2.0 functions were (such as Mid becoming Middle and changing offsets).

Connect as Boolean the API 1.0 variant. The signature you use changes which API is being used. If using the API 1.0 signature were to suddenly raise exceptions or not compile that would break existing API 1.0 code.

What I said is correct. If you are using call DB.Connect or var b as Boolean = DB.Connect you are using API 1.0.

1 Like

I just wanted to point out that all of your code can be APIv2 compatible and an:

If db.Connect Then
...
End If

would be executed without errors, but the exception that you might expect and handle between Try..Catch...End Try would not be thrown.


But all of this doesn’t answer my actual question. Even though it has now become clear that our problem has nothing to do with IsConnected, I would still like to know what exactly IsConnected does. :slight_smile:

Seems to me it doesn’t do anything. Its a property of a connection. You query it to see if the connection is still open. And I imagine its API2 only.

Personally I have wrappers for all the main database methods, within which I handle errors and, just as importantly the logging of errors. This meant that changing from database API1 stuff to API2 took me about 15 minutes.

Sure, but how does it know? Does it querry the Server (in this case, i would also recommend to not use this property…)? :slight_smile:

Good question. I only use SQLite where there is no server, and hence, no difficulty in its finding out.

Well they did update the MySQL plugin project in the Xojo Extras folder.

Boolean MySQLDatabaseIsConnected(REALdbDatabase dbObject)
{
	MySQLDatabaseData *db = (MySQLDatabaseData *)REALGetDBFromREALdbDatabase(dbObject);
	if (db && db->fConnection) {
		if (mysql_ping(db->fConnection) == 0) return true;
	}
	
	return false;
}

Danke @Tim_Parnell
Then the next question arises: Is auto-reconnect automatically activated with every connection?

If it’s using mysql_ping(MYSQL *mysql), does it report the following errors as exceptions?

@Javier_Menendez :slight_smile:

For MBS Xojo SQL Plugin and the SQLDatabaseMBS class, the IsConnected property just checks flags for whether there is a connection, not whether the connection is alive.

This is different for the isAlive property:

For Sybase, SQLBase, MS SQL Server, Oracle, ODBC, MySQL, MariaDB, DB2, CubeSQL, Informix, Interbase, FireBase and PostgreSQL we check the status of the connection. This may send a query over the network and sees if server responds properly. If network connection breaks, this function returns false.

For SQLite, DuckDB and SQL Anywhere we return true if we are connected, just like isConnected property.

I’ll add that to our documentation.

2 Likes