-
Notifications
You must be signed in to change notification settings - Fork 435
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
Changes from 14 commits
943bcd2
e5bcec7
d4c8bde
0a569bc
00ead56
ffe9b8e
972e93c
8dd0082
20cfcad
3cd8e66
a76d8b1
c48e849
7d695c9
a93642e
9b3c32e
a7b76b8
5c01b32
70f01a6
59d43f2
950bf28
b215898
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
private String originalSCatalog = null; | ||
private volatile SQLWarning originalSqlWarnings = null; | ||
private List<Statement> openStatements = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. - There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check this logic again. This might result in infinite loop. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -631,7 +631,9 @@ void closeInternal() { | |
// Regardless what happens when cleaning up, | ||
// the statement is considered closed. | ||
assert !bIsClosed; | ||
|
||
|
||
connection.removeOpenStatement(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be done right at the end of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. |
||
|
||
discardLastExecutionResults(); | ||
|
||
bIsClosed = true; | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enablePrepareOnFirstPreparedStatementCall
isBoolean
, kept the types same.There was a problem hiding this comment.
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.