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

Commit fa4f3af

Browse files
author
Chris Board
committed
Fixes issue with multiple queries in a loop running async
- Fixes an issue where if running multiple queries on a database as part of a loop in async (without the returnToMainThread being passed true) the library would deadlock and no further interaction with the database was completed.
1 parent 40e001d commit fa4f3af

File tree

8 files changed

+18
-50
lines changed

8 files changed

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

AndroidMySQLConnector/build.gradle

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

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

2626

@@ -30,8 +30,8 @@ android {
3030
//applicationId "com.BoardiesITSolutions.AndroidMySQLConnector"
3131
minSdkVersion 19
3232
targetSdkVersion 30
33-
versionCode 34
34-
versionName "0.46"
33+
versionCode 35
34+
versionName "0.47"
3535
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3636
}
3737
buildTypes {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ private void sendAuthResponse()
627627
@Override
628628
public void socketDataSent()
629629
{
630-
synchronized (mysqlIO.getConnection()) {
630+
//synchronized (mysqlIO.getConnection()) {
631631
Log.d("Connection", "Socket Data Sent");
632632
try {
633633
Helpers.MYSQL_PACKET_TYPE mysqlPacketType = Helpers.getMySQLPacketType(Connection.this.mysqlIO.getSocketByteArray());
@@ -732,7 +732,7 @@ public void run()
732732
}
733733
}
734734
}
735-
}
735+
//}
736736

