programing

Oracle JDBC 드라이버에서 타임스탬프 열에 Java 날짜를 쓸 때 시간대는 어떻게 됩니까?

telebox 2023. 8. 10. 18:43
반응형

Oracle JDBC 드라이버에서 타임스탬프 열에 Java 날짜를 쓸 때 시간대는 어떻게 됩니까?

검색해봤는데 놀랍게도 Oracle JDBC에 대한 답을 찾을 수가 없습니다. 밀접한 관련이 있는 질문에는 Postgre에 대한 답이 있습니다.SQL 및 MySQL.

기본적으로 두 개의 서로 다른 시간대에 두 개의 애플리케이션 서버가 하나의 Oracle 데이터베이스에 타임스탬프를 기록하면 어떻게 됩니까?감사해요.

편집: 쿼리를 수행할 때 JDBC가 데이터베이스로 보내는 값이 현지 시간대에 있는 것 같다는 점을 추가해야 합니다.

정확히 무슨 일이 일어나는지 알아내기 위해 몇 가지 테스트 JDBC 코드를 작성했습니다.결과는 흥미로웠습니다.Oracle에는 세 가지 밀접한 관련이 있는 데이터 유형이 있습니다.TIMESTAMP,TIMESTAMP WITH TIME ZONE,그리고.TIMESTAMP WITH LOCAL TIME ZONE저는 정확히 같은 코드를 가져와서 두 개의 다른 상자에서 실행했습니다. 하나는 "America/New_York" 시간대에서 실행되고 다른 하나는 UTC에서 실행됩니다.둘 다 UTC에서 실행되는 동일한 데이터베이스에 도달했습니다.Oracle 11.2.0.2.0 드라이버를 사용하고 있었습니다.

  • TIMESTAMP열은 Java 코드를 실행하는 컴퓨터의 로컬 시간과 상관없이 설정되었습니다.표준 시간대 변환이 수행되지 않았습니다.
  • TIMESTAMP WITH TIME ZONE열은 시간을 JDBC 클라이언트가 있는 시간대로 변환했습니다.
  • TIMESTAMP WITH LOCAL TIME ZONE열은 또한 시간을 JDBC 클라이언트가 있는 시간대로 변환했습니다.

조금 더 오래된 이 기사는 다음을 나타냅니다.TIMESTAMP WITH TIME ZONE인덱스나 파티션과 같은 작업을 수행하려면 거의 쓸모가 없습니다.하지만, 그것은 마치.TIMESTAMP WITH LOCAL TIME ZONE매우 유용할 수 있습니다. (서버의 표준 시간대를 변경하면 어떻게 되는지는 확실하지 않지만 JDBC 클라이언트의 로컬 표준 시간대에 대해 지능적입니다.)이러한 데이터 유형으로 인덱싱 동작 등을 테스트할 기회가 없었습니다.

귀사의 환경에서 제 테스트를 재현하고 싶다면 아래 샘플 클래스에 붙여넣습니다.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Date;

// create table x_tst_ts_tab(
// os_name varchar(256)
// ts timestamp,
// ts_with_tz timestamp with time zone,
// ts_with_local_tz timestamp with local time zone
// )
class TSTest {
    public static final void main(String[] argv) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        Connection conn = DriverManager.getConnection(
            "your_connection_string",
            "your_user_name",
            "your_password");

        try {
            // Insert some data
            Date nowDate = new Date();
            Timestamp nowTimestamp = new Timestamp(nowDate.getTime());
            PreparedStatement insertStmt = conn.prepareStatement(
                "INSERT INTO x_tst_ts_tab"
                + " (os_name, ts, ts_with_tz, ts_with_local_tz)"
                + " VALUES (?, ?, ?, ?)");
            try {
                insertStmt.setString(1, System.getProperty("os.name"));
                insertStmt.setTimestamp(2, nowTimestamp);
                insertStmt.setTimestamp(3, nowTimestamp);
                insertStmt.setTimestamp(4, nowTimestamp);
                insertStmt.executeUpdate();
            } finally {
                try {
                    insertStmt.close();
                } catch (Throwable t) {
                    // do nothing
                }
            }

            System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz");

            // Read back everything in the DB
            PreparedStatement selectStmt = conn.prepareStatement(
                "SELECT os_name, ts, ts_with_tz, ts_with_local_tz"
                + " FROM dom_fraud_beacon.x_tst_ts_tab");
            ResultSet result = null;
            try {
                result = selectStmt.executeQuery();
                while (result.next()) {
                    System.out.println(
                        String.format("%s,%s,%s,%s",
                                      result.getString(1),
                                      result.getTimestamp(2).toString(),
                                      result.getTimestamp(3).toString(),
                                      result.getTimestamp(4).toString()
                                      ));
                }
            } finally {
                try {
                    result.close();
                } catch (Throwable t) {
                    // do nothing
                } finally {
                    try {
                        selectStmt.close();
                    } catch (Throwable t) {
                        // do nothing
                    }
                }
            }
        } finally {
            try {
                conn.close();
            } catch (Throwable t) {
                // do nothing
            }
        }
    }
}

언급URL : https://stackoverflow.com/questions/12008801/in-the-oracle-jdbc-driver-what-happens-to-the-time-zone-when-you-write-a-java-d

반응형