sfackler / r2d2

A generic connection pool for Rust
Apache License 2.0
1.49k stars 80 forks source link

Why example use `let pool = pool.clone()` in a for loop ? #124

Closed linrongbin16 closed 2 years ago

linrongbin16 commented 2 years ago

I want to use r2d2 with diesel for connecting mysql and sqlite3.

In example code:

use std::thread;

extern crate r2d2;
extern crate r2d2_foodb;

fn main() {
    let manager = r2d2_foodb::FooConnectionManager::new("localhost:1234");
    let pool = r2d2::Pool::builder()
        .max_size(15)
        .build(manager)
        .unwrap();

    for _ in 0..20 {
        let pool = pool.clone();
        thread::spawn(move || {
            let conn = pool.get().unwrap();
            // use the connection
            // it will be returned to the pool when it falls out of scope.
        })
    }
}

I don't get the meaning of the pool.clone, can I simply remove this line ?

sfackler commented 2 years ago

What happens when you try to remove the line?

linrongbin16 commented 2 years ago

Hi, I got the answer here: https://stackoverflow.com/questions/66400432/why-is-the-connection-pool-cloned-here.