简体   繁体   English

使用Spring和DBCP和MySQL设置连接时区

[英]Setting connection timezone with Spring and DBCP and MySQL

My Enviroment 我的环境

  • Java 5 Java 5
  • Spring 2.5.5 春天2.5.5
  • DBCP DataSource (org.apache.commons.dbcp.BasicDataSource) DBCP DataSource (org.apache.commons.dbcp.BasicDataSource)
  • MySQL MySQL的

Similar posts 类似帖子

Links 链接

My Problem 我的问题

  • I need to set on my connection the timezone, aiming to prevent the conversions when dealing with TIMESTAMP columns. 我需要在我的连接上设置时区,旨在防止处理TIMESTAMP列时的转换。

My Idea/research 我的想法/研究

  • DBCP Connection Pool did not mention anything around timezone. DBCP连接池没有提及时区周围的任何内容。 LINK 链接

  • What I investigate and thought that was oK is described on THIS post, exemplifying is: 我调查,并认为这是确定在描述岗位,示范是:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="URL" value="${database.url}" /> 
    <property name="user" value="${database.username}" /> 
    <property name="password" value="${database.passwd}" /> 
    <property name="connectionCachingEnabled" value="true"/>
    <property name="sessionTimeZone" value="GMT-3"/>
</bean>

Asking for help area :) 求助区:)

  • But this is not working!! 但这不行!
  • What I want here is a simple way, preferentially using Spring to configure the timezone on jdbc connection. 我想要的是一种简单的方法,优先使用Spring在jdbc连接上配置时区。

Thanks in advance for any help/tips/advice/knowledge share 提前感谢任何帮助/提示/建议/知识分享


SOLUTION: 解:

My Solution was based on tips collected on this post! 我的解决方案基于此帖子收集的提示! Thanks for all! 谢谢大家!

(...)
@Override
public Connection getConnection() {
    Connection conn = null;
    Statement statement = null;
    try {
        conn = super.getConnection();
        statement = conn.createStatement();
        statement.execute("SET time_zone = \'" + timezone+"\'");
    } catch (SQLException e) {
        LOG.fatal("Error while SET time_zone", e);
    } finally {
        try {
            statement.close();
        } catch (SQLException e) {
            LOG.warn("Error while closing statement", e);
        }
    }
    if(LOG.isDebugEnabled())
        LOG.debug("SET time_zone("+timezone+") for connection, succeed!");
    return conn;
}
(...)

and on my Spring configuration file: 并在我的Spring配置文件中:

<bean id="dataSource" class="com.my.package.dbcp.TimezoneEnabledDataSource" destroy-method="close">
    (...)
    <property name="timezone" value="${database.timezone}" />
    (...)
</bean>

I hope this post can help someone in the future. 我希望这篇文章可以帮助将来的某个人。 Any question ping me! 有问题ping我!

You should be able to put the same SQL statements in the initConnectionSqls property of the DBCP configuration element. 您应该能够将相同的SQL语句放在DBCP配置元素的initConnectionSqls属性中。 Just add this to the DBCP configuration element 只需将其添加到DBCP配置元素即可

<property name="initConnectionSqls" value="SET time_zone = '${database.timezone}'"/>

Depending on your version of DBCP, you may have to use connectionInitSqls as the property name. 根据您的DBCP版本,您可能必须使用connectionInitSqls作为属性名称。 This information is straight from DBCP configuration documentation. 此信息直接来自DBCP配置文档。

If the data source doesn't have such a property, you can extend it and add that property: 如果数据源没有这样的属性,您可以扩展它并添加该属性:

public TimezoneEnabledDataSource extends BasicDataSource {
    private String timezone;
    //getter and setter for it

    @Override    
    public Connection getConnection() {
        Connection c = super.getConnection();
        // execute a query: SET time_zone = '-8:00'
        return c;
    }
}

See here http://www.electrictoolbox.com/mysql-set-timezone-per-connection/ for the query details. 有关查询详细信息,请参见http://www.electrictoolbox.com/mysql-set-timezone-per-connection/

MySQL documentation writes : MySQL文档写道

Per-connection time zones. 每个连接时区。 Each client that connects has its own time zone setting, given by the session time_zone variable. 连接的每个客户端都有自己的时区设置,由会话time_zone变量给出。 Initially, the session variable takes its value from the global time_zone variable, but the client can change its own time zone with this statement: 最初,会话变量从全局time_zone变量中获取其值,但客户端可以使用以下语句更改其自己的时区:

mysql> SET time_zone = timezone; mysql> SET time_zone = timezone;

You can also check if c3p0 doesn't have something built-in. 您还可以检查c3p0是否没有内置内容。

One of the possible solutions: 一种可能的解决方案:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="URL" value="${database.url}?serverTimezone=America/Los_Angeles" /> 
    <property name="user" value="${database.username}" /> 
    <property name="password" value="${database.passwd}" /> 
    <property name="connectionCachingEnabled" value="true"/>
    <property name="sessionTimeZone" value="GMT-3"/>
</bean>

There is no "sessionTimeZone" member in the BasicDataSource . 没有“SESSIONTIMEZONE”成员中的BasicDataSource Use C3P0 which is a "better" connection pool than DBCP, or even better, if you are in a Java EE web server, use it to initialize a JNDI datasource ;) 使用C3P0这是一个比DBCP“更好”的连接池,甚至更好,如果你在Java EE Web服务器中,用它来初始化JNDI数据源;)

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

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