繁体   English   中英

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

[英]how to set Connection Timeout with 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)

根据您的代码,您正在将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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM