-
Notifications
You must be signed in to change notification settings - Fork 427
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 all 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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
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(); | ||
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 (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)) { | ||
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; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -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. | ||
|
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.