简体   繁体   English

使用Socks的Java JDBC连接

[英]Java JDBC Connection using Socks

Using Java, I need to connect to a SQL Server database using JDBC. 使用Java,我需要使用JDBC连接到SQL Server数据库。 I need to go through our companies SOCKS proxy, so I did this and it appeared to work. 我需要通过我们公司的SOCKS代理,所以我做到了,它似乎起作用了。

Connection conn = null;

Properties systemProperties = System.getProperties();
systemProperties.setProperty("socksProxyHost","socksproxy.domain.com");
systemProperties.setProperty("socksProxyPort","1081");

connectionUrl = "jdbc:sqlserver://1.2.3.4:60304;databaseName=myDatabase;sslProtocol=TLSv1.2;";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(connectionUrl,"MyLogin","MyPassword");

However, I started seeing some odd things. 但是,我开始看到一些奇怪的事情。 The code is running in websphere in a JVM with numerous other applications. 该代码在JVM中的Websphere中与许多其他应用程序一起运行。

After some testing, it appears changing the properties actually affects the entire JVM. 经过一些测试,似乎更改属性实际上会影响整个JVM。 Other connections (LDAP, DB Connections) were also trying to use the SOCKS proxy I defined. 其他连接(LDAP,数据库连接)也在尝试使用我定义的SOCKS代理。

Is there another way of forcing my JDBC connection to go through socks that isn't global? 有没有其他方法可以强制我的JDBC连接通过不全局的袜子?

Not the best solution but I ended up creating a distinct JVM that sets the proxy host and port. 不是最好的解决方案,但是我最终创建了一个用于设置代理主机和端口的独特的JVM。 I'll use that JVM for any functionality that needs to connect to an external database. 我将将该JVM用于需要连接到外部数据库的所有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM