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.
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.
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';
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.
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.
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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With