Skip to content

Commit

Permalink
Simplify traceID creation in DataSource and PooledConnection. (#1747)
Browse files Browse the repository at this point in the history
This will also make sure the traceID is set before used in toString().
  • Loading branch information
ecki authored Feb 11, 2022
1 parent deb6f94 commit 5f51463
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
51 changes: 24 additions & 27 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ public class SQLServerDataSource
*/
public SQLServerDataSource() {
connectionProps = new Properties();
int dataSourceID = nextDataSourceID();
String nameL = getClass().getName();
traceID = nameL.substring(1 + nameL.lastIndexOf('.')) + ":" + dataSourceID;
loggingClassName = "com.microsoft.sqlserver.jdbc." + nameL.substring(1 + nameL.lastIndexOf('.')) + ":"
+ dataSourceID;
traceID = getClass().getSimpleName() + ':' + nextDataSourceID();
loggingClassName = "com.microsoft.sqlserver.jdbc." + traceID;
}

String getClassNameLogging() {
Expand Down Expand Up @@ -176,7 +173,7 @@ public String getApplicationName() {

/**
* Sets the the database to connect to.
*
*
* @param databaseName
* if not set, returns the default value of null.
*/
Expand All @@ -192,7 +189,7 @@ public String getDatabaseName() {

/**
* Sets the the SQL Server instance name to connect to.
*
*
* @param instanceName
* if not set, returns the default value of null.
*/
Expand Down Expand Up @@ -390,7 +387,7 @@ public String getHostNameInCertificate() {

/**
* Sets the lock timeout value.
*
*
* @param lockTimeout
* the number of milliseconds to wait before the database reports a lock timeout. The default value of -1
* means wait forever. If specified, this value will be the default for all statements on the connection.
Expand All @@ -409,7 +406,7 @@ public int getLockTimeout() {

/**
* Sets the password that will be used when connecting to SQL Server.
*
*
* @param password
* Note getPassword is deliberately declared non-public for security reasons. If the password is not set,
* getPassword returns the default value of null.
Expand All @@ -425,7 +422,7 @@ String getPassword() {

/**
* Sets the TCP-IP port number used when opening a socket connection to SQL Server.
*
*
* @param portNumber
* if not set, getPortNumber returns the default of 1433. Note as mentioned above, setPortNumber does not do
* any range checking on the port value passed in,\ invalid port numbers like 99999 can be passed in without
Expand All @@ -444,7 +441,7 @@ public int getPortNumber() {

/**
* Sets the default cursor type used for the result set.
*
*
* @param selectMethod
* This(non-Javadoc) @see com.microsoft.sqlserver.jdbc.ISQLServerDataSource#setSelectMethod(java.lang.String)
* property is useful when you are dealing with large result sets and do not want to store the whole result
Expand Down Expand Up @@ -534,7 +531,7 @@ public boolean getDelayLoadingLobs() {

/**
* Sets whether string parameters are sent to the server in UNICODE format.
*
*
* @param sendStringParametersAsUnicode
* if true (default), string parameters are sent to the server in UNICODE format. if false, string parameters
* are sent to the server in the native TDS collation format of the database, not in UNICODE. if set, returns
Expand Down Expand Up @@ -567,7 +564,7 @@ public boolean getServerNameAsACE() {

/**
* Sets the host name of the target SQL Server.
*
*
* @param serverName
* if not set, returns the default value of null is returned.
*/
Expand All @@ -583,7 +580,7 @@ public String getServerName() {

/**
* Sets the realm for Kerberos authentication.
*
*
* @param realm
* realm
*/
Expand All @@ -600,7 +597,7 @@ public String getRealm() {
/**
* Sets the Service Principal Name (SPN) of the target SQL Server.
* https://msdn.microsoft.com/en-us/library/cc280459.aspx
*
*
* @param serverSpn
* service principal name
*/
Expand All @@ -616,7 +613,7 @@ public String getServerSpn() {

/**
* Sets the fail over partner of the target SQL Server.
*
*
* @param serverName
* if not set, returns the default value of null.
*/
Expand Down Expand Up @@ -644,7 +641,7 @@ public boolean getMultiSubnetFailover() {

/**
* Sets the user name that will be used when connecting to SQL Server.
*
*
* @param user
* if not set, returns the default value of null.
*/
Expand All @@ -660,7 +657,7 @@ public String getUser() {

/**
* Sets the name of the client machine (or client workstation).
*
*
* @param workstationID
* host name of the client. if not set, the default value is constructed by calling
* InetAddress.getLocalHost().getHostName() or if getHostName() returns blank then
Expand All @@ -687,7 +684,7 @@ public String getWorkstationID() {

/**
* Sets whether the driver will convert SQL states to XOPEN compliant states.
*
*
* @param xopenStates
* if true, the driver will convert SQL states to XOPEN compliant states. The default is false which causes
* the driver to generate SQL 99 state codes. If not set, getXopenStates returns the default value of false.
Expand Down Expand Up @@ -775,7 +772,7 @@ public String getTrustManagerConstructorArg() {

/**
* Sets the datasource URL.
*
*
* @param url
* The URL property is exposed for backwards compatibility reasons. Also, several Java Application servers
* expect a setURL function on the DataSource and set it by default (JBoss and WebLogic) Note for security
Expand Down Expand Up @@ -828,7 +825,7 @@ public String getDescription() {

/**
* Sets the packet size.
*
*
* @param packetSize
* the size (in bytes) to use for the TCP/IP send and receive buffer. It is also the value used for the TDS
* packet size (SQL Server Network Packet Size). Validity of the value is checked at connect time. If no
Expand Down Expand Up @@ -1130,7 +1127,7 @@ public int getConnectRetryInterval() {

/**
* Sets a property string value.
*
*
* @param props
* @param propKey
* @param propValue
Expand All @@ -1150,7 +1147,7 @@ private void setStringProperty(Properties props, String propKey, String propValu

/**
* Returns a property value in String format.
*
*
* @param props
* @param propKey
* @param defaultValue
Expand All @@ -1171,7 +1168,7 @@ private String getStringProperty(Properties props, String propKey, String defaul

/**
* Sets an integer property value.
*
*
* @param props
* @param propKey
* @param propValue
Expand Down Expand Up @@ -1371,7 +1368,7 @@ Reference getReferenceInternal(String dataSourceClassString) {

/**
* Initializes the datasource from properties found inside the reference
*
*
* @param ref
* Called by SQLServerDataSourceObjectFactory to initialize new DataSource instance.
*/
Expand Down Expand Up @@ -1429,7 +1426,7 @@ private static int nextDataSourceID() {

/**
* writeReplace
*
*
* @return serialization proxy
* @throws java.io.ObjectStreamException
* if error
Expand All @@ -1441,7 +1438,7 @@ private Object writeReplace() throws java.io.ObjectStreamException {
/**
* For added security/robustness, the only way to rehydrate a serialized SQLServerDataSource is to use a
* SerializationProxy. Direct use of readObject() is not supported.
*
*
* @param stream
* input stream object
* @throws java.io.InvalidObjectException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class SQLServerPooledConnection implements PooledConnection, Serializable

SQLServerPooledConnection(SQLServerDataSource ds, String user, String password) throws SQLException {
listeners = new Vector<>();
traceID = getClass().getSimpleName() + ':' + nextPooledConnectionID();
// Piggyback SQLServerDataSource logger for now.
pcLogger = SQLServerDataSource.dsLogger;

Expand All @@ -67,16 +68,14 @@ public class SQLServerPooledConnection implements PooledConnection, Serializable
pcLogger.finer(toString() + " Start create new connection for pool.");

physicalConnection = createNewConnection();
String nameL = getClass().getName();
traceID = nameL.substring(1 + nameL.lastIndexOf('.')) + ":" + nextPooledConnectionID();
if (pcLogger.isLoggable(Level.FINE))
pcLogger.fine(toString() + " created by (" + ds.toString() + ")" + " Physical connection " + safeCID()
+ ", End create new connection for pool");
}

/**
* Provides a helper function to provide an ID string suitable for tracing.
*
*
* @return traceID String
*/
@Override
Expand All @@ -86,7 +85,7 @@ public String toString() {

/**
* Helper function to create a new connection for the pool.
*
*
* @return SQLServerConnection instance
* @throws SQLException
*/
Expand All @@ -96,7 +95,7 @@ private SQLServerConnection createNewConnection() throws SQLException {

/**
* Returns an object handle for the physical connection that this PooledConnection object represents.
*
*
* @throws SQLException
* when an error occurs
* @return a Connection object that is a handle to this PooledConnection object
Expand Down

0 comments on commit 5f51463

Please sign in to comment.