Skip to content

Commit

Permalink
fixing exception messages
Browse files Browse the repository at this point in the history
svn path=/trunk/mcs/; revision=54575
  • Loading branch information
Konstantin Triger committed Dec 18, 2005
1 parent 90696d6 commit 256f9dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,21 +563,14 @@ protected virtual string Port
switch (ProviderType) {
case PROVIDER_TYPE.SQLOLEDB :
if (String.Empty.Equals(port)) {
// if needed - resolve MSSQL port
// FIXME : decide about behaviour in the case all the timeout spent on port resolution
//long start = DateTime.Now.Ticks;
port = DbPortResolver.getMSSqlPort(DataSource,InstanceName,ConnectionTimeout).ToString();
//long end = DateTime.Now.Ticks;
//if( (end - start) < ConnectionTimeout*1000000) {
//timeout -= (int)(end - start)/1000000;
//}
}
// todo : what should we do if all the timeout spent on port resolution ?
if ("-1".Equals(port)) {
string message = String.Format ("Unable to retrieve the port number for {0} using UDP on port 1434. Please see your network administrator to solve this problem or add the port number of your SQL server instance to your connection string (i.e. port=1681).",ServerName);
Exception e = CreateException (message);
throw e;
try {
port = DbPortResolver.getMSSqlPort(this).ToString();
}
catch (SQLException e) {
throw CreateException(e);
}
}

ConnectionStringHelper.AddValue(UserParameters,StringManager.GetStringArray("CON_PORT"),port);
break;
}
Expand Down Expand Up @@ -609,7 +602,7 @@ public override string DataSource
}
}

protected virtual string InstanceName
internal string InstanceName
{
get {
string dataSource = ConnectionStringHelper.FindValue(UserParameters,StringManager.GetStringArray("CON_DATA_SOURCE"));
Expand Down
5 changes: 5 additions & 0 deletions mcs/class/System.Data/System.Data.ProviderBase.jvm/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2005-14-12 Konstantin Triger <kostat@mainsoft.com>

* DbPortResolver.cs, AbstractDBConnection.cs: differentiate between the situation where
don't have a response and where instance name does not exist.

2005-14-12 Konstantin Triger <kostat@mainsoft.com>

* DbPortResolver.cs: fix port resolution for unnamed instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@

namespace System.Data.Common
{
public class DbPortResolver
internal sealed class DbPortResolver
{
public static int getMSSqlPort(String sqlName, String instanceName,int timeout)
public static int getMSSqlPort(AbstractDBConnection connection)
{
String instanceName = connection.InstanceName;
int timeout = connection.ConnectionTimeout;
int port = -1;
try
{
DatagramSocket socket = new DatagramSocket();

// send request
sbyte[] buf = new sbyte[] {2};
InetAddress address = InetAddress.getByName(sqlName);
InetAddress address = InetAddress.getByName(connection.DataSource);
DatagramPacket packet = new DatagramPacket(buf, buf.Length, address, 1434);
socket.send(packet);
sbyte[] recbuf = new sbyte[1024];
Expand All @@ -56,8 +58,7 @@ public static int getMSSqlPort(String sqlName, String instanceName,int timeout)
// try to receive from socket while increasing timeouts in geometric progression
int iterationTimeout = 1;
int totalTimeout = 0;
while (totalTimeout < timeout*1000)
{
for(;;) {
socket.setSoTimeout(iterationTimeout);
try
{
Expand All @@ -68,6 +69,11 @@ public static int getMSSqlPort(String sqlName, String instanceName,int timeout)
{
totalTimeout += iterationTimeout;
iterationTimeout *= 2;
if (totalTimeout >= timeout*1000) {
throw new java.sql.SQLException(
String.Format ("Unable to retrieve the port number for {0} using UDP on port 1434. Please see your network administrator to solve this problem or add the port number of your SQL server instance to your connection string (i.e. port=1433).", connection.DataSource)
);
}
}
}
sbyte[] rcvdSbytes = packet.getData();
Expand Down Expand Up @@ -104,12 +110,19 @@ public static int getMSSqlPort(String sqlName, String instanceName,int timeout)
prev = st.nextToken();
}
socket.close();

if (!instanceReached)
throw new java.sql.SQLException(
String.Format ("Specified SQL Server '{0}\\{1}' not found.", connection.DataSource, instanceName)
);
return port;

}
catch (java.lang.Exception e)
{
return port;
catch (java.sql.SQLException) {
throw;
}
catch (Exception e) {
throw new java.sql.SQLException(e.Message);
}
}

Expand Down

0 comments on commit 256f9dc

Please sign in to comment.