pub struct ConnectionPool { /* private fields */ }Expand description
连接池。
Implementations§
Source§impl ConnectionPool
impl ConnectionPool
Sourcepub fn new(
cfg: ClientConfig,
min_connections: usize,
max_connections: usize,
) -> Result<Self>
pub fn new( cfg: ClientConfig, min_connections: usize, max_connections: usize, ) -> Result<Self>
min_connections: 最小空闲连接数;max_connections: 最大连接数。
预热时创建的连接失败会被忽略(服务器未启动时仍可创建池)。
Examples found in repository?
examples/cursor_pool.rs (line 15)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 let cfg = ClientConfig {
7 host: std::env::var("DM_HOST").unwrap_or_else(|_| "127.0.0.1".into()),
8 port: std::env::var("DM_PORT").unwrap_or_else(|_| "5236".into()).parse()?,
9 user: std::env::var("DM_USER").unwrap_or_else(|_| "SYSDBA".into()),
10 password: std::env::var("DM_PWD").unwrap_or_else(|_| "password".into()),
11 ..Default::default()
12 };
13
14 // 连接池(最小 1 / 最大 5)
15 let pool = ConnectionPool::new(cfg, 1, 5)?;
16 {
17 let mut conn = pool.acquire()?;
18 {
19 let mut cur = conn.cursor();
20 cur.execute("select 1", &[])?;
21 println!("description: {:?}", cur.description());
22 while let Some(row) = cur.fetchone()? {
23 println!("row: {:?}", row);
24 }
25 }
26 // 惰性游标:大结果集自动分批拉取
27 {
28 let mut cur: Cursor = conn.cursor();
29 cur.execute("SELECT id FROM proto_test WHERE id >= ?", &[Param::Int(0)])?;
30 let rows = cur.fetchall()?;
31 println!("fetchall {} 行", rows.len());
32 }
33 } // Drop 自动归还连接
34
35 // executemany 批量插入
36 {
37 let mut conn = pool.acquire()?;
38 let batch: Vec<Vec<Param>> = (0..10)
39 .map(|i| vec![Param::Int(i), Param::Str(format!("name-{i}"))])
40 .collect();
41 let mut cur = conn.cursor();
42 let n = cur.executemany("INSERT INTO batch_demo VALUES (?, ?)", &batch)?;
43 println!("executemany 影响行数: {n}");
44 }
45
46 pool.close();
47 println!("✅ 游标/连接池示例结束");
48 Ok(())
49}Sourcepub fn with_lazy_check(self, v: bool) -> Self
pub fn with_lazy_check(self, v: bool) -> Self
开启/关闭惰性保活检查:acquire 跳过 SELECT 1,坏连接延迟到首次使用时
由 auto_reconnect 自动重连(省一次往返;需 ClientConfig.auto_reconnect = true)。
用法:ConnectionPool::new(cfg, 1, 10)?.with_lazy_check(true)
Sourcepub fn acquire(&self) -> Result<PooledConnection<'_>>
pub fn acquire(&self) -> Result<PooledConnection<'_>>
借用一个连接(必要时新建;坏连接自动剔除重连)。
Examples found in repository?
examples/cursor_pool.rs (line 17)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 let cfg = ClientConfig {
7 host: std::env::var("DM_HOST").unwrap_or_else(|_| "127.0.0.1".into()),
8 port: std::env::var("DM_PORT").unwrap_or_else(|_| "5236".into()).parse()?,
9 user: std::env::var("DM_USER").unwrap_or_else(|_| "SYSDBA".into()),
10 password: std::env::var("DM_PWD").unwrap_or_else(|_| "password".into()),
11 ..Default::default()
12 };
13
14 // 连接池(最小 1 / 最大 5)
15 let pool = ConnectionPool::new(cfg, 1, 5)?;
16 {
17 let mut conn = pool.acquire()?;
18 {
19 let mut cur = conn.cursor();
20 cur.execute("select 1", &[])?;
21 println!("description: {:?}", cur.description());
22 while let Some(row) = cur.fetchone()? {
23 println!("row: {:?}", row);
24 }
25 }
26 // 惰性游标:大结果集自动分批拉取
27 {
28 let mut cur: Cursor = conn.cursor();
29 cur.execute("SELECT id FROM proto_test WHERE id >= ?", &[Param::Int(0)])?;
30 let rows = cur.fetchall()?;
31 println!("fetchall {} 行", rows.len());
32 }
33 } // Drop 自动归还连接
34
35 // executemany 批量插入
36 {
37 let mut conn = pool.acquire()?;
38 let batch: Vec<Vec<Param>> = (0..10)
39 .map(|i| vec![Param::Int(i), Param::Str(format!("name-{i}"))])
40 .collect();
41 let mut cur = conn.cursor();
42 let n = cur.executemany("INSERT INTO batch_demo VALUES (?, ?)", &batch)?;
43 println!("executemany 影响行数: {n}");
44 }
45
46 pool.close();
47 println!("✅ 游标/连接池示例结束");
48 Ok(())
49}Sourcepub fn release(&self, conn: Connection)
pub fn release(&self, conn: Connection)
归还连接;连接无效则丢弃。
Sourcepub fn close(&self)
pub fn close(&self)
Examples found in repository?
examples/cursor_pool.rs (line 46)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 let cfg = ClientConfig {
7 host: std::env::var("DM_HOST").unwrap_or_else(|_| "127.0.0.1".into()),
8 port: std::env::var("DM_PORT").unwrap_or_else(|_| "5236".into()).parse()?,
9 user: std::env::var("DM_USER").unwrap_or_else(|_| "SYSDBA".into()),
10 password: std::env::var("DM_PWD").unwrap_or_else(|_| "password".into()),
11 ..Default::default()
12 };
13
14 // 连接池(最小 1 / 最大 5)
15 let pool = ConnectionPool::new(cfg, 1, 5)?;
16 {
17 let mut conn = pool.acquire()?;
18 {
19 let mut cur = conn.cursor();
20 cur.execute("select 1", &[])?;
21 println!("description: {:?}", cur.description());
22 while let Some(row) = cur.fetchone()? {
23 println!("row: {:?}", row);
24 }
25 }
26 // 惰性游标:大结果集自动分批拉取
27 {
28 let mut cur: Cursor = conn.cursor();
29 cur.execute("SELECT id FROM proto_test WHERE id >= ?", &[Param::Int(0)])?;
30 let rows = cur.fetchall()?;
31 println!("fetchall {} 行", rows.len());
32 }
33 } // Drop 自动归还连接
34
35 // executemany 批量插入
36 {
37 let mut conn = pool.acquire()?;
38 let batch: Vec<Vec<Param>> = (0..10)
39 .map(|i| vec![Param::Int(i), Param::Str(format!("name-{i}"))])
40 .collect();
41 let mut cur = conn.cursor();
42 let n = cur.executemany("INSERT INTO batch_demo VALUES (?, ?)", &batch)?;
43 println!("executemany 影响行数: {n}");
44 }
45
46 pool.close();
47 println!("✅ 游标/连接池示例结束");
48 Ok(())
49}pub fn total(&self) -> usize
pub fn idle_count(&self) -> usize
Source§impl ConnectionPool
impl ConnectionPool
包装为 Arc 以便跨线程共享。
Auto Trait Implementations§
impl !Freeze for ConnectionPool
impl RefUnwindSafe for ConnectionPool
impl Send for ConnectionPool
impl Sync for ConnectionPool
impl Unpin for ConnectionPool
impl UnsafeUnpin for ConnectionPool
impl UnwindSafe for ConnectionPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more