Установка часового пояса подключения с помощью Spring, DBCP и MySQL

My Enviroment

  • Java 5
  • Spring 2.5.5
  • DBCP DataSource (org.apache.commons.dbcp.BasicDataSource)
  • 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.

My Idea/research

  • DBCP Connection Pool did not mention anything around timezone. LINK

  • What I investigate and thought that was oK is described on THIS post, exemplifying is:


     
     
     
    
    

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.

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:


    (...)
    
    (...)

I hope this post can help someone in the future. Any question ping me!

13
задан Community 23 May 2017 в 11:53
поделиться