r2d2 icon indicating copy to clipboard operation
r2d2 copied to clipboard

Test connection before initializing pool

Open Pzixel opened this issue 4 years ago • 0 comments

Consider following code

let postgres_manager = ConnectionManager::<PgConnection>::new("postgre://bac");
let pool = Pool::new(postgres_manager)?;

Currently it produces following output:

2020-05-13T16:30:07.777244900+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
...

It returns Err in the end, but it takes time and unnessessary requests to db.

I think probably it should check if connection is ok before initializing pool. Something like:

    pub fn new(manager: M) -> Result<Pool<M>, Error> {
        let _test_connecction = manager.connect()?;
        Pool::builder().build(manager)
    }

Pzixel avatar May 13 '20 13:05 Pzixel