Setting session timezone with spring jdbc oracle

前端 未结 3 1989
一生所求
一生所求 2020-12-20 19:21

I have a spring/jdbc/oracle 10g application. The Oracle server database timezone is set to GMT + 2 JVM timezone is GMT + 2 (even though it doesn\'t matter in my case).

相关标签:
3条回答
  • 2020-12-20 19:55

    In spring configuration, add below VM options before running the application:

    -Duser.timezone=EDT
    

    Also make sure your pom has latest jdbc driver

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>12.2.0.1</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-20 20:03

    I solved this problem by upgrading Oracle's JDBC drivers from v10.2.0.1.0 to v11.2.0.3. No changes to my java code were required.

    Source: Spring forums.

    0 讨论(0)
  • 2020-12-20 20:09

    The correct way is to use DelegatingDataSource, retrieve OracleConnection object from the original data source and call OracleConnection.setSessionTimeZone() with the appropriate parameter.

    C3P0 code looks like:

    private Object[] timeZoneArgs = new Object[] { "Europe/Berlin" };
    
    @Override
    public Connection getConnection() throws SQLException {
        Connection conn = super.getConnection();
        try {
            final Method setSessionTimeZoneMethod = OracleConnection.class.getMethod("setSessionTimeZone", String.class);
            final C3P0ProxyConnection castCon = (C3P0ProxyConnection) conn;
            castCon.rawConnectionOperation(setSessionTimeZoneMethod, C3P0ProxyConnection.RAW_CONNECTION, timeZoneArgs);
            return conn;
        } catch (Exception e) {
            log.error("setSessionTimeZone failed " + e.getMessage());
            return conn;
        }
    }
    
    0 讨论(0)
提交回复
热议问题