3

从 Tomcat 数据源获取连接时出现问题。

以下是配置文件:

  1. server.xml
<Resource name="jdbc/mssql" auth="Container"
      type="javax.sql.DataSource" removeAbandoned="true"
          removeAbandonedTimeout="30" maxActive="-1"
          maxIdle="-1" maxWait="10000"      username="xxx" password="xxx"       driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"      url="jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;databaseName=XXX" />
  1. context.xml
<ResourceLink global="jdbc/mssql" name="jdbc/mssql" type="javax.sql.DataSource" />

从我的应用程序代码:

InitialContext initContext = new InitialContext();

if(initContext == null) {
    String message = "There was no context in tomcat server..";
    throw new Exception(message);
}
DataSource datasourceLocal = null;
datasourceLocal = (DataSource)initContext.lookup("java:/comp/env/jdbc/mssql");

if(datasourceLocal == null){
   String message = "Could not find the dataSource in tomcat server..";
   throw new Exception(message);
}
else {
    logger.info("ConnPool Success..");
}

Connection conn = datasourceLocal.getConnection();

我有 2 台数据库服务器(将 IP 替换为 xxx.xxx.xxx.xxx 中的server.xml),它在第 1 台上工作正常,但在第 2 台上,它只是挂在获取连接线上(我在此之前和之后放置了一些跟踪线)。

4

0 回答 0