【问题标题】:how to set Connection Timeout with jndi?如何使用 jndi 设置连接超时?
【发布时间】:2015-09-29 11:24:59
【问题描述】:

我有这个代码:

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 )

【问题讨论】:

  • 不使用 context.xml 意味着你想从代码本身重置?
  • 是的,我想通过代码设置连接超时

标签: java sql jndi connection-pooling


【解决方案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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多