简体   繁体   English

使用Spring和c3p0和MySQL动态设置连接时区

[英]Setting connection timezone dynamically with Spring and c3p0 and MySQL

I want to dynamically set the timezone for a MySQL JDBC connection with c3p0 as connection pool. 我想为c3p0作为连接池动态设置MySQL JDBC连接的时区。 To set time zone for a given MySQL JDBC connection I know I can use "SET time_zone = '+02:00';", but if I set this on a connection I get from the pool I assume the connection will have this time zone when it is being given back to the pool and given to a new request witch request a connection?!? 要为给定的MySQL JDBC连接设置时区,我知道我可以使用“ SET time_zone ='+02:00';”,但是如果我在从池中获取的连接上设置此时区,则认为该连接将具有该时区当它被送回泳池并被赋予新的请求时,女巫请求连接?!? Have I understood it correct? 我了解正确吗?

If yes, how can I set dynamically in a way that the pooled connection is having its timezone reverted to original one (in my case I use UTC as default on MySQL and in the JCM through TimeZone.setDefault(UTC) during startup of my spring context). 如果是,如何动态设置池连接的时区为原来的时区(在我的情况下,我在弹簧启动过程中通过TimeZone.setDefault(UTC)在MySQL和JCM中使用UTC作为默认值)上下文)。

Here is my code snippet from my (Report)Dao 这是我的(Report)Dao中的代码段

public TrafficDuringDayGroupedByPhoneEvent countAllSummariesForTimeReport(Integer childId, Date start, Date end, MessageType type, TimeZone timezone) {


    Integer[] res = {0, 0, 0, 0};
    Session session = getSession();

    //SET time_zone = '+02:00';
    String sql = "select count(calls.d), d from (SELECT  start_time, \n"
            + "(case when hour(start_time) >= 6 and  hour(start_time) < 9 then 'morning'\n"
            + "when hour(start_time) >= 9 and  hour(start_time) < 15 then 'day'\n"
            + "when hour(start_time) >= 15 and  hour(start_time) < 21 then 'aft'\n"
            + "when hour(start_time) >= 21 and  hour(start_time) <= 24 then 'night'\n"
            + "when hour(start_time) < 6 then 'night' end) as d\n" + "FROM bpr_report_event b inner join ";

    ....
}

My question is similar to the one below, except that I want to set it dynamically per connection, not the same timezone as the guy in the question below does. 我的问题与下面的问题类似,不同之处在于我想针对每个连接动态地设置它,而不是与下面的问题中的那个家伙时区相同。

Setting connection timezone with Spring and DBCP and MySQL 使用Spring和DBCP和MySQL设置连接时区

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM