Skip to content

Commit

Permalink
[CONJ-1006] disabling cachePrepStmts with useServerPrepStmts might re…
Browse files Browse the repository at this point in the history
…sult in Exception
  • Loading branch information
rusher committed Sep 1, 2022
1 parent 9ffd90e commit 0f6ba26
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/mariadb/jdbc/BaseCallableStatement.java
Expand Up @@ -47,6 +47,7 @@ public abstract class BaseCallableStatement extends ServerPreparedStatement
* @param procedureName procedure name
* @param canUseServerTimeout indicate if server support server timeout
* @param canUseServerMaxRows indicate if server support server max rows
* @param canCachePrepStmts can cache server prepared result
* @param resultSetType resultset type
* @param resultSetConcurrency resultset concurrency
* @param defaultFetchSize default fetch size
Expand All @@ -60,6 +61,7 @@ public BaseCallableStatement(
String procedureName,
boolean canUseServerTimeout,
boolean canUseServerMaxRows,
boolean canCachePrepStmts,
int resultSetType,
int resultSetConcurrency,
int defaultFetchSize)
Expand All @@ -70,6 +72,7 @@ public BaseCallableStatement(
lock,
canUseServerTimeout,
canUseServerMaxRows,
canCachePrepStmts,
Statement.RETURN_GENERATED_KEYS,
resultSetType,
resultSetConcurrency,
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/mariadb/jdbc/Connection.java
Expand Up @@ -45,6 +45,7 @@ public class Connection implements java.sql.Connection {
private final AtomicInteger savepointId = new AtomicInteger();
private boolean readOnly;
private final boolean canUseServerTimeout;
private final boolean canCachePrepStmts;
private final boolean canUseServerMaxRows;
private final int defaultFetchSize;
private final boolean forceTransactionEnd;
Expand All @@ -71,6 +72,7 @@ public Connection(Configuration conf, ReentrantLock lock, Client client) {
this.canUseServerMaxRows =
context.getVersion().isMariaDBServer()
&& context.getVersion().versionGreaterOrEqual(10, 3, 0);
this.canCachePrepStmts = context.getConf().cachePrepStmts();
this.defaultFetchSize = context.getConf().defaultFetchSize();
}

Expand Down Expand Up @@ -147,6 +149,7 @@ public PreparedStatement prepareInternal(
lock,
canUseServerTimeout,
canUseServerMaxRows,
canCachePrepStmts,
autoGeneratedKeys,
resultSetType,
resultSetConcurrency,
Expand Down Expand Up @@ -482,6 +485,7 @@ public CallableStatement prepareCall(String sql, int resultSetType, int resultSe
lock,
canUseServerTimeout,
canUseServerMaxRows,
canCachePrepStmts,
resultSetType,
resultSetConcurrency);
} else {
Expand All @@ -493,6 +497,7 @@ public CallableStatement prepareCall(String sql, int resultSetType, int resultSe
lock,
canUseServerTimeout,
canUseServerMaxRows,
canCachePrepStmts,
resultSetType,
resultSetConcurrency);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/mariadb/jdbc/FunctionStatement.java
Expand Up @@ -21,6 +21,7 @@ public class FunctionStatement extends BaseCallableStatement implements Callable
* @param lock thread lock object
* @param canUseServerTimeout can use server timeout
* @param canUseServerMaxRows can use server max rows
* @param canCachePrepStmts can cache server prepared result
* @param resultSetType result set type
* @param resultSetConcurrency concurrency type
* @throws SQLException if any error occurs
Expand All @@ -33,6 +34,7 @@ public FunctionStatement(
ReentrantLock lock,
boolean canUseServerTimeout,
boolean canUseServerMaxRows,
boolean canCachePrepStmts,
int resultSetType,
int resultSetConcurrency)
throws SQLException {
Expand All @@ -44,6 +46,7 @@ public FunctionStatement(
procedureName,
canUseServerTimeout,
canUseServerMaxRows,
canCachePrepStmts,
resultSetType,
resultSetConcurrency,
0);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/mariadb/jdbc/ProcedureStatement.java
Expand Up @@ -23,6 +23,7 @@ public class ProcedureStatement extends BaseCallableStatement implements Callabl
* @param lock thread locker
* @param canUseServerTimeout can use server timeout
* @param canUseServerMaxRows can use server max rows
* @param canCachePrepStmts can cache server prepared result
* @param resultSetType result-set type
* @param resultSetConcurrency concurrency
* @throws SQLException if any exception occurs
Expand All @@ -35,6 +36,7 @@ public ProcedureStatement(
ReentrantLock lock,
boolean canUseServerTimeout,
boolean canUseServerMaxRows,
boolean canCachePrepStmts,
int resultSetType,
int resultSetConcurrency)
throws SQLException {
Expand All @@ -46,6 +48,7 @@ public ProcedureStatement(
procedureName,
canUseServerTimeout,
canUseServerMaxRows,
canCachePrepStmts,
resultSetType,
resultSetConcurrency,
0);
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/org/mariadb/jdbc/ServerPreparedStatement.java
Expand Up @@ -33,7 +33,7 @@ public class ServerPreparedStatement extends BasePreparedStatement {
Pattern.compile(
"^(\\s*/\\*([^*]|\\*[^/])*\\*/)*\\s*(SELECT|UPDATE|INSERT|DELETE|REPLACE|DO|CALL)",
Pattern.CASE_INSENSITIVE);

private final boolean canCachePrepStmts;
/**
* Server prepare statement constructor
*
Expand All @@ -42,6 +42,7 @@ public class ServerPreparedStatement extends BasePreparedStatement {
* @param lock thread safe lock
* @param canUseServerTimeout can server use timeout
* @param canUseServerMaxRows can server use max rows
* @param canCachePrepStmts can server cache prepared statement
* @param autoGeneratedKeys must command return automatically generated keys
* @param resultSetType resultset type
* @param resultSetConcurrency resultset concurrency
Expand All @@ -54,6 +55,7 @@ public ServerPreparedStatement(
ReentrantLock lock,
boolean canUseServerTimeout,
boolean canUseServerMaxRows,
boolean canCachePrepStmts,
int autoGeneratedKeys,
int resultSetType,
int resultSetConcurrency,
Expand All @@ -69,7 +71,8 @@ public ServerPreparedStatement(
resultSetType,
resultSetConcurrency,
defaultFetchSize);
prepareResult = con.getContext().getPrepareCache().get(sql, this);
this.canCachePrepStmts = canCachePrepStmts;
prepareResult = canCachePrepStmts ? con.getContext().getPrepareCache().get(sql, this) : null;
if (prepareResult == null && !PREPARABLE_STATEMENT_PATTERN.matcher(sql).find()) {
con.getClient().execute(new PreparePacket(sql), this, true);
}
Expand All @@ -86,7 +89,8 @@ protected void executeInternal() throws SQLException {
validParameters();
lock.lock();
String cmd = escapeTimeout(sql);
if (prepareResult == null) prepareResult = con.getContext().getPrepareCache().get(cmd, this);
if (prepareResult == null && canCachePrepStmts)
prepareResult = con.getContext().getPrepareCache().get(cmd, this);
try {
if (prepareResult == null && con.getContext().hasClientCapability(STMT_BULK_OPERATIONS)) {
try {
Expand Down Expand Up @@ -135,7 +139,7 @@ private void executePipeline(String cmd) throws SQLException {

private void executeStandard(String cmd) throws SQLException {
// send COM_STMT_PREPARE
if (prepareResult == null) {
if (prepareResult == null && canCachePrepStmts) {
prepareResult = con.getContext().getPrepareCache().get(cmd, this);
if (prepareResult == null) {
con.getClient().execute(new PreparePacket(cmd), this, true);
Expand Down Expand Up @@ -194,7 +198,8 @@ private void executeInternalPreparedBatch() throws SQLException {
*/
private void executeBatchBulk(String cmd) throws SQLException {
List<Completion> res;
if (prepareResult == null) prepareResult = con.getContext().getPrepareCache().get(cmd, this);
if (prepareResult == null && canCachePrepStmts)
prepareResult = con.getContext().getPrepareCache().get(cmd, this);
try {
if (prepareResult == null) {
ClientMessage[] packets;
Expand Down Expand Up @@ -248,7 +253,8 @@ private void executeBatchBulk(String cmd) throws SQLException {
* @throws SQLException if Command error
*/
private void executeBatchPipeline(String cmd) throws SQLException {
if (prepareResult == null) prepareResult = con.getContext().getPrepareCache().get(cmd, this);
if (prepareResult == null && canCachePrepStmts)
prepareResult = con.getContext().getPrepareCache().get(cmd, this);
// server is 10.2+, permitting to execute last prepare with (-1) statement id.
// Server send prepare, followed by execute, in one exchange.
int maxCmd = 250;
Expand Down Expand Up @@ -332,7 +338,7 @@ private void executeBatchStandard(String cmd) throws SQLException {
for (Parameters batchParameter : batchParameters) {
// prepare is in loop, because if connection fail, prepare is reset, and need to be re
// prepared
if (prepareResult == null) {
if (prepareResult == null && canCachePrepStmts) {
prepareResult = con.getContext().getPrepareCache().get(cmd, this);
if (prepareResult == null) {
con.getClient().execute(new PreparePacket(cmd), this, false);
Expand Down
Expand Up @@ -142,7 +142,7 @@ public StandardClient(
clientCapabilities,
conf,
this.exceptionFactory,
new PrepareCache(conf.prepStmtCacheSize(), this));
conf.cachePrepStmts() ? new PrepareCache(conf.prepStmtCacheSize(), this) : null);

this.reader.setServerThreadId(handshake.getThreadId(), hostAddress);
this.writer.setServerThreadId(handshake.getThreadId(), hostAddress);
Expand Down
Expand Up @@ -80,6 +80,9 @@ public void execute() throws SQLException {
try (Connection con = createCon("&useServerPrepStmts")) {
execute(con);
}
try (Connection con = createCon("&useServerPrepStmts&cachePrepStmts=false")) {
execute(con);
}
}

private void execute(Connection conn) throws SQLException {
Expand Down

0 comments on commit 0f6ba26

Please sign in to comment.