Java JDBC Connection using Socks

别来无恙 提交于 2020-12-06 19:21:49

问题


Using Java, I need to connect to a SQL Server database using JDBC. I need to go through our companies SOCKS proxy, so I did this and it appeared to work.

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.

After some testing, it appears changing the properties actually affects the entire JVM. Other connections (LDAP, DB Connections) were also trying to use the SOCKS proxy I defined.

Is there another way of forcing my JDBC connection to go through socks that isn't global?


回答1:


Not the best solution but I ended up creating a distinct JVM that sets the proxy host and port. I'll use that JVM for any functionality that needs to connect to an external database.



来源:https://stackoverflow.com/questions/50880979/java-jdbc-connection-using-socks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!