我有一个 spring/jdbc/oracle 10g 应用程序。 Oracle 服务器数据库时区设置为 GMT + 2 JVM 时区为 GMT + 2(尽管在我的情况下这无关紧要)。
我有一个执行一些日期操作的存储过程。问题是 session 时区与数据库时区不同(格林威治标准时间),即使我没有在我的代码/配置中显式设置 session 时区。
据我所知, session 时区默认等于数据库时区。知道为什么 session 时区与数据库时区不同,或者我如何在 spring 配置 (org.apache.commons.dbcp.BasicDataSource) 中配置它?
谢谢。
最佳答案
正确的方法是使用 DelegatingDataSource
,从原始数据源中检索 OracleConnection
对象并使用适当的调用 OracleConnection.setSessionTimeZone()
参数。
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;
}
}
关于java - 使用 spring jdbc oracle 设置 session 时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2805907/