Repository: diesel-rs/diesel
File: diesel/src/r2d2.rs (Rust)

Code before:
//!
//! pub fn create_user(conn: &mut PgConnection, user_name: &str) -> Result<usize, Error> {
//!     use schema::users::dsl::*;
//!
//!     diesel::insert_into(users)
//!         .values(name.eq(user_name))
//!         .execute(conn)
//! }
//!
//! pub fn setup_user_table(conn: &mut PgConnection) {
//!     diesel::sql_query("DROP TABLE IF EXISTS users CASCADE").execute(conn).unwrap();
//!     diesel::sql_query("CREATE TABLE users (
//!         id SERIAL PRIMARY KEY,
//!         name VARCHAR NOT NULL
//!     )")
//!         .execute(conn)
//!         .unwrap();
//! }
//!
//! pub fn delete_user_table(conn: &mut PgConnection) {
//!     diesel::sql_query("DROP TABLE IF EXISTS users CASCADE").execute(conn).unwrap();
//! }
//!
//! fn main() {
//!     let pool_size = 1;
//!     let connection_pool = Arc::new(Mutex::new(get_connection_pool(pool_size)));
//!     setup_user_table(&mut connection_pool.lock().unwrap().get().unwrap());
//!
//!     let mut threads = vec![];
//!     let max_users_to_create = 1;
//!
//!     for i in 0..max_users_to_create {
//!         let connection_pool = Arc::clone(&connection_pool);
//!         threads.push(thread::spawn({
//!             move || {
//!                 let connection_pool = connection_pool.lock().unwrap();
//!                 let conn = &mut connection_pool.get().unwrap();
//!                 let name = format!("Person {}", i);
//!                 create_user(conn, &name).unwrap();
//!             }
//!         }))
//!     }
//!
//!     for handle in threads {
//!         handle.join().unwrap();
//!     }
//!
//!     delete_user_table(&mut connection_pool.lock().unwrap().get().unwrap());
//! }
//! ```
//!

Review comment (suggestion):
Now that I look at [the documentation](https://docs.diesel.rs/master/r2d2/index.html#structs) (which doesn't put the pool behind a `Mutex` and `Arc`) I'm not sure this is entirely needed and so don't wanna mislead anyone who uses this module. Will need to revisit.

Code after:
//!     let manager = ConnectionManager::<DbConnection>::new(url);
//!     // Refer to the `r2d2` documentation for more methods to use
//!     // when building a connection pool
//!     Pool::builder()
//! #         .max_size(1)
//!         .test_on_check_out(true)
//! #         .connection_customizer(Box::new(SetupUserTableCustomizer))
//!         .build(manager)
//!         .expect("Could not build connection pool")
//! }
//!
//! pub fn create_user(conn: &mut DbConnection, user_name: &str) -> Result<usize, Error> {
//!     use schema::users::dsl::*;
//!
//!     diesel::insert_into(users)
//!         .values(name.eq(user_name))
//!         .execute(conn)
//! }
//!
//! fn main() {
//!     let pool = get_connection_pool();
//!     let mut threads = vec![];
//!     let max_users_to_create = 1;
//!
//!     for i in 0..max_users_to_create {
//!         let pool = pool.clone();
//!         threads.push(thread::spawn({
//!             move || {
//!                 let conn = &mut pool.get().unwrap();
//!                 let name = format!("Person {}", i);
//!                 create_user(conn, &name).unwrap();
//!             }
//!         }))
//!     }
//!
//!     for handle in threads {
//!         handle.join().unwrap();
//!     }
//! }
//! ```
//!
//! # A note on error handling
//!
//! When used inside a pool, if an individual connection becomes
//! broken (as determined by the [R2D2Connection::is_broken] method)
//! then `r2d2` will close and return the connection to the DB.
//!
//! `diesel` determines broken connections by whether or not the current
//! thread is panicking or if individual `Connection` structs are
//! broken (determined by the `is_broken()` method). Generically, these
//! are left to individual backends to implement themselves.