737737
@Override
738738
public void handleException(final MySQLConnException ex)
@@ -806,10 +806,12 @@ public final String readString(byte[] byteBuffer) {
806806

807807
public void resetPacketSequenceNumber()
808808
{
809+
Log.d("Connection", "Resetting packet sequence number");
809810
this.packetSequenceNumber = 1;
810811
}
811812
public void resetPacketSequenceNumber(boolean resetToZero)
812813
{
814+
Log.d("Connection", "Resetting packet sequence number");
813815
if (resetToZero)
814816
{
815817
this.packetSequenceNumber = 0;
@@ -828,9 +830,9 @@ public static String toString(byte[] value, int offset, int length) {
828830

829831
public Statement createStatement()
830832
{
831-
synchronized (this.mysqlIO.getConnection()) {
833+
//synchronized (this.mysqlIO.getConnection()) {
832834
return new Statement(this);
833-
}
835+
//}
834836
}
835837

836838
private void parseVersionNumber()

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class MySQLIO
2626
private BufferedInputStream sockStream;
2727
private byte[] fullData;
2828
private int currentBytesRead = 0;
29-
private static final Semaphore mutex = new Semaphore(1);
3029
private static boolean isDBConnected = false;
3130

3231
public MySQLIO(Connection connection, Socket mysqlSock) throws IOException
@@ -84,7 +83,6 @@ public byte[] getSocketByteArray()
8483
private void getSocketData() throws IOException
8584
{
8685
try {
87-
mutex.acquire();
8886
Log.d("MySQLIO", "Reading Socket Data");
8987
ByteArrayOutputStream baos = new ByteArrayOutputStream();
9088
Log.d("MySQLIO", "Created baos");
@@ -109,9 +107,6 @@ private void getSocketData() throws IOException
109107
else {
110108

111109
fullData = Bytes.concat(fullData, tempFullData);
112-
//byte[] newFullData = new byte[fullData.length + tempFullData.length];
113-
//System.arraycopy(tempFullData, 0, newFullData, fullData.length, newFullData.length);
114-
//fullData = newFullData;
115110
}
116111
baos.reset();
117112
Log.d("MySQLIO", "Less than 1024 bytes receives. Expected Payload Length: " + expectedPayloadLength + " Current Byte Array Length: " + tempFullData.length);
@@ -144,10 +139,6 @@ private void getSocketData() throws IOException
144139
break;
145140
}
146141
Log.d("MySQIO", "Packet Type at -8 was: " + packetType + " so continuing fetching data");
147-
//Print out each byte from the array so can determine how the data looks
148-
/*for (int i = 0; i < tempFullData.length; i++) {
149-
Log.d("ByteData", "Index: " + i + " Value: " + tempFullData[i]);
150-
}*/
151142

152143
if (expectedPayloadLength == (tempFullData.length - 4))
153144
{
@@ -176,25 +167,12 @@ private void getSocketData() throws IOException
176167

177168
Log.d("MySQLIO", "Get Socket Data Finished. Full Data Length: " + fullData.length);
178169

179-
/*if (fullData == null) {
180-
fullData = baos.toByteArray();
181-
}
182-
else {
183-
//byte[] newFullData = new byte[fullData.length + tempFullData.length];
184-
//System.arraycopy(tempFullData, 0, newFullData, fullData.length, newFullData.length);
185-
//fullData = newFullData;
186-
fullData = Bytes.concat(fullData, tempFullData);
187-
//System.arraycopy(tempFullData, fullData.length, fullData, fullData.length, tempFullData.length);
188-
}*/
189-
//fullData = baos.toByteArray();
190170
Log.d("MySQLIO", "Full data written to: now size: " + fullData.length);
191171
currentBytesRead = 0;
192-
mutex.release();
193172
}
194173
catch (Exception ex)
195174
{
196175
Log.e("MySQIO", ex.toString());
197-
mutex.release();
198176
}
199177
}
200178

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class SocketSender extends AsyncTask<byte[], Void, Void>
1414
IIntConnectionInterface iIntConnectionInterface;
1515
Connection mysqlConn;
1616
private static final String TAG = "SocketSender";
17-
private Semaphore mutex = new Semaphore(1);
1817

1918
public SocketSender(Connection mysqlConn, IIntConnectionInterface iIntConnectionInterface)
2019
{
@@ -27,7 +26,6 @@ protected Void doInBackground(byte[]... bytes)
2726
{
2827
try
2928
{
30-
mutex.acquire();
3129
byte[] byteArray = bytes[0];
3230
if (byteArray == null)
3331
{
@@ -54,11 +52,10 @@ protected Void doInBackground(byte[]... bytes)
5452
}
5553
this.mysqlConn.getMysqlIO().reset();
5654
iIntConnectionInterface.socketDataSent();
57-
mutex.release();
55+
5856
}
59-
catch (IOException | InterruptedException ex)
57+
catch (IOException ex)
6058
{
61-
mutex.release();
6259
if (this.mysqlConn.getiConnectionInterface() != null && ex instanceof IOException)
6360
{
6461
this.mysqlConn.getiConnectionInterface().handleIOException((IOException)ex);

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class Statement
2121
{
2222
Connection mysqlConn;
2323
int affectedRows = 0;
24-
private static final Semaphore mutex = new Semaphore(1);
2524

2625
public Statement(Connection mysqlConn)
2726
{
@@ -42,7 +41,6 @@ public int getAffectedRows()
4241
public void execute(String query, final IConnectionInterface iConnectionInterface)
4342
{
4443
try {
45-
mutex.acquire();
4644
this.mysqlConn.resetPacketSequenceNumber(true);
4745
COM_Query comQuery = new COM_Query(this.mysqlConn, COM_Query.COM_QUERY, query);
4846
byte[] data = comQuery.getPacketData().toByteArray();
@@ -123,7 +121,6 @@ public void run()
123121
}
124122
}
125123
}
126-
mutex.release();
127124
}
128125
catch (final IOException ex) {
129126
if (mysqlConn.getReturnCallbackToMainThread()) {
@@ -138,7 +135,6 @@ public void run()
138135
else {
139136
iConnectionInterface.handleIOException(ex);
140137
}
141-
mutex.release();
142138
}
143139
catch (final MySQLConnException ex)
144140
{
@@ -155,7 +151,6 @@ public void run()
155151
{
156152
iConnectionInterface.handleMySQLConnException(ex);
157153
}
158-
mutex.release();
159154
}
160155
}
161156

@@ -174,32 +169,30 @@ public void run()
174169
else {
175170
iConnectionInterface.handleMySQLConnException(ex);
176171
}
177-
mutex.release();
178172
}
179173
});
180174

181175
socketSender.execute(data);
182176
}
183177
catch (IOException ex) {
178+
Log.e("Statement", "IOException: " + ex.toString());
184179
iConnectionInterface.handleIOException(ex);
185-
mutex.release();
186180
}
187181
catch (Exception ex)
188182
{
189-
Log.e("Statement", ex.toString());
190-
mutex.release();
183+
Log.e("Statement", "Exception: " + ex.toString());
184+
iConnectionInterface.handleException(ex);
191185
}
192186
}
193187

194188
public void executeQuery(String query, final IResultInterface iResultInterface)
195189
{
196-
synchronized (this.mysqlConn) {
190+
197191
try {
198192
this.mysqlConn.resetPacketSequenceNumber(true);
199193

200194
COM_Query comQuery = new COM_Query(this.mysqlConn, COM_Query.COM_QUERY, query);
201195
byte[] data = comQuery.getPacketData().toByteArray();
202-
203196
SocketSender socketSender = new SocketSender(this.mysqlConn, new IIntConnectionInterface() {
204197
@Override
205198
public void socketDataSent()
@@ -240,7 +233,6 @@ public void run()
240233
}
241234
}
242235
else {
243-
Log.d("Statement", "ABOUT TO CREATE COM_QUERY_RESPONSE");
244236
final COM_QueryResponse comQueryResponse = new COM_QueryResponse(Statement.this.mysqlConn);
245237
if (mysqlConn.getReturnCallbackToMainThread())
246238
mysqlConn.getActivity().runOnUiThread(new Runnable() {
@@ -311,6 +303,5 @@ public void run()
311303
catch (IOException ex) {
312304
iResultInterface.handleException(ex);
313305
}
314-
}
315306
}
316307
}

build.gradle

Lines changed: 2 additions & 2 deletions
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 {
@@ -16,7 +16,7 @@ buildscript {
1616
google()
1717
}
1818
dependencies {
19-
classpath 'com.android.tools.build:gradle:4.1.2'
19+
classpath 'com.android.tools.build:gradle:4.1.3'
2020
if (!publishToMavenLocal) {
2121
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
2222
}

demoapplication/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ dependencies {
3030
implementation fileTree(dir: 'libs', include: ['*.jar'])
3131

3232
implementation 'androidx.appcompat:appcompat:1.1.0'
33-
implementation 'com.github.BoardiesITSolutions:Android-MySQL-Connector:0.46_MySQL8'
33+
implementation 'com.github.BoardiesITSolutions:Android-MySQL-Connector:0.47_MySQL8'
3434

3535
}

0 commit comments

Comments
 (0)