文档主页
X DevAPI 用户指南
下载本手册
PDF (US Ltr) - 1.4Mb
PDF (A4) - 1.4Mb


X DevAPI 用户指南  /  ...  /  使用连接池连接到单个 MySQL 服务器

2.2.3 使用连接池连接到单个 MySQL 服务器

X DevAPI 支持连接池,这可以减少需要打开许多到 MySQL 服务器连接的应用程序的开销。连接由客户端对象作为池进行管理。使用客户端打开新会话时,在打开新的网络连接之前,会尝试从池中检索现有且当前未使用的连接,然后重置并重用该连接。

连接池使用单个键值对进行配置(请参阅 使用键值对连接),该键值对包含一个名为 pooling 的键。 pooling 键的值是另一组键值对,其中包含下表中描述的任意键组合

表 2.1 配置连接池的选项

选项 含义 默认值
enabled 启用连接池。当该选项设置为 false 时,将返回常规的非池化连接,并且将忽略下面列出的其他连接池选项。 true
maxSize 池中允许的最大连接数 25
maxIdleTime 连接在队列中允许空闲的最大毫秒数,然后才会关闭。值为零表示无限。 0
queueTimeout 请求允许等待连接变为可用状态的最大毫秒数。值为零表示无限 0

关闭会话会将底层连接标记为未使用,并将其返回到客户端对象的连接池。

关闭客户端对象将关闭其管理的所有连接,使客户端创建的所有会话无效,并销毁托管池。

注意

MySQL Shell 不支持连接池。

Node.js JavaScript 代码

Press CTRL+C to copy
var mysqlx = require('@mysql/xdevapi'); var client = mysqlx.getClient( { user: 'user', host: 'localhost', port: 33060 }, { pooling: { enabled: true, maxIdleTime: 30000, maxSize: 25, queueTimeout: 10000 } } ); client.getSession() .then(session => { console.log(session.inspect()) return session.close() // the connection becomes idle in the client pool }) .then(() => { return client.getSession() }) .then(session => { console.log(session.inspect()) return client.close() // closes all connections and destroys the pool })

C# 代码

Press CTRL+C to copy
using (Client client = MySQLX.GetClient("server=localhost;user=user:port=33060;", new { pooling = new { Enabled = true, MaxSize = 100, MaxIdleTime=30000, QueueTimeout = 10000 } })) { using (Session session = client.GetSession()) { foreach (Collection coll in session.Schema.GetCollections()) { Console.WriteLine(coll.Name); } } // session.Dispose() is called and the session becomes idle in the pool } // client.Dispose() is called then all sessions are closed and pool is destroyed

Python 代码

Press CTRL+C to copy
connection_string = { 'host': 'localhost', 'port': 37210, 'user': 'user', 'password': 'password' } client_options = { 'pooling': { "max_size": 10, "max_idle_time": 30000 } } client = mysqlx.get_client(connection_string, client_options) session1 = client.get_session() session2 = client.get_session() # closing all the sessions client.close()

Java 代码

Press CTRL+C to copy
//Obtain new ClientFactory ClientFactory cf = new ClientFactory(); //Obtain Client from ClientFactory Client cli = cf.getClient(this.baseUrl, "{\"pooling\":{\"enabled\":true, \"maxSize\":8, \"maxIdleTime\":30000, \"queueTimeout\":10000} }"); Session sess = cli.getSession(); //Use Session as usual //Close Client after use cli.close();

C++ 代码

Press CTRL+C to copy
using namespace mysqlx; Client cli("user:password@host_name/db_name", ClientOption::POOL_MAX_SIZE, 7); Session sess = cli.getSession(); // use Session sess as usual cli.close(); // close all Sessions

使用 X DevAPI for C 的 Connector/C++ 代码

Press CTRL+C to copy
char error_buf[255]; int error_code; mysqlx_client_t *cli = mysqlx_get_client_from_url( "user:password@host_name/db_name", "{ \"maxSize\": 7 }", error_buf, &error_code ); mysqlx_session_t *sess = mysqlx_get_session_from_client(cli); // use sess as before mysqlx_close_client(cli); // close session sess