简体   繁体   English

如何使用Eclipse的数据工具平台设置登录超时?

[英]How do I set a login timeout using Eclipse's Data Tools Platform?

I'm trying to set a connection timeout for a project that uses Eclipse's Data Tools Platform. 我正在尝试为使用Eclipse的数据工具平台的项目设置连接超时。 The purpose is to prevent waiting if a database connection takes too long to establish. 目的是防止在数据库连接建立时间过长时等待。 The project supports several database types. 该项目支持多种数据库类型。

If the project used plain java.sql, I could set the timeout as follows: 如果项目使用纯java.sql,则可以将超时设置如下:

final int TIMEOUT_SECONDS = 5;
DriverManager.setLoginTimeout(TIMEOUT_SECONDS);
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);

I tried to look for something similar in org.eclipse.datatools.connectivity classes, such as IConnectionProfile. 我试图在org.eclipse.datatools.connectivity类中寻找类似的东西,例如IConnectionProfile。 So far, I have been unable to locate a specific method like DriverManager.setLoginTimeout. 到目前为止,我无法找到特定的方法,例如DriverManager.setLoginTimeout。

Both java.sql and org.eclipse.datatools.connectivity support adding properties to set connection information: java.sql和org.eclipse.datatools.connectivity都支持添加属性以设置连接信息:

java.sql java.sql

Properties properties = new Properties();
properties.setProperty("user", connectionInfo.username);
properties.setProperty("password", connectionInfo.password);
String connectionTimeoutProperty = getConnectionTimeoutProperty();      // depends on the database vendor
String connectionTimeoutValue = getConnectionTimeoutPropertyValue();    // The value in seconds or milliseconds - also depends on the vendor
properties.setProperty(connectionTimeoutProperty, connectionTimeoutValue);
connection = DriverManager.getConnection(jdbcUrl, properties);

org.eclipse.database.connectivity org.eclipse.database.connectivity

Properties properties = new Properties();
// Similar property setup as above.
ProfileManager.getInstance().createProfile(profileName, "Auto Generated", "org.eclipse.datatools.connectivity.db.generic.connectionProfile", properties, "", false);
return ProfileManager.getInstance().getProfileByName(profileName);

Using properties, I've been able to set a timeout for most of the databases. 使用属性,我已经能够为大多数数据库设置超时。 For instance, Oracle supports a property named oracle.net.CONNECT_TIMEOUT expressed in milliseconds, whereas Microsoft SQL Server suports a property named loginTimeout expressed in seconds. 例如,Oracle支持以毫秒表示的名为oracle.net.CONNECT_TIMEOUT的属性,而Microsoft SQL Server支持以秒表示的名为loginTimeout的属性。

I have yet to find values for Sybase, Teradata, and Netezza. 我尚未找到Sybase,Teradata和Netezza的值。

Question 1: Is there a simpler mechanism to set a login timeout for the Data Tools Platform? 问题1:是否有更简单的机制为Data Tools Platform设置登录超时? Question 2: If there is no better solution, is there a way to set the timeout for Sybase, Teradata, and Netezza? 问题2:如果没有更好的解决方案,是否可以为Sybase,Teradata和Netezza设置超时? I can handle properties with different names and value types. 我可以处理具有不同名称和值类型的属性。

After some research, I discovered that creating an IConnectionProfile actually uses java.sql.DriverManager under the covers. 经过一番研究,我发现创建IConnectionProfile实际上是在幕后使用java.sql.DriverManager。 If I set DriverManager.setLoginTimeout before creating the connection profile, the timeout is recognized. 如果在创建连接配置文件之前设置了DriverManager.setLoginTimeout,则可以识别超时。

However, I also learned that setLoginTimeout is not guaranteed to be recognized by all drivers. 但是,我还了解到不能保证setLoginTimeout可以被所有驱动程序识别。 For instance, Informix, Netezza, and PostgreSQL did not use the time set by setLoginTimeout. 例如,Informix,Netezza和PostgreSQL没有使用setLoginTimeout设置的时间。 For my use case, those databases did not apply, so I just used setLoginTimeout. 对于我的用例,那些数据库不适用,因此我只使用了setLoginTimeout。

While I didn't use it for my solution, it is possible to use properties to set the login timeout (see original post). 虽然我没有在解决方案中使用它,但是可以使用属性来设置登录超时(请参阅原始文章)。 However, the property name and value type (seconds or milliseconds) are driver dependent. 但是,属性名称和值类型(秒或毫秒)取决于驱动程序。

Following are the results for individual databases I tested. 以下是我测试的单个数据库的结果。

Here are the results of my tests using using setLoginTimeout with a low value (2 seconds) and a URL guaranteed to timeout: 这是使用具有低值(2秒)和保证超时的URL的setLoginTimeout进行测试的结果:

  • DB2 UDB - works DB2 UDB-作品
  • DB2 zOS - works DB2 zOS-作品
  • HIVE - works HIVE-工程
  • INFORMIX - does not work - uses its own timeout (20+ seconds) INFORMIX-不起作用-使用自己的超时时间(超过20秒)
  • NETEZZA - does not work - uses its own timeout (20+ seconds) NETEZZA-不起作用-使用自己的超时时间(超过20秒)
  • ORACLE - works (about 2-3 seconds slow) ORACLE-工作(慢约2-3秒)
  • POSTGRES - does not work - uses its own timeout 10+ sec POSTGRES-不起作用-使用自己的超时时间10+秒
  • SQL_SERVER - works SQL_SERVER-工作
  • SYBASE - works SYBASE-工程
  • TERADATA - works TERADATA-工程

Here are the test results using properties (the property name and value type are in parentheses): 这是使用属性的测试结果(属性名称和值类型在括号中):

  • DB2 UDB (loginTimeout, seconds) - works DB2 UDB(loginTimeout,秒)-有效
  • DB2 zOS (loginTimeout, seconds) - works DB2 zOS(loginTimeout,秒)-有效
  • HIVE (loginTimeout, seconds) - works HIVE(loginTimeout,秒)-作品
  • INFORMIX (INFORMIXCONTIME, seconds) - works INFORMIX(INFORMIXCONTIME,秒)-有效
  • ORACLE (oracle.net.CONNECT_TIMEOUT, milliseconds) - works (about 2-3 seconds slow) ORACLE(oracle.net.CONNECT_TIMEOUT,毫秒)-有效(慢2-3秒)
  • POSTGRES - no property found POSTGRES-找不到属性
  • NETEZZA (loginTimeout, seconds) - works if the property is in the URL instead of a separate property. NETEZZA(loginTimeout,秒)-如果属性位于URL中而不是单独的属性中,则可以使用。
  • SQL_SERVER (loginTimeout, seconds) - works SQL_SERVER(loginTimeout,秒)-有效
  • SYBASE - no property found SYBASE-找不到属性
  • TERADATA - no property found TERADATA-找不到属性

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

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