使用 spring jdbc oracle 设置会话时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2805907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting session timezone with spring jdbc oracle
提问by user337620
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).
我有一个 spring/jdbc/oracle 10g 应用程序。Oracle 服务器数据库时区设置为 GMT + 2 JVM 时区为 GMT + 2(即使在我的情况下无关紧要)。
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.
我有一个执行一些日期操作的存储过程。问题是会话时区与数据库时区不同(GMT),即使我没有在我的代码/配置中明确设置会话时区。
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) ?
据我所知,会话时区默认等于数据库时区。知道为什么会话时区与数据库时区不同,或者我如何在 spring 配置 (org.apache.commons.dbcp.BasicDataSource) 中配置它?
Thank you.
谢谢你。
采纳答案by Lukas Herman
The correct way is to use DelegatingDataSource
, retrieve OracleConnection
object from the original data source and call OracleConnection.setSessionTimeZone()
with the appropriate parameter.
正确的方法是使用DelegatingDataSource
,OracleConnection
从原始数据源检索对象并OracleConnection.setSessionTimeZone()
使用适当的参数调用。
C3P0 code looks like:
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;
}
}
回答by Grilse
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.
我通过将 Oracle 的 JDBC 驱动程序从 v10.2.0.1.0 升级到 v11.2.0.3 解决了这个问题。不需要更改我的 java 代码。
Source: Spring forums.
来源:春季论坛。
回答by Abhimanyu Singh
In spring configuration, add below VM options before running the application:
在 spring 配置中,在运行应用程序之前添加以下 VM 选项:
-Duser.timezone=EDT
Also make sure your pom has latest jdbc driver
还要确保您的 pom 具有最新的 jdbc 驱动程序
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>