Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request Boundary methods - beginRequest()/endRequest() implementation #708

Merged
merged 21 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.microsoft.sqlserver.jdbc;

import java.sql.SQLException;
import java.sql.ShardingKey;

public interface ISQLServerConnection43 extends ISQLServerConnection {

public void beginRequest() throws SQLException;

public void endRequest() throws SQLException;

public void setShardingKey(ShardingKey shardingKey) throws SQLServerException;

public void setShardingKey(ShardingKey shardingKey,
ShardingKey superShardingKey) throws SQLServerException;

public boolean setShardingKeyIfValid(ShardingKey shardingKey,
int timeout) throws SQLServerException;

public boolean setShardingKeyIfValid(ShardingKey shardingKey,
ShardingKey superShardingKey,
int timeout) throws SQLServerException;

}
132 changes: 121 additions & 11 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -3218,6 +3219,9 @@ public Statement createStatement(int resultSetType,
checkClosed();
Statement st = new SQLServerStatement(this, resultSetType, resultSetConcurrency,
SQLServerStatementColumnEncryptionSetting.UseConnectionSetting);
if (requestStarted) {
addOpenStatement(st);
}
loggerExternal.exiting(getClassNameLogging(), "createStatement", st);
return st;
}
Expand All @@ -3241,7 +3245,9 @@ public PreparedStatement prepareStatement(String sql,
st = new SQLServerPreparedStatement(this, sql, resultSetType, resultSetConcurrency,
SQLServerStatementColumnEncryptionSetting.UseConnectionSetting);
}

if (requestStarted) {
addOpenStatement(st);
}
loggerExternal.exiting(getClassNameLogging(), "prepareStatement", st);
return st;
}
Expand All @@ -3264,7 +3270,9 @@ private PreparedStatement prepareStatement(String sql,
else {
st = new SQLServerPreparedStatement(this, sql, resultSetType, resultSetConcurrency, stmtColEncSetting);
}

if (requestStarted) {
addOpenStatement(st);
}
loggerExternal.exiting(getClassNameLogging(), "prepareStatement", st);
return st;
}
Expand All @@ -3288,7 +3296,9 @@ public CallableStatement prepareCall(String sql,
st = new SQLServerCallableStatement(this, sql, resultSetType, resultSetConcurrency,
SQLServerStatementColumnEncryptionSetting.UseConnectionSetting);
}

if (requestStarted) {
addOpenStatement(st);
}
loggerExternal.exiting(getClassNameLogging(), "prepareCall", st);
return st;
}
Expand Down Expand Up @@ -4646,6 +4656,9 @@ public Statement createStatement(int nType,
checkValidHoldability(resultSetHoldability);
checkMatchesCurrentHoldability(resultSetHoldability);
Statement st = new SQLServerStatement(this, nType, nConcur, stmtColEncSetting);
if (requestStarted) {
addOpenStatement(st);
}
loggerExternal.exiting(getClassNameLogging(), "createStatement", st);
return st;
}
Expand Down Expand Up @@ -4708,7 +4721,9 @@ public PreparedStatement prepareStatement(java.lang.String sql,
else {
st = new SQLServerPreparedStatement(this, sql, nType, nConcur, stmtColEncSetting);
}

if (requestStarted) {
addOpenStatement(st);
}
loggerExternal.exiting(getClassNameLogging(), "prepareStatement", st);
return st;
}
Expand Down Expand Up @@ -4744,7 +4759,9 @@ public CallableStatement prepareCall(String sql,
else {
st = new SQLServerCallableStatement(this, sql, nType, nConcur, stmtColEncSetiing);
}

if (requestStarted) {
addOpenStatement(st);
}
loggerExternal.exiting(getClassNameLogging(), "prepareCall", st);
return st;
}
Expand Down Expand Up @@ -5296,14 +5313,87 @@ public <T> T unwrap(Class<T> iface) throws SQLException {
return t;
}

public void beginRequest() throws SQLFeatureNotSupportedException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLFeatureNotSupportedException("beginRequest not implemented");
private boolean requestStarted = false;
private boolean originalDatabaseAutoCommitMode;
private int originalTransactionIsolationLevel;
private int originalNetworkTimeout;
private int originalHoldability;
private boolean originalSendTimeAsDatetime;
private int originalStatementPoolingCacheSize;
private boolean originalDisableStatementPooling;
private int originalServerPreparedStatementDiscardThreshold;
private Boolean originalEnablePrepareOnFirstPreparedStatementCall;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh also this is the wrapper Boolean, this would be null by default. Change this to "boolean" if you want to avoid weird confusions in the future.

