2015-11-23 79 views
1

我在我的web应用程序中通过每次获取数据库连接时重置数据库会话时区去handle multiple time zone在jboss/weblogic服务器中配置带时区的数据源

代码如下:

public Connection getConnection(GmDNSNamesEnum enDNSName) throws AppError 
    { 
     ... 
     DataSource ds = (DataSource) ic.lookup(enDNSName.getDNSName()); 
      conn = ds.getConnection(); 
      conn.setAutoCommit(false); 
      setTimeZone(strTimeZone); 
     ... 
     return conn; 
    } 




private void setTimeZone(String strTimeZone){ 
    Statement stmt = null; 
    if(conn != null) { 
    try{ 
    stmt = conn.createStatement(); 
     stmt.execute("alter session set time_zone=\'" + strTimeZone+"\'"); 
    } catch (Exception e) 
     { 
     String strErrorMsg = GmCommonClass.getExceptionStackTrace(e); 
      log.error("Exception e "+strErrorMsg); 
     throw new AppError(e); 
     } 
    } 
    } 

是否有设置数据库会话时区的任何替代方法?

现在,我正在寻找具有不同时区的配置数据源在jboss/weblogic服务器中,并使用特定于用户时区的适当数据源,而不是每次通过执行alter session脚本来重置会话时区。

在此先感谢

回答

0

哇,好的。

也许你可以尝试将日期和时间保存到时区感知的数据库列中,而不是每次连接时都使数据源扭曲?约定是以UTC时间存储日期,然后在表示层中进行时区转换(或至少在检索后)。

+0

如果日期列定义为TIMESTAMP WITH LOCAL TIME ZONE,那么Oracle将负责将日期转换为当前用户会话时区,不需要手动转换。所以我尝试每次将用户会话时区设置为数据库连接。 – Gopi