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 14 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;

}
140 changes: 131 additions & 9 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.sql.Statement;
import java.sql.Struct;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -3215,6 +3216,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 @@ -3238,7 +3242,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 @@ -3261,7 +3267,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 @@ -3285,7 +3293,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 @@ -4650,6 +4660,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 @@ -4712,7 +4725,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 @@ -4748,7 +4763,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 @@ -5300,14 +5317,99 @@ public <T> T unwrap(Class<T> iface) throws SQLException {
return t;
}

public void beginRequest() throws SQLFeatureNotSupportedException {
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 = null;
private volatile SQLWarning originalSqlWarnings = null;
private List<Statement> openStatements = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need to assign false or null randomly to these variables - primitives like boolean will initialize to false by default, and objects will initialize to null by default. I'm not sure if this was intentional, but we could probably clean this up.

Copy link
Contributor

Choose a reason for hiding this comment

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

i like how you put the variables here (member variables usually go to the top, but I see that this class already contains instances where they put the relevant member variables near the relevant methods, so this is probably the right way to organize things. I'm assuming this was intentional)


protected void beginRequestInternal() throws SQLException {
DriverJDBCVersion.checkSupportsJDBC43();
Copy link
Member

Choose a reason for hiding this comment

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

Please remove unnecessary checks in both internal methods - need not check for checksSupportsJDBC43 anymore since the public APIs will be available only with SQLServerConnection43 class object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason why I added the check was to prevent 4.2 APIs from calling these methods. I guess we can remove it too.

throw new SQLFeatureNotSupportedException("beginRequest not implemented");
synchronized (this) {
if (!requestStarted) {
originalDatabaseAutoCommitMode = databaseAutoCommitMode;
originalTransactionIsolationLevel = transactionIsolationLevel;
originalNetworkTimeout = getNetworkTimeout();
originalHoldability = holdability;
originalSendTimeAsDatetime = sendTimeAsDatetime;
originalStatementPoolingCacheSize = statementPoolingCacheSize;
originalDisableStatementPooling = disableStatementPooling;
originalServerPreparedStatementDiscardThreshold = serverPreparedStatementDiscardThreshold;
originalEnablePrepareOnFirstPreparedStatementCall = enablePrepareOnFirstPreparedStatementCall;
originalSCatalog = sCatalog;
originalSqlWarnings = sqlWarnings;
openStatements = new ArrayList<Statement>();
Copy link
Member

Choose a reason for hiding this comment

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

There's a lot more adding/removing compared to getting/setting. Using a LinkedList would yield better performance? The try-with-resources list removal behavior will still remain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed.

requestStarted = true;
}
}
}

public void endRequest() throws SQLFeatureNotSupportedException {
protected void endRequestInternal() throws SQLException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLFeatureNotSupportedException("endRequest not implemented");
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 (serverPreparedStatementDiscardThreshold != originalServerPreparedStatementDiscardThreshold) {
if (0 > originalServerPreparedStatementDiscardThreshold) {
setServerPreparedStatementDiscardThreshold(DEFAULT_SERVER_PREPARED_STATEMENT_DISCARD_THRESHOLD);
}
else {
setServerPreparedStatementDiscardThreshold(originalServerPreparedStatementDiscardThreshold);
}
}
if (enablePrepareOnFirstPreparedStatementCall != originalEnablePrepareOnFirstPreparedStatementCall) {
if (null == originalEnablePrepareOnFirstPreparedStatementCall) {
setEnablePrepareOnFirstPreparedStatementCall(DEFAULT_ENABLE_PREPARE_ON_FIRST_PREPARED_STATEMENT_CALL);
Copy link
Member

@cheenamalhotra cheenamalhotra Jun 5, 2018

Choose a reason for hiding this comment

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

Can we ignore false change by using getter directly for setting original value and reading new value as well?

Also why are we comparing null with original value - why do we care? The driver anyway calls getter() to retrive the property value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we ignore false change by using getter directly for setting original value and reading new value as well? - Yes.

Also why are we comparing null with original value - why do we care? The driver anyway calls getter() to retrive the property value. - boolean cannot be null.

Copy link
Member

@cheenamalhotra cheenamalhotra Jun 5, 2018

Choose a reason for hiding this comment

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

Well it's null by default, I guess that's why you're checking for null for original value, but if you always use getter() for original value - it will never be null and this check won't be needed. Also we don't even need to reset it explicitly to its default value - just store Original back if new value is different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True.

}
else {
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 @@ -5883,6 +5985,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,7 +631,9 @@ void closeInternal() {
// Regardless what happens when cleaning up,
// the statement is considered closed.
assert !bIsClosed;


connection.removeOpenStatement(this);
Copy link
Member

Choose a reason for hiding this comment

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

This should be done right at the end of Statement.close() just before logging to avoid losing references of statements if they were not closed properly (failure possible in discardLastExecutionResults) due to any reason in a pooled connection scenario. It could lead into losing references/memory leakage of unclosed statements/resultsets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed.


discardLastExecutionResults();

bIsClosed = true;
Expand Down
Loading