Copy link
Contributor Author

@ulvii ulvii Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enablePrepareOnFirstPreparedStatementCall is Boolean, kept the types same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh. you know what, I saw that getEnablePrepareOnFirstPreparedStatementCall() returns boolean and assumed enablePrepareOnFirstPreparedStatementCall was boolean too. Looking at the code, the Boolean was intentional (it makes use of the null state), so keeping the originalEnablePrepareOnFirstPreparedStatementCall as Boolean here is the right choice too.

private String originalSCatalog;
private volatile SQLWarning originalSqlWarnings;
private List<Statement> openStatements;

protected void beginRequestInternal() throws SQLException {
synchronized (this) {
if (!requestStarted) {
originalDatabaseAutoCommitMode = databaseAutoCommitMode;
originalTransactionIsolationLevel = transactionIsolationLevel;
originalNetworkTimeout = getNetworkTimeout();
originalHoldability = holdability;
originalSendTimeAsDatetime = sendTimeAsDatetime;
originalStatementPoolingCacheSize = statementPoolingCacheSize;
originalDisableStatementPooling = disableStatementPooling;
originalServerPreparedStatementDiscardThreshold = getServerPreparedStatementDiscardThreshold();
originalEnablePrepareOnFirstPreparedStatementCall = getEnablePrepareOnFirstPreparedStatementCall();
originalSCatalog = sCatalog;
originalSqlWarnings = sqlWarnings;
openStatements = new LinkedList<Statement>();
requestStarted = true;
}
}
}

public void endRequest() throws SQLFeatureNotSupportedException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLFeatureNotSupportedException("endRequest not implemented");
protected void endRequestInternal() throws SQLException {
synchronized (this) {
if (requestStarted) {
if (!databaseAutoCommitMode) {
rollback();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you decided to do a rollback here. If database is not in autocommit mode, at least the rollback will always succeed. However, it looks like Oracle JDBC spec for endRequest suggests that if the pooling manager calls endRequest while there is a transaction going on, the driver throw a SQLException (https://docs.oracle.com/javase/9/docs/api/java/sql/Connection.html#endRequest--). Have you considered why we're not throwing an exception if there's a transaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the 3rd party pool managers do a rollback before returning connections to the pool, I decided to keep it the same way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, I think that's fair.

}
if (databaseAutoCommitMode != originalDatabaseAutoCommitMode) {
setAutoCommit(originalDatabaseAutoCommitMode);
}
if (transactionIsolationLevel != originalTransactionIsolationLevel) {
setTransactionIsolation(originalTransactionIsolationLevel);
}
if (getNetworkTimeout() != originalNetworkTimeout) {
setNetworkTimeout(null, originalNetworkTimeout);
}
if (holdability != originalHoldability) {
setHoldability(originalHoldability);
}
if (sendTimeAsDatetime != originalSendTimeAsDatetime) {
setSendTimeAsDatetime(originalSendTimeAsDatetime);
}
if (statementPoolingCacheSize != originalStatementPoolingCacheSize) {
setStatementPoolingCacheSize(originalStatementPoolingCacheSize);
}
if (disableStatementPooling != originalDisableStatementPooling) {
setDisableStatementPooling(originalDisableStatementPooling);
}
if (getServerPreparedStatementDiscardThreshold() != originalServerPreparedStatementDiscardThreshold) {
setServerPreparedStatementDiscardThreshold(originalServerPreparedStatementDiscardThreshold);
}
if (getEnablePrepareOnFirstPreparedStatementCall() != originalEnablePrepareOnFirstPreparedStatementCall) {
setEnablePrepareOnFirstPreparedStatementCall(originalEnablePrepareOnFirstPreparedStatementCall);
}
if (!sCatalog.equals(originalSCatalog)) {
setCatalog(originalSCatalog);
}
sqlWarnings = originalSqlWarnings;
if (null != openStatements) {
while (!openStatements.isEmpty()) {
try (Statement st = openStatements.get(0)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this logic again. This might result in infinite loop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, after discussing with Ulvi it turns out the try block calls close() which calls removeOpenStatement, so this while loop does eliminate elements from the list successfully. no change needed here.

}
}
openStatements.clear();
}
requestStarted = false;
}
}
}

/**
Expand Down Expand Up @@ -5879,6 +5969,26 @@ public void onEviction(Sha1HashKey key, PreparedStatementHandle handle) {
}
}
}

/**
* @param st
* Statement to add to openStatements
*/
final synchronized void addOpenStatement(Statement st) {
if (null != openStatements) {
openStatements.add(st);
}
}

/**
* @param st
* Statement to remove from openStatements
*/
final synchronized void removeOpenStatement(Statement st) {
if (null != openStatements) {
openStatements.remove(st);
}
}
}

