应用中使用DBCP连接池,线下环境部署运行正常,但迁移到云上后就抛连接获取错误。
于是写了个测试
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://jxajyhxt.sqlserver.rds.aliyuncs.com:3433;databasename=jxaj1";
String userName = "**"; // 用户名
String userPwd = "******"; // 密码
ArrayList<Connection> dbConns = new ArrayList<Connection>();;
try {
Class.forName(JDriver).newInstance();
for (int i = 0; i < 10; i++){
System.out.print ("获取连接 ["+i+"]");
Connection dbConn = DriverManager.getConnection (dbURL, userName, userPwd);
System.out.println(dbConn.getMetaData().getURL());
dbConns.add(dbConn);
}
} catch (Exception e) {
e.printStackTrace ();
} finally {
try {
for (int i = 0, len = dbConns.size (); i < len; i++){
System.out.println ("释放连接 ["+i+"]");
Connection dbConn = dbConns.get(i);
if (dbConn != null) dbConn.close();
}
} catch (SQLException e) {
e.printStackTrace ();
}
}
每次都是能获取到一个连接,但在取第二个连接时报错:
------------------------------------------------------------------------------------------------------------------------
获取连接 [0] jdbc:sqlserver://jxajyhxt.sqlserver.rds.aliyuncs.com:3433;responseBuffering=adaptive;encrypt=false;databaseName=jxaj1;selectMethod=direct;trustServerCertificate=false;applicationName=Microsoft SQL Server JDBC Driver;lastUpdateCount=true;
获取连接 [1] com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host R8IC01361, named instance MS3022 has failed. Error: java.net.UnknownHostException: R8IC01361. Please verify the server and instance names, check that no firewall is blocking UDP traffic to port 1434, and for SQL Server 2005 or later, verify that the SQL Server Browser service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3090)
at com.microsoft.sqlserver.jdbc.FailoverInfo.setupInfo(FailOverInfo.java:59)
at com.microsoft.sqlserver.jdbc.FailoverInfo.failoverPermissionCheck(FailOverInfo.java:85)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithFailover(SQLServerConnection.java:812)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:763)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at com.topinfo.jxaj.test.DbTest.getConnectionSqlServer(DbTest.java:28)
at com.topinfo.jxaj.test.DbTest.main(DbTest.java:64)
释放连接 [0]
------------------------------------------------------------------------------------------------
跟连接池报的错误一致。解析出的host: R8IC01361 是机器名还是....
谁能帮助一下,万分感谢!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。