有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用jndi设置连接超时?

我有这个密码:

Object objc = null;

if (jdbcFactory == null) {
  try {
    InitialContext c = new InitialContext();
    this.jdbcFactory = (DataSource) c.lookup(this.jndiName);

  }
  catch (Exception e) {
    Logger log = Logger.getLogger(this.getClass());

  }
}
// jdbcFactory initiated.
try {
  objc = this.jdbcFactory.getConnection();  
} catch (Exception e) {

}
return objc;
}

如何设置连接超时?(不使用context.xml)


共 (1) 个答案

  1. # 1 楼答案

    根据您的代码,您正在将this.jdbcFactory转换为javax.sql.DataSource;根据代码,这个接口扩展了javax.sql.CommonDataSource,它定义了以下方法javax.sql.CommonDataSource#setLoginTimeout

    本方法文件规定:

     * <p>Sets the maximum time in seconds that this data source will wait
     * while attempting to connect to a database.  A value of zero
     * specifies that the timeout is the default system timeout
     * if there is one; otherwise, it specifies that there is no timeout.
     * When a <code>DataSource</code> object is created, the login timeout is
     * initially zero.