我正在尝试按照此处的教程mysql
使用连接池连接到数据库:apache
dbcp2
返回连接的我的数据库连接类如下所示:
public class DbConnection {
private static interface Singleton {
final DbConnection INSTANCE = new DbConnection();
}
private final PoolingDataSource<PoolableConnection> dataSource;
private DbConnection() {
// A ConnectionFactory that the pool will use to create Connections.
DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, pass); //url,user,pass for db connnection
//implement the pooling functionality.
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxWaitMillis(500);
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMinIdle(5);
//PoolingDataSource expect an ObjectPool
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
// Set the poolableConnectionFactory's pool property to the owning pool
poolableConnectionFactory.setPool(connectionPool);
this.dataSource = new PoolingDataSource<>(connectionPool);
}
public static Connection getCon() throws SQLException {
return Singleton.INSTANCE.dataSource.getConnection();
}
}
我正在使用 apache jmeter 来测试连接并从我的 mysql 数据库返回一些东西。我创建了 100 个用户,启动时间(以秒为单位)为 2 秒。我创建了一个Http request,当我试图在其中查看我的响应时,view results tree我成功地获得了前 20 个请求的响应。来自(21 到 100)的后续请求有空白响应。我经历了许多涉及的问题:
java.sql.SQLException:无法获取连接,池错误等待空闲对象超时
森林海
相关分类