New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting time zone on reconnect #582

Closed
Boscop opened this Issue Jan 19, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@Boscop

Boscop commented Jan 19, 2017

I use this code for my postgres connection:

use std::env;

use diesel::pg::PgConnection;
use r2d2_diesel::ConnectionManager;
use r2d2;

lazy_static! {
    static ref CONNECTION: r2d2::Pool<ConnectionManager<PgConnection>> = {
        let database_url = env::var("DATABASE_URL")
            .expect("DATABASE_URL must be set");
        let config = r2d2::Config::default();
        let manager = ConnectionManager::<PgConnection>::new(database_url);
        r2d2::Pool::new(config, manager).expect("Failed to create pool")
    };
}

pub fn connection() -> r2d2::Pool<ConnectionManager<PgConnection>> {
    CONNECTION.clone()
}

And when my server starts, I do this to set the time zone to UTC, because I want to store all times as UTC in a timestamp type (without timezone):

database::connection().get().unwrap().execute("SET TIME ZONE 'UTC';").unwrap();

(Times are converted to the user's time zone to relative times using moment.js on the frontend. Until then, they stay in UTC.)

Since timezone is a per session setting, my question is, what if the connection between postgres and my web server breaks down (e.g. postgres is restarted).
How can I get notified via the ConnectionManager and set the time zone to UTC again? (Since it's a new session and the timezone will be reset to the local time zone.)

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 2, 2017

This is probably a question to ask on the r2d2 repo, but it looks like you can set a connection customizer in the pool's configuration. http://sfackler.github.io/r2d2/doc/v0.7.1/r2d2/config/struct.Builder.html#method.connection_customizer

@sgrif sgrif closed this Feb 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment