Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting session timezone with spring jdbc oracle

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).

I have a stored procedure that performs some date operations. The problem is that session timezone is different(GMT) than database timezone even though I do not set session timezone explicit in my code/configuration.

As far as I know the session timezone is by default equal to database timezone. Any idea why is the session timezone different than database timezone or how can I configure it in spring configuration (org.apache.commons.dbcp.BasicDataSource) ?

Thank you.

like image 207
user337620 Avatar asked May 10 '10 19:05

user337620


People also ask

How to set timezone for JDBC connection?

To set the timezone for a given JDBC connection, navigate to the Advanced tab and select the timezone from the dropdown menu. By default, UTC is selected.

How do I set UTC time zone in Oracle?

Use the ALTER DATABASE SET TIME_ZONE command to change the time zone of a database. This command takes either a named region such as America/Los_Angeles or an absolute offset from UTC. This example sets the time zone to UTC: ALTER DATABASE SET TIME_ZONE = '+00:00';

What is session timezone in Oracle?

Purpose. SESSIONTIMEZONE returns the time zone of the current session. The return type is a time zone offset (a character type in the format '[+|-]TZH:TZM' ) or a time zone region name, depending on how the user specified the session time zone value in the most recent ALTER SESSION statement.

Does Oracle use spring?

Oracle Enterprise Pack for Eclipse (OEPE) supports Spring IDE 2.0 and Spring Framework 2.5. OEPE allows to automatically generate Spring configuration and beans from persistence mappings. In this tutorial part, you will use the Spring framework for developing service beans / business delegates.


2 Answers

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;
    }
}
like image 80
Lukas Herman Avatar answered Sep 22 '22 21:09

Lukas Herman


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.

like image 30
Grilse Avatar answered Sep 19 '22 21:09

Grilse