pool static method
- required String host,
- int port = 3306,
- required String database,
- required String username,
- required String password,
- int maxConnections = 4,
- int maxPendingOperations = 32,
- Duration queueTimeout = const Duration(seconds: 10),
- bool useTls = true,
- SecurityContext? tlsContext,
- bool allowPublicKeyRetrieval = false,
- Duration connectTimeout = const Duration(seconds: 10),
- String collation = 'utf8mb4_general_ci',
- int preparedStatementCacheSize = 32,
Creates and owns a lazy MySQL or MariaDB connection pool.
Each connection uses UTC and the same TLS and prepared-statement settings as open. Top-level operations can run concurrently, while one transaction always keeps one connection. Use open when one serialized connection is the cheaper and sufficient choice.
maxConnections defaults to four physical connections.
maxPendingOperations defaults to 32 calls waiting for capacity; a later
call fails with SqlErrorCode.unavailable. queueTimeout defaults to ten
seconds and covers only that queue wait. Once a connection opening starts,
connectTimeout bounds its handshake and session setup instead.
Separate top-level calls may use different physical sessions. Do not send
manual transaction control or rely on a session-level SET across calls;
use transaction for every multi-statement unit of work.
Implementation
static MysqlDatabase pool({
required String host,
int port = 3306,
required String database,
required String username,
required String password,
int maxConnections = 4,
int maxPendingOperations = 32,
Duration queueTimeout = const Duration(seconds: 10),
bool useTls = true,
SecurityContext? tlsContext,
bool allowPublicKeyRetrieval = false,
Duration connectTimeout = const Duration(seconds: 10),
String collation = 'utf8mb4_general_ci',
int preparedStatementCacheSize = 32,
}) {
_validateConnectionOptions(
port: port,
connectTimeout: connectTimeout,
collation: collation,
preparedStatementCacheSize: preparedStatementCacheSize,
);
if (maxConnections < 1) {
throw ArgumentError.value(
maxConnections,
'maxConnections',
'Must be greater than zero.',
);
}
if (maxPendingOperations < 0) {
throw ArgumentError.value(
maxPendingOperations,
'maxPendingOperations',
'Must not be negative.',
);
}
if (queueTimeout.inMilliseconds < 1) {
throw ArgumentError.value(
queueTimeout,
'queueTimeout',
'Must be at least one millisecond.',
);
}
return MysqlDatabase._pool(
_MysqlPool(
maxConnections: maxConnections,
maxPendingOperations: maxPendingOperations,
queueTimeout: queueTimeout,
open: () => _openConnection(
host: host,
port: port,
database: database,
username: username,
password: password,
useTls: useTls,
tlsContext: tlsContext,
allowPublicKeyRetrieval: allowPublicKeyRetrieval,
connectTimeout: connectTimeout,
collation: collation,
preparedStatementCacheSize: preparedStatementCacheSize,
),
),
);
}