使用spring jdbc oracle设置会话时区 [英] Setting session timezone with spring jdbc oracle
问题描述
我有一个spring / jdbc / oracle 10g应用程序。
Oracle服务器数据库时区设置为GMT + 2 JVM时区是GMT + 2(即使在我的情况下无关紧要)。
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有一个存储过程执行一些日期操作。问题是会话时区与数据库时区不同(GMT),即使我没有在我的代码/配置中明确设置会话时区。
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.
据我所知session timezone默认等于数据库时区。知道为什么会话时区与数据库时区不同,或者如何在spring配置中配置它(org.apache.commons.dbcp.BasicDataSource)?
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) ?
谢谢。
推荐答案
正确的方法是使用 DelegatingDataSource
,检索<$ c来自原始数据源的$ c> OracleConnection 对象,并使用适当的参数调用 OracleConnection.setSessionTimeZone()
。
The correct way is to use DelegatingDataSource
, retrieve OracleConnection
object from the original data source and call OracleConnection.setSessionTimeZone()
with the appropriate parameter.
C3P0代码如下:
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;
}
}
这篇关于使用spring jdbc oracle设置会话时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!