在 ormlite ConnectionSource 中创建连接时如何设置超时?

How to set timeout while creating connection in ormlite ConnectionSource?

连接是通过以下方法创建的

import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
ConnectionSource connectionSource =
    new JdbcConnectionSource(url + databaseName + "?currentSchema=trial&useSSL=false",
            userName, password);

How to set timeout while creating connection in ormlite ConnectionSource?

很遗憾,目前无法设置连接超时。我建议使用数据库连接池来为您提供此功能。您可以使用 Apache's DBCP, HikariCP, or others.

引用 pooled connection sources 上的文档:

There are many other, external data sources that can be used instead, including more robust and probably higher-performance pooled connection managers. You can instantiate your own directly and wrap it in the DataSourceConnectionSource class which delegates to it.

// basic Apache data source
BasicDataSource dataSource = new BasicDataSource();
String databaseUrl = "jdbc:h2:mem:account";
dataSource.setUrl(databaseUrl);
// we wrap it in the DataSourceConnectionSource
ConnectionSource connectionSource = 
   new DataSourceConnectionSource(dataSource, databaseUrl);