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
pub mod ora_dao;

use parking_lot::{ RwLock};
use r2d2_oracle::OracleConnectionManager;
use r2d2::{Pool, PooledConnection};
use ora_dao::create_oracle_connection_manager;

#[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 add(left: usize, right: usize) -> usize {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}