// Helper class for security manager functions used by SQLServerConnection class.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.microsoft.sqlserver.jdbc;

import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.ShardingKey;

Expand All @@ -8,7 +9,51 @@ public class SQLServerConnection43 extends SQLServerConnection implements ISQLSe
SQLServerConnection43(String parentInfo) throws SQLServerException {
super(parentInfo);
}


/**
* Hints to the driver that a request, an independent unit of work, is beginning on this connection. It backs up the values of the connection
* properties that are modifiable through public methods. Each request is independent of all other requests with regard to state local to the
* connection either on the client or the server. Work done between {@code beginRequest}, {@code endRequest} pairs does not depend on any other
* work done on the connection either as part of another request or outside of any request. A request may include multiple transactions. There may
* be dependencies on committed database state as that is not local to the connection. {@code beginRequest} marks the beginning of the work unit.
* <p>
* Local state is defined as any state associated with a Connection that is local to the current Connection either in the client or the database
* that is not transparently reproducible.
* <p>
* Calls to {@code beginRequest} and {@code endRequest} are not nested. Multiple calls to {@code beginRequest} without an intervening call to
* {@code endRequest} is not an error. The first {@code beginRequest} call marks the start of the request and subsequent calls are treated as a
* no-op It is recommended to enclose each unit of work in {@code beginRequest}, {@code endRequest} pairs such that there is no open transaction
* at the beginning or end of the request and no dependency on local state that crosses request boundaries. Committed database state is not local.
*
* This method is to be used by Connection pooling managers.
* <p>
* The pooling manager should call {@code beginRequest} on the underlying connection prior to returning a connection to the caller.
* <p>
*
* @throws SQLException
* if an error occurs
* @see endRequest
*/
public void beginRequest() throws SQLException {
beginRequestInternal();
}

/**
* Hints to the driver that a request, an independent unit of work, has completed. It rolls back the open transactions. Resets the connection
* properties that are modifiable through public methods back to their original values. Calls to {@code beginRequest} and {@code endRequest} are
* not nested. Multiple calls to {@code endRequest} without an intervening call to {@code beginRequest} is not an error. The first
* {@code endRequest} call marks the request completed and subsequent calls are treated as a no-op. If {@code endRequest} is called without an
* initial call to {@code beginRequest} is a no-op. This method is to be used by Connection pooling managers.
* <p>
*
* @throws SQLException
* if an error occurs
* @see beginRequest
*/
public void endRequest() throws SQLException {
endRequestInternal();
}

public void setShardingKey(ShardingKey shardingKey) throws SQLServerException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLServerException("setShardingKey not implemented", new SQLFeatureNotSupportedException("setShardingKey not implemented"));
Expand All @@ -17,20 +62,22 @@ public void setShardingKey(ShardingKey shardingKey) throws SQLServerException {
public void setShardingKey(ShardingKey shardingKey,
ShardingKey superShardingKey) throws SQLServerException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLServerException("setShardingKey not implemented", new SQLFeatureNotSupportedException("setShardingKey not implemented")) ;
throw new SQLServerException("setShardingKey not implemented", new SQLFeatureNotSupportedException("setShardingKey not implemented"));
}

public boolean setShardingKeyIfValid(ShardingKey shardingKey,
int timeout) throws SQLServerException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLServerException("setShardingKeyIfValid not implemented", new SQLFeatureNotSupportedException("setShardingKeyIfValid not implemented"));
throw new SQLServerException("setShardingKeyIfValid not implemented",
new SQLFeatureNotSupportedException("setShardingKeyIfValid not implemented"));
}

public boolean setShardingKeyIfValid(ShardingKey shardingKey,
ShardingKey superShardingKey,
int timeout) throws SQLServerException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLServerException("setShardingKeyIfValid not implemented", new SQLFeatureNotSupportedException("setShardingKeyIfValid not implemented"));
throw new SQLServerException("setShardingKeyIfValid not implemented",
new SQLFeatureNotSupportedException("setShardingKeyIfValid not implemented"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,14 @@ void closeInternal() {
// Regardless what happens when cleaning up,
// the statement is considered closed.
assert !bIsClosed;

discardLastExecutionResults();

bIsClosed = true;
autoGeneratedKeys = null;
sqlWarnings = null;
inOutParam = null;

connection.removeOpenStatement(this);
}

public void close() throws SQLServerException {
Expand Down
Loading