Skip to content

Commit

Permalink
[misc] set default client character set to utf8mb4 client only when s…
Browse files Browse the repository at this point in the history
…erver version > 5.5
  • Loading branch information
rusher committed Feb 21, 2016
1 parent 161f581 commit 34a91cb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 47 deletions.
Expand Up @@ -231,10 +231,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
return rs.getMetaData();
}
if (resultSetMetaData == null) {
MariaDbServerPreparedStatement ssps = new MariaDbServerPreparedStatement(connection, this.sqlQuery, Statement.NO_GENERATED_KEYS);
ssps.close();
resultSetMetaData = ssps.getMetaData();
parameterMetaData = ssps.getParameterMetaData();
loadMetadata();
}
return resultSetMetaData;
}
Expand All @@ -259,14 +256,17 @@ protected void setParameter(final int parameterIndex, final ParameterHolder hold
public ParameterMetaData getParameterMetaData() throws SQLException {
checkClose();
if (parameterMetaData == null) {
MariaDbServerPreparedStatement ssps = new MariaDbServerPreparedStatement(connection, this.sqlQuery, Statement.NO_GENERATED_KEYS);
ssps.close();
resultSetMetaData = ssps.getMetaData();
parameterMetaData = ssps.getParameterMetaData();
loadMetadata();
}
return parameterMetaData;
}

private void loadMetadata() throws SQLException {
MariaDbServerPreparedStatement serverPreparedStatement = new MariaDbServerPreparedStatement(connection, this.sqlQuery, Statement.NO_GENERATED_KEYS);
serverPreparedStatement.close();
resultSetMetaData = serverPreparedStatement.getMetaData();
parameterMetaData = serverPreparedStatement.getParameterMetaData();
}

/**
* Clears the current parameter values immediately. <P>In general, parameter values remain in force for repeated use
Expand Down
74 changes: 38 additions & 36 deletions src/main/java/org/mariadb/jdbc/MariaDbServerPreparedStatement.java
Expand Up @@ -90,15 +90,15 @@ public MariaDbServerPreparedStatement(MariaDbConnection connection, String sql,
private void prepare(String sql) throws SQLException {
checkClose();
try {
connection.lock.lock();
lock.lock();
try {
if (protocol.hasUnreadData()) {
ExceptionMapper.throwException(new QueryException("There is an open result set on the current connection, which must be "
+ "closed prior to executing a query"), connection, this);
}
prepareResult = protocol.prepare(sql);
} finally {
connection.lock.unlock();
lock.unlock();
}
parameterCount = prepareResult.parameters.length;
currentParameterHolder = new ParameterHolder[prepareResult.parameters.length];
Expand Down Expand Up @@ -217,7 +217,7 @@ public int[] executeBatch() throws SQLException {

MariaDbResultSet rs = null;
MariaDbType[] parameterTypeHeader = new MariaDbType[parameterCount];
connection.lock.lock();
lock.lock();
executing = true;
QueryException exception = null;
try {
Expand Down Expand Up @@ -262,7 +262,7 @@ public int[] executeBatch() throws SQLException {
clearBatch();
throw new BatchUpdateException(sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode(), Arrays.copyOf(ret, counter), sqle);
} finally {
connection.lock.unlock();
lock.unlock();
clearBatch();
}
batchResultSet = rs;
Expand Down Expand Up @@ -364,7 +364,7 @@ public boolean execute() throws SQLException {
}
MariaDbResultSet rs = null;
boolean result = false;
connection.lock.lock();
lock.lock();
try {
result = executeInternal(currentParameterHolder, new MariaDbType[parameterCount]);
if (autoGeneratedKeys) {
Expand All @@ -373,7 +373,7 @@ public boolean execute() throws SQLException {
} catch (SQLException sqle) {
throw new BatchUpdateException(sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode(), new int[]{0}, sqle);
} finally {
connection.lock.unlock();
lock.unlock();
clearBatch();
}
batchResultSet = rs;
Expand All @@ -393,41 +393,43 @@ public boolean execute() throws SQLException {
*/
@Override
public void close() throws SQLException {
if (queryResult != null) {
queryResult.close();
queryResult = null;
}
// No possible future use for the cached results, so these can be cleared
// This makes the cache eligible for garbage collection earlier if the statement is not
// immediately garbage collected
cachedResultSets.clear();

if (protocol != null && protocol.isConnected()) {
try {
protocol.releasePrepareStatement(sql, prepareResult.statementId);
} catch (QueryException e) {
//if (log.isDebugEnabled()) log.debug("Error releasing preparedStatement", e);
closed.set(true);
lock.lock();
try {
if (queryResult != null) {
queryResult.close();
queryResult = null;
}
}

if (isStreaming()) {
connection.lock.lock();
try {
while (getInternalMoreResults(true)) {
// No possible future use for the cached results, so these can be cleared
// This makes the cache eligible for garbage collection earlier if the statement is not
// immediately garbage collected
cachedResultSets.clear();
if (protocol != null && protocol.isConnected()) {
try {
protocol.releasePrepareStatement(sql, prepareResult.statementId);
} catch (QueryException e) {
//if (log.isDebugEnabled()) log.debug("Error releasing preparedStatement", e);
}
} finally {
connection.lock.unlock();
}
}
super.close();
isClosed = true;

if (connection == null || connection.pooledConnection == null || connection.pooledConnection.statementEventListeners.isEmpty()) {
return;
if (isStreaming()) {
lock.lock();
try {
while (getInternalMoreResults(true)) {
}
} finally {
lock.unlock();
}
}
protocol = null;
if (connection == null || connection.pooledConnection == null
|| connection.pooledConnection.statementEventListeners.isEmpty()) {
return;
}
connection.pooledConnection.fireStatementClosed(this);
} finally {
lock.unlock();
}
connection.pooledConnection.fireStatementClosed(this);
connection = null;
protocol = null;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mariadb/jdbc/MariaDbStatement.java
Expand Up @@ -90,7 +90,7 @@ public class MariaDbStatement implements Statement {
protected boolean isRewriteable = true;
protected String firstRewrite = null;
protected ResultSet batchResultSet = null;
private AtomicBoolean closed = new AtomicBoolean();
protected AtomicBoolean closed = new AtomicBoolean();
boolean isTimedout;
volatile boolean executing;
List<Query> batchQueries;
Expand All @@ -102,7 +102,7 @@ public class MariaDbStatement implements Statement {
private int fetchSize;
private boolean isStreaming = false;
private int maxRows;
private ReentrantLock lock;
protected ReentrantLock lock;

public static final Pattern deleteEndSemicolonPattern = Pattern.compile("[;][ ]*$", Pattern.CASE_INSENSITIVE);

Expand Down
Expand Up @@ -590,7 +590,8 @@ private boolean isServerLanguageUtf8mb4(byte serverLanguage) {
}

private byte decideLanguage(byte serverLanguage) {
byte result = (isServerLanguageUtf8mb4(serverLanguage) ? serverLanguage : 45);
//force UTF8mb4 if possible, UTF8 if not.
byte result = (isServerLanguageUtf8mb4(serverLanguage) ? serverLanguage : (byte) (versionGreaterOrEqual(5, 5, 3) ? 45 : 33));
return result;
}

Expand Down

0 comments on commit 34a91cb

Please sign in to comment.