Skip to content

Commit

Permalink
[misc] deallocate query using compression correction and test correction
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Sep 15, 2016
1 parent b18f5a8 commit 40183b7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 88 deletions.
Expand Up @@ -231,11 +231,9 @@ public void addBatch() throws SQLException {

@Override
public void addBatch(String sql) throws SQLException {
preparedStatement.addBatch(sql);
throw new SQLException("Cannot do addBatch(String) on preparedStatement");
}



@Override
public void setArray(int parameterIndex, Array array) throws SQLException {
preparedStatement.setArray(parameterIndex, array);
Expand Down
Expand Up @@ -187,6 +187,16 @@ public void addBatch() throws SQLException {
queryParameters.add(currentParameterHolder.values().toArray(new ParameterHolder[0]));
}

/**
* Add batch.
* @param sql typically this is a SQL <code>INSERT</code> or <code>UPDATE</code> statement
* @throws SQLException every time since that method is forbidden on prepareStatement
*/
@Override
public void addBatch(final String sql) throws SQLException {
throw new SQLException("Cannot do addBatch(String) on preparedStatement");
}

public void clearBatch() {
queryParameters.clear();
hasLongData = false;
Expand Down Expand Up @@ -427,7 +437,7 @@ public void close() throws SQLException {
// This makes the cache eligible for garbage collection earlier if the statement is not
// immediately garbage collected

if (serverPrepareResult != null && protocol != null && protocol.isConnected()) {
if (serverPrepareResult != null && protocol != null) {
try {
serverPrepareResult.getUnProxiedProtocol().releasePrepareStatement(serverPrepareResult);
} catch (QueryException e) {
Expand Down
Expand Up @@ -26,8 +26,10 @@ public static void init(boolean mustLog) {
method = loggerClass.getMethod("getLogger", Class.class);
} catch (ClassNotFoundException classNotFound) {
System.out.println("Logging cannot be activated, missing slf4j dependency");
hasToLog = Boolean.FALSE;
} catch (NoSuchMethodException classNotFound) {
System.out.println("Logging cannot be activated, missing slf4j dependency");
hasToLog = Boolean.FALSE;
}
}
}
Expand Down
81 changes: 0 additions & 81 deletions src/main/java/org/mariadb/jdbc/internal/packet/ComStmtClose.java

This file was deleted.

Expand Up @@ -717,10 +717,10 @@ public boolean forceReleasePrepareStatement(int statementId) throws QueryExcepti
try {
checkClose();
try {
ComStmtClose.send(writer, statementId);
writer.closePrepare(statementId);
return true;
} catch (IOException e) {
throw new QueryException("Could not send query: " + e.getMessage(), -1, CONNECTION_EXCEPTION.getSqlState(), e);
throw new QueryException("Could not deallocate query: " + e.getMessage(), -1, CONNECTION_EXCEPTION.getSqlState(), e);
}
} finally {
lock.unlock();
Expand Down
Expand Up @@ -1043,4 +1043,37 @@ public boolean isUseCompression() {
return useCompression;
}

/**
* Send COM_STMT_CLOSE packet.
* @param statementId statement id to close.
* @throws IOException if connection error occur.
*/
public void closePrepare(int statementId) throws IOException {
byte[] packetBuffer;
if (useCompression) {
packetBuffer = new byte[12];
packetBuffer[0] = (byte) 5;
//packetBuffer[1,2,3,4,5,6] = (byte) 0;
packetBuffer[7] = Packet.COM_STMT_CLOSE;
packetBuffer[8] = (byte) (statementId & 0xff);
packetBuffer[9] = (byte) ((statementId >> 8) & 0xff);
packetBuffer[10] = (byte) ((statementId >> 16) & 0xff);
packetBuffer[11] = (byte) ((statementId >> 24) & 0xff);
} else {
packetBuffer = new byte[9];
packetBuffer[0] = (byte) 5; //packet length 1st byte
//packetBuffer[1,2,3] = (byte) 0;
packetBuffer[4] = Packet.COM_STMT_CLOSE;
packetBuffer[5] = (byte) (statementId & 0xff);
packetBuffer[6] = (byte) ((statementId >> 8) & 0xff);
packetBuffer[7] = (byte) ((statementId >> 16) & 0xff);
packetBuffer[8] = (byte) ((statementId >> 24) & 0xff);
}
if (logger.isTraceEnabled()) {
logger.trace("send packet seq:" + seqNo + " length:" + 5
+ " data:" + Utils.hexdump(packetBuffer, maxQuerySizeToLog));
}
outputStream.write(packetBuffer);
}

}
1 change: 0 additions & 1 deletion src/test/java/org/mariadb/jdbc/PreparedStatementTest.java
Expand Up @@ -74,7 +74,6 @@ public void cannotPrepareBatchFallback() throws Exception {
PreparedStatement stmt = sharedConnection.prepareStatement(
"insert into test_insert_select ( field1) (select TMP.field1 from (select ? `field1` from dual) TMP)",
Statement.RETURN_GENERATED_KEYS);
stmt.addBatch("insert into test_insert_select (field1) values ('test2')");
stmt.setString(1, "test");
stmt.addBatch();
stmt.executeBatch();
Expand Down

0 comments on commit 40183b7

Please sign in to comment.