我正在尝试为使用Eclipse的数据工具平台的项目设置连接超时。目的是防止等待数据库连接需要太长建立。该项目支持几种数据库类型。
如果该项目使用了plain java.sql,我可以按以下方式设置超时:
final int TIMEOUT_SECONDS = 5;
DriverManager.setLoginTimeout(TIMEOUT_SECONDS);
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
我试图在org.eclipse.datatools.connectionitive类中寻找类似的东西,例如iconnectionProfile。到目前为止,我一直无法找到诸如drivermanager.setlogintimeout的特定方法。
java.sql和org.eclipse.datatools.connectivity支持将属性添加到设置连接信息:
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
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);
使用属性,我能够为大多数数据库设置超时。例如,Oracle支持以毫秒为单位表示的名为oracle.net.connect_timeout的属性,而Microsoft SQL Server suports suports suport suport suports loginTimeout以秒为单位表示。
。我尚未找到Sybase,Teradata和Netezza的值。
问题1:是否有更简单的机制来为数据工具平台设置登录超时?问题2:如果没有更好的解决方案,是否可以设置Sybase,Teradata和Netezza的超时?我可以处理具有不同名称和价值类型的属性。
经过一些研究,我发现创建一个IconnectionProfile实际上使用Java.sql.drivermanager在封面下。如果我在创建连接配置文件之前设置DriverManager.SetLogIntimeout,则可以识别超时。
但是,我还了解到,不能保证所有驱动程序都可以识别setlogintimeout。例如,Informix,Netezza和PostgreSQL并未使用SetLogIntimeout设置的时间。对于我的用例,这些数据库不适用,所以我只使用了setLogIntimeout。
虽然我没有将其用于解决方案,但可以使用属性设置登录超时(请参阅原始帖子(。但是,属性名称和值类型(秒或毫秒(是驱动程序的。
以下是我测试过的单个数据库的结果。
这是我使用使用setLogIntiemout的测试结果,值较低(2秒(,并保证超时的URL:
- db2 udb -works
- DB2 ZOS-作品
- Hive -Works
- Informix-不起作用 - 使用自己的超时(20秒以上(
- netezza-不起作用 - 使用自己的超时(20秒(
- oracle-作品(大约2-3秒慢(
- Postgres-不起作用 - 使用其自己的超时10 sec
- sql_server -works
- Sybase -Works
- Teradata -Works
这是使用属性的测试结果(属性名称和值类型在括号中(:
- db2 udb(logintimeout,seconds( - 作品
- db2 zos(logintimeout,seconds( - 作品
- Hive(logintimeout,seconds( - 作品
- Informix(InformixContime,秒( - 作品
- oracle(oracle.net.connect_timeout,毫秒( - 工作(约2-3秒慢(
- Postgres-未找到财产
- netezza(logintimeout,seconds( - 如果属性在URL而不是单独的属性中,则可以使用。
- sql_server(logintimeout,seconds( - 作品
- Sybase-找不到财产
- Teradata-找不到财产