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

Commit 40e001d

Browse files
author
Chris Board
committed
Better handling if network connectivity lost
In the event of a network error, a helper method that checks the socket data returned by the MySQL server to check the packet type could be null and would therefore throw a NullPointerException. Instead, now a MySQLConnException will be thrown so it will be returned back with the callback to the database request.
1 parent 45fe0e1 commit 40e001d

File tree

13 files changed

+86
-18
lines changed

13 files changed

+86
-18
lines changed
0 Bytes
Binary file not shown.

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AndroidMySQLConnector/build.gradle

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

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

2626

2727
android {
28-
compileSdkVersion 29
28+
compileSdkVersion 30
2929
defaultConfig {
3030
//applicationId "com.BoardiesITSolutions.AndroidMySQLConnector"
3131
minSdkVersion 19
32-
targetSdkVersion 29
33-
versionCode 33
34-
versionName "0.45"
32+
targetSdkVersion 30
33+
versionCode 34
34+
versionName "0.46"
3535
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3636
}
3737
buildTypes {
@@ -63,6 +63,7 @@ android {
6363
def outputFile = output.outputFile
6464
if (outputFile != null && outputFile.name.endsWith('.aar')) {
6565
def fileName = "${archivesBaseName}-${version}.aar"
66+
//def fileName = "${archivesBaseName}-debug.aar"
6667
//output.outputFile = new File(outputFile.parent, fileName)
6768
outputFileName = fileName
6869
}
@@ -83,8 +84,8 @@ android {
8384
}
8485

8586
def getArtifactFullPath() {
86-
//return ".//build/outputs/aar/${archivesBaseName}-${project.version}.aar"
87-
return ".//build/outputs/aar/${archivesBaseName}-debug.aar"
87+
return ".//build/outputs/aar/${archivesBaseName}-${project.version}.aar"
88+
//return ".//build/outputs/aar/${archivesBaseName}-debug.aar"
8889
}
8990

9091
dependencies {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,23 @@ public void run()
714714
iConnectionInterface.handleInvalidSQLPacketException(ex);
715715
}
716716
}
717+
catch (final MySQLConnException ex) {
718+
Log.e("MySQLConnection", ex.toString());
719+
if (getReturnCallbackToMainThread())
720+
{
721+
getActivity().runOnUiThread(new Runnable() {
722+
@Override
723+
public void run()
724+
{
725+
iConnectionInterface.handleMySQLConnException(ex);
726+
}
727+
});
728+
}
729+
else
730+
{
731+
iConnectionInterface.handleMySQLConnException(ex);
732+
}
733+
}
717734
}
718735
}
719736

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.BoardiesITSolutions.AndroidMySQLConnector;
22

3+
import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.MySQLConnException;
4+
import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.MySQLException;
5+
36
import java.io.IOException;
47

