简体   繁体   中英

how to set Connection Timeout with jndi?

i have this code:

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;
}

how can i set connection timeout ? (without using context.xml )

According to your code you are casting the this.jdbcFactory to the javax.sql.DataSource ; according to the code this interface extends javax.sql.CommonDataSource which defines the following method javax.sql.CommonDataSource#setLoginTimeout

This method documentation states that it:

 * <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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