问题
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