Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Better connection timeout handling
Browse files Browse the repository at this point in the history
- Fixed issue so that the connection timeout doesn't the default Android socket timeout - which can be several minutes
- Changed default connection timeout to be 30 seconds
- Added new method "setConnectionTimeout" to Connection class to override the default conection timeout in seconds.
  • Loading branch information
Chris Board committed Mar 24, 2021
1 parent 23e9092 commit 690d677
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
8 changes: 4 additions & 4 deletions AndroidMySQLConnector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ else
}

archivesBaseName="AndroidMySQLConnector"
version '0.47'
version '0.48'
group 'com.BoardiesITSolutions'


Expand All @@ -30,8 +30,8 @@ android {
//applicationId "com.BoardiesITSolutions.AndroidMySQLConnector"
minSdkVersion 19
targetSdkVersion 30
versionCode 35
versionName "0.47"
versionCode 36
versionName "0.48"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down Expand Up @@ -90,6 +90,6 @@ def getArtifactFullPath() {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation("com.google.guava:guava:28.2-android")
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class Connection
private String database;
private int port = 3306;

private int connectionTimeout = 30;

//Connection Details
private boolean isMariaDB = false;
private int majorVersion;
Expand Down Expand Up @@ -130,6 +132,25 @@ public Connection(String hostname, String username, String password, int port, I
this.connect();
}

/**
* Set the connection timeout for the database connection. If not
* set will default to 30 seconds
* @param seconds
*/
public void setConnectionTimeout(int seconds)
{
this.connectionTimeout = seconds;
}

/**
* Return the connection timeout in seconds
* @return
*/
public int getConnectionTimeout()
{
return this.connectionTimeout;
}

public IConnectionInterface getiConnectionInterface()
{
return this.iConnectionInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.util.concurrent.Semaphore;

import static com.BoardiesITSolutions.AndroidMySQLConnector.Connection.CLIENT_SSL;
Expand All @@ -30,10 +33,30 @@ protected Void doInBackground(byte[]... bytes)
if (byteArray == null)
{
//If we have no bytes to send - we're just establishing the initial socket connection
Socket socket = new Socket(this.mysqlConn.getHostname(), this.mysqlConn.getPort());
//We've create the socket return the callback so we can process the socket data
Socket socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(this.mysqlConn.getHostname(), this.mysqlConn.getPort());

this.mysqlConn.setSocket(socket);
this.mysqlConn.setMySQLIO(new MySQLIO(this.mysqlConn, socket));

if ((this.mysqlConn.getSSLSocket() != null) && (this.mysqlConn.getServerCapabilities() & CLIENT_SSL) == CLIENT_SSL)
{
Log.d("SocketSender", "Set MySQLIO for SSL socket");
this.mysqlConn.getSSLSocket().setSoTimeout(this.mysqlConn.getConnectionTimeout() * 1000);
this.mysqlConn.getSSLSocket().connect(socketAddress, this.mysqlConn.getConnectionTimeout() * 1000);
this.mysqlConn.setMySQLIO(new MySQLIO(this.mysqlConn, this.mysqlConn.getSSLSocket()));

}
else {

this.mysqlConn.getPlainSocket().setSoTimeout(this.mysqlConn.getConnectionTimeout() * 1000);
this.mysqlConn.getPlainSocket().connect(socketAddress, this.mysqlConn.getConnectionTimeout() * 1000);

this.mysqlConn.setMySQLIO(new MySQLIO(this.mysqlConn, this.mysqlConn.getPlainSocket()));


}


}
else
{
Expand All @@ -54,6 +77,14 @@ protected Void doInBackground(byte[]... bytes)
iIntConnectionInterface.socketDataSent();

}
catch (SocketTimeoutException ex)
{
Log.e("SocketSender", "Socket Timeout Exception: " + ex.toString());
if (this.mysqlConn.getiConnectionInterface() != null)
{
this.mysqlConn.getiConnectionInterface().handleException(ex);
}
}
catch (IOException ex)
{
if (this.mysqlConn.getiConnectionInterface() != null && ex instanceof IOException)
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ To connect to a MySQL server you first need to create the MySQL Connection objec
Connection mysqlConnection = new Connection("<Hostname>", "<Username>", "<Password>", <Port>, "<Database>", new IConnectionInterface()
```

Once your connection object has been created you can then optionally call the following method `returnCallbackToMainThread(true, MainActivity.this);`
if you want any callback whether database connection or database queries, the call back to be executed via the UI thread. This can be useful if the callback needs to update the UI. By default
this will be false, so the callback will be executed within the same thread as the database connection activity, and if you then attempt to update UI without switching to the UI thread
yourself, you will receive an Android exception.

Also optionally, when the connection object has been created you can call the method `connection.setConnectionTimeout(5);` to override the default connection timeout
of the database network activity. In this example the timeout will be 5 seconds, by default, the connection timeout will be 30 seconds.

In the above example, <database> is an optional parameter. By setting this, when the connection is established, the database name will be the default
database used. The IConnectionInterface parameter handles connection specific events, such as successfully connected or exception handlers.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
buildscript {

ext {
publishToMavenLocal = false
publishToMavenLocal = true
}

repositories {
Expand Down

0 comments on commit 690d677

Please sign in to comment.