1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#![doc(html_root_url="http://outersky.github.io/r2d2-mysql/doc/v0.2.0/r2d2_mysql/")]
#![crate_name="r2d2_mysql"]
#![crate_type="rlib"]
#![crate_type="dylib"]
extern crate mysql;
extern crate rustc_serialize as serialize;
extern crate r2d2;
pub mod param;
pub mod url;
pub mod pool;
pub use pool::MysqlConnectionManager;
pub use param::connect;
#[cfg(test)]
mod test {
use r2d2;
use std::sync::Arc;
use std::thread;
use super::{MysqlConnectionManager};
const DB_URL : &'static str = "mysql://root:12345678@localhost:3306/test";
#[test]
fn query_pool(){
let config = r2d2::config::Builder::new().pool_size(30).build();
let manager = MysqlConnectionManager::new(DB_URL).unwrap();
let pool = Arc::new(r2d2::Pool::new(config, manager).unwrap());
let mut tasks = vec![];
for _ in 0..3 {
let pool = pool.clone();
let th = thread::spawn(move || {
let mut conn = pool.get().map_err(|err| println!("get connection from pool error in line:{} ! error: {:?}", line!(), err) ).unwrap();
conn.query("select user()").map_err(|err| println!("execute query error in line:{} ! error: {:?}", line!(), err) ).unwrap();
});
tasks.push(th);
}
for th in tasks {
let _ = th.join();
}
}
}