I was able to reproduce your behaviour on Mathematica V9 64-bit under Windows 7. Neither TimeConstrained nor $SQLTimeout would work. However, an explicit "Timeout" option worked for me:
OpenSQLConnection[
JDBC["Microsoft SQL Server(jTDS)", "187.111.111.111/mydb"]
, "Username" -> "myUser"
, "Password" -> "myPass"
, "Timeout" -> 1
]
The evaluation stopped with the message JDBC::error: Login timed out. after one second. Interestingly, this expression apparently made a persistent change to the DatabaseLink state because thereafter any attempt to open the database would time out after one second -- even if I did not specify the "Timeout" option! If I subsequently tried a longer explicit timeout setting, that longer value would then "stick" and be applied to all future connection attempts. It would seem that we are looking at a bug here. I cannot say that I am surprised however. The handling of timeouts and early cancellation of SQL transactions is notoriously unreliable in many, if not most, SQL software stacks. In our context here, we are doubly removed from SQL Server by both JDBC and DatabaseLink.
Incidentally, it also does not surprise me that TimeConstrained fails here. In practice, I find that TimeConstrained sometimes has difficulty interrupting a process that engages in a blocking I/O operation (and the front-end does too).
Update
Further investigation reveals that DatabaseLink is using the Java class java.sql.DriverManager to allocate non-pooled SQL connections. Furthermore, it is setting the loginTimeout property of this class in a persistent fashion. Therefore, we can adjust that property ourselves without actually attempting to open a connection thus:
Needs["JLink`"]
LoadJavaClass["java.sql.DriverManager"];
java`sql`DriverManager`setLoginTimeout[2]
We can query the current setting like this:
java`sql`DriverManager`getLoginTimeout[]
The same investigation also revealed that OpenSQLConnection only uses the $SQLTimeout global variable if we pass an invalid value for the "Timeout" option:
Block[{$SQLTimeout = 1}
, OpenSQLConnection[
JDBC["Microsoft SQL Server(jTDS)","187.111.111.111/mydb"]
, "Username" -> "myUser"
, "Password" -> "myPass"
, "Timeout" -> "invalid"
]
]
We are even informed that the default will be used:
OpenSQLConnection::timeout: Illegal value for Timeout option: invalid (continuing with default value)
All this, and more, can be found if one studies the files found in:
FileNameJoin[{$InstallationDirectory, "SystemFiles", "Links", "DatabaseLink"}] //
SystemOpen
TimeConstrainedin the question title? $\endgroup$$SQLTimeoutvariable is there exactly for that purpose. Can you explain what you have tried and why you think it does not work? $\endgroup$$SQLTimeoutis vague, but could be understand as if$SQLTimeoutdoesn't work for initiating the connection but only for queries. Have you tried that? Maybe this question is also of interest. As mentioned there you might need to change some settings for the network connection on the client or for the connection settings on the sql server. Another possibility would be to build something with the v9 asynchronouse tasks, but I haven't tried that... $\endgroup$