58
public class Helpers
@@ -11,10 +14,14 @@ public enum MYSQL_PACKET_TYPE {MYSQL_ERROR_PACKET, MYSQL_OK_PACKET, MYSQL_EOF_PA
1114
* @param socketData The full socket data byte array that should be checked
1215
* @return MYSQL_PACKET_TYPE
1316
*/
14-
public static MYSQL_PACKET_TYPE getMySQLPacketType(byte[] socketData) throws IOException
17+
public static MYSQL_PACKET_TYPE getMySQLPacketType(byte[] socketData) throws IOException, MySQLConnException
1518
{
1619
//The packet type to check is the 4th byte, the first 3 bytes is the packet length,
1720
//the 4th byte is the MySQL sequence number and the 5th byte is the packet type
21+
if (socketData == null)
22+
{
23+
throw new MySQLConnException("Empty socket data was returned", 0, 0);
24+
}
1825
if (socketData.length < 4)
1926
{
2027
IOException exception = new IOException("Unexpected SQL Packet Size. Got Size: " + socketData.length);

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/PacketManager/MySQLErrorPacket.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.BoardiesITSolutions.AndroidMySQLConnector.Connection;
77
import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.InvalidSQLPacketException;
8+
import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.MySQLConnException;
89
import com.BoardiesITSolutions.AndroidMySQLConnector.Helpers;
910

1011
import java.io.ByteArrayOutputStream;
@@ -17,7 +18,7 @@ public class MySQLErrorPacket extends BasePacket
1718
private String errorMsg;
1819

1920
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
20-
public MySQLErrorPacket(Connection mysqlConn) throws IOException, InvalidSQLPacketException
21+
public MySQLErrorPacket(Connection mysqlConn) throws IOException, InvalidSQLPacketException, MySQLConnException
2122
{
2223
super(mysqlConn);
2324
this.processPacket();
@@ -37,7 +38,7 @@ public String getErrorMsg()
3738
}
3839

3940
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
40-
private void processPacket() throws IOException, InvalidSQLPacketException
41+
private void processPacket() throws IOException, InvalidSQLPacketException, MySQLConnException
4142
{
4243
this.setPacketLength(this.mysqlConn.getMysqlIO().fromByteArray((byte[]) this.mysqlConn.getMysqlIO().extractData(3)));
4344

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/PacketManager/MySQLOKPacket.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.BoardiesITSolutions.AndroidMySQLConnector.Connection;
77
import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.InvalidSQLPacketException;
8+
import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.MySQLConnException;
89
import com.BoardiesITSolutions.AndroidMySQLConnector.Helpers;
910

1011
import java.io.ByteArrayOutputStream;
@@ -15,7 +16,7 @@ public class MySQLOKPacket extends BasePacket
1516
private int affectedRows = 0;
1617
private int lastInsertID = 0;
1718

18-
public MySQLOKPacket(Connection mysqlConn) throws IOException, InvalidSQLPacketException
19+
public MySQLOKPacket(Connection mysqlConn) throws IOException, InvalidSQLPacketException, MySQLConnException
1920
{
2021
super(mysqlConn);
2122
processPacket();
@@ -31,7 +32,7 @@ public int getLastInsertID()
3132
return this.lastInsertID;
3233
}
3334

34-
private void processPacket() throws IOException, InvalidSQLPacketException
35+
private void processPacket() throws IOException, InvalidSQLPacketException, MySQLConnException
3536
{
3637
this.setPacketLength(this.mysqlConn.getMysqlIO().fromByteArray((byte[]) this.mysqlConn.getMysqlIO().extractData(3)));
3738
this.setPacketSequenceNumber((byte)this.mysqlConn.getMysqlIO().extractData(1));

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ public void run()
140140
}
141141
mutex.release();
142142
}
143+
catch (final MySQLConnException ex)
144+
{
145+
if (mysqlConn.getReturnCallbackToMainThread()) {
146+
mysqlConn.getActivity().runOnUiThread(new Runnable() {
147+
@Override
148+
public void run()
149+
{
150+
iConnectionInterface.handleMySQLConnException(ex);
151+
}
152+
});
153+
}
154+
else
155+
{
156+
iConnectionInterface.handleMySQLConnException(ex);
157+
}
158+
mutex.release();
159+
}
143160
}
144161

145162
@Override
@@ -251,7 +268,23 @@ public void run()
251268
else {
252269
iResultInterface.handleIOException(ex);
253270
}
254-
271+
}
272+
catch (final MySQLConnException ex)
273+
{
274+
if (mysqlConn.getReturnCallbackToMainThread())
275+
{
276+
mysqlConn.getActivity().runOnUiThread(new Runnable() {
277+
@Override
278+
public void run()
279+
{
280+
iResultInterface.handleMySQLConnException(ex);
281+
}
282+
});
283+
}
284+
else
285+
{
286+
iResultInterface.handleMySQLConnException(ex);
287+
}
255288
}
256289
}
257290

0 commit comments

Comments
 (0)