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

Commit 690d677

Browse files
author
Chris Board
committed
Better connection timeout handling
- 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.
1 parent 23e9092 commit 690d677

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed
0 Bytes
Binary file not shown.

AndroidMySQLConnector/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ else
2020
}
2121

2222
archivesBaseName="AndroidMySQLConnector"
23-
version '0.47'
23+
version '0.48'
2424
group 'com.BoardiesITSolutions'
2525

2626

@@ -30,8 +30,8 @@ android {
3030
//applicationId "com.BoardiesITSolutions.AndroidMySQLConnector"
3131
minSdkVersion 19
3232
targetSdkVersion 30
33-
versionCode 35
34-
versionName "0.47"
33+
versionCode 36
34+
versionName "0.48"
3535
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3636
}
3737
buildTypes {
@@ -90,6 +90,6 @@ def getArtifactFullPath() {
9090

9191
dependencies {
9292
implementation fileTree(dir: 'libs', include: ['*.jar'])
93-
implementation 'androidx.appcompat:appcompat:1.1.0'
93+
implementation 'androidx.appcompat:appcompat:1.2.0'
9494
implementation("com.google.guava:guava:28.2-android")
9595
}

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/Connection.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class Connection
4040
private String database;
4141
private int port = 3306;
4242

43+
private int connectionTimeout = 30;
44+
4345
//Connection Details
4446
private boolean isMariaDB = false;
4547
private int majorVersion;
@@ -130,6 +132,25 @@ public Connection(String hostname, String username, String password, int port, I
130132
this.connect();
131133
}
132134

135+
/**
136+
* Set the connection timeout for the database connection. If not
137+
* set will default to 30 seconds
138+
* @param seconds
139+
*/
140+
public void setConnectionTimeout(int seconds)
141+
{
142+
this.connectionTimeout = seconds;
143+
}
144+
145+
/**
146+
* Return the connection timeout in seconds
147+
* @return
148+
*/
149+
public int getConnectionTimeout()
150+
{
151+
return this.connectionTimeout;
152+
}
153+
133154
public IConnectionInterface getiConnectionInterface()
134155
{
135156
return this.iConnectionInterface;

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/SocketSender.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import android.util.Log;
55
import java.io.BufferedOutputStream;
66
import java.io.IOException;
7+
import java.net.InetSocketAddress;
78
import java.net.Socket;
9+
import java.net.SocketAddress;
10+
import java.net.SocketTimeoutException;
811
import java.util.concurrent.Semaphore;
912

1013
import static com.BoardiesITSolutions.AndroidMySQLConnector.Connection.CLIENT_SSL;
@@ -30,10 +33,30 @@ protected Void doInBackground(byte[]... bytes)
3033
if (byteArray == null)
3134
{
3235
//If we have no bytes to send - we're just establishing the initial socket connection
33-
Socket socket = new Socket(this.mysqlConn.getHostname(), this.mysqlConn.getPort());
34-
//We've create the socket return the callback so we can process the socket data
36+
Socket socket = new Socket();
37+
SocketAddress socketAddress = new InetSocketAddress(this.mysqlConn.getHostname(), this.mysqlConn.getPort());
38+
3539
this.mysqlConn.setSocket(socket);
36-
this.mysqlConn.setMySQLIO(new MySQLIO(this.mysqlConn, socket));
40+
41+
if ((this.mysqlConn.getSSLSocket() != null) && (this.mysqlConn.getServerCapabilities() & CLIENT_SSL) == CLIENT_SSL)
42+
{
43+
Log.d("SocketSender", "Set MySQLIO for SSL socket");
44+
this.mysqlConn.getSSLSocket().setSoTimeout(this.mysqlConn.getConnectionTimeout() * 1000);
45+
this.mysqlConn.getSSLSocket().connect(socketAddress, this.mysqlConn.getConnectionTimeout() * 1000);
46+
this.mysqlConn.setMySQLIO(new MySQLIO(this.mysqlConn, this.mysqlConn.getSSLSocket()));
47+
48+
}
49+
else {
50+
51+
this.mysqlConn.getPlainSocket().setSoTimeout(this.mysqlConn.getConnectionTimeout() * 1000);
52+
this.mysqlConn.getPlainSocket().connect(socketAddress, this.mysqlConn.getConnectionTimeout() * 1000);
53+
54+
this.mysqlConn.setMySQLIO(new MySQLIO(this.mysqlConn, this.mysqlConn.getPlainSocket()));
55+
56+
57+
}
58+
59+
3760
}
3861
else
3962
{
@@ -54,6 +77,14 @@ protected Void doInBackground(byte[]... bytes)
5477
iIntConnectionInterface.socketDataSent();
5578

5679
}
80+
catch (SocketTimeoutException ex)
81+
{
82+
Log.e("SocketSender", "Socket Timeout Exception: " + ex.toString());
83+
if (this.mysqlConn.getiConnectionInterface() != null)
84+
{
85+
this.mysqlConn.getiConnectionInterface().handleException(ex);
86+
}
87+
}
5788
catch (IOException ex)
5889
{
5990
if (this.mysqlConn.getiConnectionInterface() != null && ex instanceof IOException)

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ To connect to a MySQL server you first need to create the MySQL Connection objec
6464
Connection mysqlConnection = new Connection("<Hostname>", "<Username>", "<Password>", <Port>, "<Database>", new IConnectionInterface()
6565
```
6666

67+
Once your connection object has been created you can then optionally call the following method `returnCallbackToMainThread(true, MainActivity.this);`
68+
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
69+
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
70+
yourself, you will receive an Android exception.
71+
72+
Also optionally, when the connection object has been created you can call the method `connection.setConnectionTimeout(5);` to override the default connection timeout
73+
of the database network activity. In this example the timeout will be 5 seconds, by default, the connection timeout will be 30 seconds.
74+
6775
In the above example, <database> is an optional parameter. By setting this, when the connection is established, the database name will be the default
6876
database used. The IConnectionInterface parameter handles connection specific events, such as successfully connected or exception handlers.
6977

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
buildscript {
55

66
ext {
7-
publishToMavenLocal = false
7+
publishToMavenLocal = true
88
}
99

1010
repositories {

0 commit comments

Comments
 (0)