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
pub mod ora_dao; use r2d2_oracle::OracleConnectionManager; use r2d2::{Pool, PooledConnection}; use parking_lot::{ RwLock}; use std::time::Duration; use unidb::get_db_properties; #[macro_use] extern crate lazy_static; #[macro_use] extern crate log; lazy_static!{ static ref ORACLE_POOL: RwLock<Pool<OracleConnectionManager>> = { RwLock::new(create_oracle_connection_manager()) }; } pub fn connect_db() -> Result<PooledConnection <OracleConnectionManager>, r2d2::Error> { let conn = ORACLE_POOL.read(); return match conn.get() { Ok(t) => Ok(t), Err(e) => { error!("==> {:?}", e); drop(conn); let mut conn = ORACLE_POOL.write(); *conn = create_oracle_connection_manager(); conn.get() } }; } pub fn create_oracle_connection_manager() -> Pool<OracleConnectionManager> { let properties = get_db_properties(); let username = match properties.get("db.oracle.username") { Some(v) => v, _ => panic!("No such property: db.oracle.username"), }; let password = match properties.get("db.oracle.password") { Some(v) => v, _ => panic!("No such property: db.oracle.password"), }; let database = match properties.get("db.oracle.database") { Some(v) => v, _ => panic!("No such property: db.oracle.database"), }; let pool_size = match properties.get("db.oracle.pool_size") { Some(t) => t.parse::<u32>().unwrap(), None => panic!("No such property: db.oracle.pool_size") }; let idle_timeout_sec = match properties.get("db.oracle.idle_timeout_sec") { Some(t) => t.parse::<u64>().unwrap_or(15), None => 15, }; let manager = OracleConnectionManager::new(username, password, database); let pool = r2d2::Pool::builder() .idle_timeout(Some(Duration::from_secs(idle_timeout_sec))) .max_size(pool_size) .build(manager) .unwrap(); pool } #[cfg(test)] mod tests { #[test] fn it_works() { assert_eq!(2 + 2, 4); } }