use of com.mysql.cj.ServerPreparedQuery in project ABC by RuiPinto96274.
the class ServerPreparedStatement method realClose.
@Override
public void realClose(boolean calledExplicitly, boolean closeOpenResults) throws SQLException {
JdbcConnection locallyScopedConn = this.connection;
if (locallyScopedConn == null) {
// already closed
return;
}
synchronized (locallyScopedConn.getConnectionMutex()) {
if (this.connection != null) {
//
// Don't communicate with the server if we're being called from the finalizer...
//
// This will leak server resources, but if we don't do this, we'll deadlock (potentially, because there's no guarantee when, what order, and
// what concurrency finalizers will be called with). Well-behaved programs won't rely on finalizers to clean up their statements.
//
CJException exceptionDuringClose = null;
if (this.isCached) {
locallyScopedConn.decachePreparedStatement(this);
this.isCached = false;
}
super.realClose(calledExplicitly, closeOpenResults);
((ServerPreparedQuery) this.query).clearParameters(false);
// Finally deallocate the prepared statement.
if (calledExplicitly && !locallyScopedConn.isClosed()) {
synchronized (locallyScopedConn.getConnectionMutex()) {
try {
((NativeSession) locallyScopedConn.getSession()).sendCommand(this.commandBuilder.buildComStmtClose(null, ((ServerPreparedQuery) this.query).getServerStatementId()), true, 0);
} catch (CJException sqlEx) {
exceptionDuringClose = sqlEx;
}
}
}
if (exceptionDuringClose != null) {
throw exceptionDuringClose;
}
}
}
}
use of com.mysql.cj.ServerPreparedQuery in project ABC by RuiPinto96274.
the class ServerPreparedStatement method serverPrepare.
protected void serverPrepare(String sql) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
SQLException t = null;
try {
ServerPreparedQuery q = (ServerPreparedQuery) this.query;
q.serverPrepare(sql);
} catch (IOException ioEx) {
t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), ioEx, this.exceptionInterceptor);
} catch (CJException sqlEx) {
SQLException ex = SQLExceptionsMapping.translateException(sqlEx);
if (this.dumpQueriesOnException.getValue()) {
StringBuilder messageBuf = new StringBuilder(((PreparedQuery<?>) this.query).getOriginalSql().length() + 32);
messageBuf.append("\n\nQuery being prepared when exception was thrown:\n\n");
messageBuf.append(((PreparedQuery<?>) this.query).getOriginalSql());
ex = appendMessageToException(ex, messageBuf.toString(), this.exceptionInterceptor);
}
t = ex;
} finally {
// Leave the I/O channel in a known state...there might be packets out there that we're not interested in
try {
this.session.clearInputStream();
} catch (Exception e) {
if (t == null) {
t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), e, this.exceptionInterceptor);
}
}
if (t != null) {
throw t;
}
}
}
}
use of com.mysql.cj.ServerPreparedQuery in project JavaSegundasQuintas by ecteruel.
the class ServerPreparedStatement method executeBatchSerially.
@Override
protected long[] executeBatchSerially(int batchTimeout) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
JdbcConnection locallyScopedConn = this.connection;
if (locallyScopedConn.isReadOnly()) {
throw SQLError.createSQLException(Messages.getString("ServerPreparedStatement.2") + Messages.getString("ServerPreparedStatement.3"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
}
clearWarnings();
// Store this for later, we're going to 'swap' them out
// as we execute each batched statement...
ServerPreparedQueryBindValue[] oldBindValues = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
try {
long[] updateCounts = null;
if (this.query.getBatchedArgs() != null) {
int nbrCommands = this.query.getBatchedArgs().size();
updateCounts = new long[nbrCommands];
if (this.retrieveGeneratedKeys) {
this.batchedGeneratedKeys = new ArrayList<>(nbrCommands);
}
for (int i = 0; i < nbrCommands; i++) {
updateCounts[i] = -3;
}
SQLException sqlEx = null;
int commandIndex = 0;
ServerPreparedQueryBindValue[] previousBindValuesForBatch = null;
CancelQueryTask timeoutTask = null;
try {
timeoutTask = startQueryTimer(this, batchTimeout);
for (commandIndex = 0; commandIndex < nbrCommands; commandIndex++) {
Object arg = this.query.getBatchedArgs().get(commandIndex);
try {
if (arg instanceof String) {
updateCounts[commandIndex] = executeUpdateInternal((String) arg, true, this.retrieveGeneratedKeys);
// limit one generated key per OnDuplicateKey statement
getBatchedGeneratedKeys(this.results.getFirstCharOfQuery() == 'I' && containsOnDuplicateKeyInString((String) arg) ? 1 : 0);
} else {
((ServerPreparedQuery) this.query).setQueryBindings((ServerPreparedQueryBindings) arg);
ServerPreparedQueryBindValue[] parameterBindings = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
if (previousBindValuesForBatch != null) {
for (int j = 0; j < parameterBindings.length; j++) {
if (parameterBindings[j].bufferType != previousBindValuesForBatch[j].bufferType) {
((ServerPreparedQuery) this.query).getQueryBindings().getSendTypesToServer().set(true);
break;
}
}
}
try {
updateCounts[commandIndex] = executeUpdateInternal(false, true);
} finally {
previousBindValuesForBatch = parameterBindings;
}
// limit one generated key per OnDuplicateKey statement
getBatchedGeneratedKeys(containsOnDuplicateKeyUpdateInSQL() ? 1 : 0);
}
} catch (SQLException ex) {
updateCounts[commandIndex] = EXECUTE_FAILED;
if (this.continueBatchOnError && !(ex instanceof MySQLTimeoutException) && !(ex instanceof MySQLStatementCancelledException) && !hasDeadlockOrTimeoutRolledBackTx(ex)) {
sqlEx = ex;
} else {
long[] newUpdateCounts = new long[commandIndex];
System.arraycopy(updateCounts, 0, newUpdateCounts, 0, commandIndex);
throw SQLError.createBatchUpdateException(ex, newUpdateCounts, this.exceptionInterceptor);
}
}
}
} finally {
stopQueryTimer(timeoutTask, false, false);
resetCancelledState();
}
if (sqlEx != null) {
throw SQLError.createBatchUpdateException(sqlEx, updateCounts, this.exceptionInterceptor);
}
}
return (updateCounts != null) ? updateCounts : new long[0];
} finally {
((ServerPreparedQuery) this.query).getQueryBindings().setBindValues(oldBindValues);
((ServerPreparedQuery) this.query).getQueryBindings().getSendTypesToServer().set(true);
clearBatch();
}
}
}
use of com.mysql.cj.ServerPreparedQuery in project JavaSegundasQuintas by ecteruel.
the class ServerPreparedStatement method serverPrepare.
protected void serverPrepare(String sql) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
SQLException t = null;
try {
ServerPreparedQuery q = (ServerPreparedQuery) this.query;
q.serverPrepare(sql);
} catch (IOException ioEx) {
t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), ioEx, this.exceptionInterceptor);
} catch (CJException sqlEx) {
SQLException ex = SQLExceptionsMapping.translateException(sqlEx);
if (this.dumpQueriesOnException.getValue()) {
StringBuilder messageBuf = new StringBuilder(((PreparedQuery<?>) this.query).getOriginalSql().length() + 32);
messageBuf.append("\n\nQuery being prepared when exception was thrown:\n\n");
messageBuf.append(((PreparedQuery<?>) this.query).getOriginalSql());
ex = appendMessageToException(ex, messageBuf.toString(), this.exceptionInterceptor);
}
t = ex;
} finally {
// Leave the I/O channel in a known state...there might be packets out there that we're not interested in
try {
this.session.clearInputStream();
} catch (Exception e) {
if (t == null) {
t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), e, this.exceptionInterceptor);
}
}
if (t != null) {
throw t;
}
}
}
}
use of com.mysql.cj.ServerPreparedQuery in project JavaSegundasQuintas by ecteruel.
the class ServerPreparedStatement method asSql.
@Override
public String asSql(boolean quoteStreamsAndUnknowns) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
ClientPreparedStatement pStmtForSub = null;
try {
pStmtForSub = ClientPreparedStatement.getInstance(this.connection, ((PreparedQuery<?>) this.query).getOriginalSql(), this.getCurrentDatabase());
int numParameters = ((PreparedQuery<?>) pStmtForSub.query).getParameterCount();
int ourNumParameters = ((PreparedQuery<?>) this.query).getParameterCount();
ServerPreparedQueryBindValue[] parameterBindings = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
for (int i = 0; (i < numParameters) && (i < ourNumParameters); i++) {
if (parameterBindings[i] != null) {
if (parameterBindings[i].isNull()) {
pStmtForSub.setNull(i + 1, MysqlType.NULL);
} else {
ServerPreparedQueryBindValue bindValue = parameterBindings[i];
//
switch(bindValue.bufferType) {
case MysqlType.FIELD_TYPE_TINY:
pStmtForSub.setByte(i + 1, ((Long) bindValue.value).byteValue());
break;
case MysqlType.FIELD_TYPE_SHORT:
pStmtForSub.setShort(i + 1, ((Long) bindValue.value).shortValue());
break;
case MysqlType.FIELD_TYPE_LONG:
pStmtForSub.setInt(i + 1, ((Long) bindValue.value).intValue());
break;
case MysqlType.FIELD_TYPE_LONGLONG:
pStmtForSub.setLong(i + 1, ((Long) bindValue.value).longValue());
break;
case MysqlType.FIELD_TYPE_FLOAT:
pStmtForSub.setFloat(i + 1, ((Float) bindValue.value).floatValue());
break;
case MysqlType.FIELD_TYPE_DOUBLE:
pStmtForSub.setDouble(i + 1, ((Double) bindValue.value).doubleValue());
break;
default:
pStmtForSub.setObject(i + 1, parameterBindings[i].value);
break;
}
}
}
}
return pStmtForSub.asSql(quoteStreamsAndUnknowns);
} finally {
if (pStmtForSub != null) {
try {
pStmtForSub.close();
} catch (SQLException sqlEx) {
// ignore
}
}
}
}
}
Aggregations