Skip to content

Commit 5f51463

Browse files
authored
Simplify traceID creation in DataSource and PooledConnection. (#1747)
This will also make sure the traceID is set before used in toString().
1 parent deb6f94 commit 5f51463

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,8 @@ public class SQLServerDataSource
7676
*/
7777
public SQLServerDataSource() {
7878
connectionProps = new Properties();
79-
int dataSourceID = nextDataSourceID();
80-
String nameL = getClass().getName();
81-
traceID = nameL.substring(1 + nameL.lastIndexOf('.')) + ":" + dataSourceID;
82-
loggingClassName = "com.microsoft.sqlserver.jdbc." + nameL.substring(1 + nameL.lastIndexOf('.')) + ":"
83-
+ dataSourceID;
79+
traceID = getClass().getSimpleName() + ':' + nextDataSourceID();
80+
loggingClassName = "com.microsoft.sqlserver.jdbc." + traceID;
8481
}
8582

8683
String getClassNameLogging() {
@@ -176,7 +173,7 @@ public String getApplicationName() {
176173

177174
/**
178175
* Sets the the database to connect to.
179-
*
176+
*
180177
* @param databaseName
181178
* if not set, returns the default value of null.
182179
*/
@@ -192,7 +189,7 @@ public String getDatabaseName() {
192189

193190
/**
194191
* Sets the the SQL Server instance name to connect to.
195-
*
192+
*
196193
* @param instanceName
197194
* if not set, returns the default value of null.
198195
*/
@@ -390,7 +387,7 @@ public String getHostNameInCertificate() {
390387

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

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

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

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

535532
/**
536533
* Sets whether string parameters are sent to the server in UNICODE format.
537-
*
534+
*
538535
* @param sendStringParametersAsUnicode
539536
* if true (default), string parameters are sent to the server in UNICODE format. if false, string parameters
540537
* are sent to the server in the native TDS collation format of the database, not in UNICODE. if set, returns
@@ -567,7 +564,7 @@ public boolean getServerNameAsACE() {
567564

568565
/**
569566
* Sets the host name of the target SQL Server.
570-
*
567+
*
571568
* @param serverName
572569
* if not set, returns the default value of null is returned.
573570
*/
@@ -583,7 +580,7 @@ public String getServerName() {
583580

584581
/**
585582
* Sets the realm for Kerberos authentication.
586-
*
583+
*
587584
* @param realm
588585
* realm
589586
*/
@@ -600,7 +597,7 @@ public String getRealm() {
600597
/**
601598
* Sets the Service Principal Name (SPN) of the target SQL Server.
602599
* https://msdn.microsoft.com/en-us/library/cc280459.aspx
603-
*
600+
*
604601
* @param serverSpn
605602
* service principal name
606603
*/
@@ -616,7 +613,7 @@ public String getServerSpn() {
616613

617614
/**
618615
* Sets the fail over partner of the target SQL Server.
619-
*
616+
*
620617
* @param serverName
621618
* if not set, returns the default value of null.
622619
*/
@@ -644,7 +641,7 @@ public boolean getMultiSubnetFailover() {
644641

645642
/**
646643
* Sets the user name that will be used when connecting to SQL Server.
647-
*
644+
*
648645
* @param user
649646
* if not set, returns the default value of null.
650647
*/
@@ -660,7 +657,7 @@ public String getUser() {
660657

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

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

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

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

11311128
/**
11321129
* Sets a property string value.
1133-
*
1130+
*
11341131
* @param props
11351132
* @param propKey
11361133
* @param propValue
@@ -1150,7 +1147,7 @@ private void setStringProperty(Properties props, String propKey, String propValu
11501147

11511148
/**
11521149
* Returns a property value in String format.
1153-
*
1150+
*
11541151
* @param props
11551152
* @param propKey
11561153
* @param defaultValue
@@ -1171,7 +1168,7 @@ private String getStringProperty(Properties props, String propKey, String defaul
11711168

11721169
/**
11731170
* Sets an integer property value.
1174-
*
1171+
*
11751172
* @param props
11761173
* @param propKey
11771174
* @param propValue
@@ -1371,7 +1368,7 @@ Reference getReferenceInternal(String dataSourceClassString) {
13711368

13721369
/**
13731370
* Initializes the datasource from properties found inside the reference
1374-
*
1371+
*
13751372
* @param ref
13761373
* Called by SQLServerDataSourceObjectFactory to initialize new DataSource instance.
13771374
*/
@@ -1429,7 +1426,7 @@ private static int nextDataSourceID() {
14291426

14301427
/**
14311428
* writeReplace
1432-
*
1429+
*
14331430
* @return serialization proxy
14341431
* @throws java.io.ObjectStreamException
14351432
* if error
@@ -1441,7 +1438,7 @@ private Object writeReplace() throws java.io.ObjectStreamException {
14411438
/**
14421439
* For added security/robustness, the only way to rehydrate a serialized SQLServerDataSource is to use a
14431440
* SerializationProxy. Direct use of readObject() is not supported.
1444-
*
1441+
*
14451442
* @param stream
14461443
* input stream object
14471444
* @throws java.io.InvalidObjectException

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPooledConnection.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class SQLServerPooledConnection implements PooledConnection, Serializable
5555

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

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

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

7776
/**
7877
* Provides a helper function to provide an ID string suitable for tracing.
79-
*
78+
*
8079
* @return traceID String
8180
*/
8281
@Override
@@ -86,7 +85,7 @@ public String toString() {
8685

8786
/**
8887
* Helper function to create a new connection for the pool.
89-
*
88+
*
9089
* @return SQLServerConnection instance
9190
* @throws SQLException
9291
*/
@@ -96,7 +95,7 @@ private SQLServerConnection createNewConnection() throws SQLException {
9695

9796
/**
9897
* Returns an object handle for the physical connection that this PooledConnection object represents.
99-
*
98+
*
10099
* @throws SQLException
101100
* when an error occurs
102101
* @return a Connection object that is a handle to this PooledConnection object

0 commit comments

Comments
 (0)