2015-11-23 75 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