我正在编写一个java应用程序,它从Oracle获取UTC时间,然后在一个表上查询一行是否与该时间匹配。如果没有结果,请再次使用UTC,依此类推。
但有时候时间会倒退或者需要花费1或2分钟的时间才能获得正确的时间。
这是UTC时间的日志:
15/03/2016 16:42:59 [INFO] Time (UTC): 21:42:25 {1}
15/03/2016 16:42:59 [INFO] Time (UTC): 21:42:25 {2}
15/03/2016 16:42:59 [INFO] Time (UTC): 21:42:25 {3}
15/03/2016 16:42:59 [INFO] Time (UTC): 21:42:25 {4}
15/03/2016 16:42:59 [INFO] Time (UTC): 21:42:25 {5}
15/03/2016 16:43:00 [INFO] Time (UTC): 21:44:12 {6}
15/03/2016 16:43:00 [INFO] Time (UTC): 21:42:26 {7}
15/03/2016 16:43:00 [INFO] Time (UTC): 21:42:26 {8}
15/03/2016 16:43:00 [INFO] Time (UTC): 21:42:26 {9}
15/03/2016 16:43:00 [INFO] Time (UTC): 21:42:26 {10}
15/03/2016 16:43:00 [INFO] Time (UTC): 21:42:26 {11}
15/03/2016 16:43:01 [INFO] Time (UTC): 21:42:27 {12}
15/03/2016 16:43:01 [INFO] Time (UTC): 21:42:27 {13}
15/03/2016 16:43:01 [INFO] Time (UTC): 21:42:27 {14}
15/03/2016 16:43:01 [INFO] Time (UTC): 21:42:27 {15}
15/03/2016 16:43:01 [INFO] Time (UTC): 21:42:27 {16}
15/03/2016 16:43:01 [INFO] Time (UTC): 21:42:27 {17}
15/03/2016 16:43:02 [INFO] Time (UTC): 21:42:28 {18}
15/03/2016 16:43:02 [INFO] Time (UTC): 21:42:28 {19}
15/03/2016 16:43:02 [INFO] Time (UTC): 21:42:28 {20}
15/03/2016 16:43:02 [INFO] Time (UTC): 21:42:28 {21}
15/03/2016 16:43:02 [INFO] Time (UTC): 21:42:28 {22}
15/03/2016 16:43:02 [INFO] Time (UTC): 21:42:28 {23}
15/03/2016 16:43:03 [INFO] Time (UTC): 21:44:15 {24}
15/03/2016 16:43:03 [INFO] Time (UTC): 21:44:15 {25}
15/03/2016 16:43:03 [INFO] Time (UTC): 21:44:15 {26}
15/03/2016 16:43:03 [INFO] Time (UTC): 21:44:15 {27}
15/03/2016 16:43:03 [INFO] Time (UTC): 21:44:16 {28}
15/03/2016 16:43:04 [INFO] Time (UTC): 21:44:16 {29}
15/03/2016 16:43:04 [INFO] Time (UTC): 21:42:30 {30}
这是我的代码的一部分:
int arraycounter = 0;
int index = 0;
while (arraycounter = 0)
{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String utc_time = "";
String querytime = "select to_char(sys_extract_utc(systimestamp),'HH24:MI:SS') from dual";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:"+ConnectionString);
stmt = con.createStatement();
rs = stmt.executeQuery(querytime);
while(rs.next()) {
utc_time = rs.getString(1);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
String query = "select * from table where to_char(date, 'hh24:mi:ss') = '"+utc_time+"'";
index++;
logger.info("Time (UTC): "+ utc_time +" {"+(index)+"}");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:"+ConnectionString);
stmt = con.createStatement();
rs = stmt.executeQuery(querytime);
while( rs.next() ){
//save the result into an arraylist
arraycounter++;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
为什么会这样? 是否有更好的方式采用UTC时间?
答案 0 :(得分:2)
我的Oracle数据库在RAC上,实例的时间不同步。 我检查了进入实例并检查时间,然后进入另一个实例并检查时间。
要知道你可以在什么情况下运行:
select instance_name from v$instance;
为了连接到数据库的特定实例,您还可以在连接描述符中指定特定实例的INSTANCE_NAME。如果您具有Oracle Real Application Clusters配置,则此功能非常有用。例如,以下连接描述符指定与sales.us.acme.com关联的sales1的实例名称。
(DESCRIPTION=
(ADDRESS=(PROTOCOL=tcp)(HOST=sales-server)(PORT=1521))
(CONNECT_DATA=
(SERVICE_NAME=sales.us.acme.com)
(INSTANCE_NAME=sales1)))
您可以在Oracle SQL Developer上将其指定为SID = sales1