简体   繁体   English

在Java中有效跟踪数据库时间的方法

[英]Way to efficiently keep track of the database time in java

Before I start, I want to say that this is related to a Bukkit plugin. 在开始之前,我想说这与Bukkit插件有关。

I have tried searching this all over the web, but I can't just find a good way to do it. 我已经尝试过在整个网络上进行搜索,但是我不仅找到了一种不错的方法。

I am trying to build a plugin that can connect to a database (usually MySQL), and locally keep track of the time of the database, even if it goes down. 我正在尝试构建一个可以连接到数据库(通常是MySQL)的插件,并在本地跟踪数据库的时间,即使它出现故障也是如此。 The plugin is a ban management system, for multiple servers. 该插件是用于多个服务器的禁令管理系统。 The situation is this one: The timezone of the database server is different from the time on the servers, and each server has its own timezone. 情况就是这样:数据库服务器的时区不同于服务器上的时区,并且每个服务器都有自己的时区。 There's only 1 database, so, the best way to do it is keeping track of the time using the time of the database. 只有一个数据库,因此,最好的方法是使用数据库时间来跟踪时间。 But, if the database goes down, a thread will run until the database comes back and the record is successfully inserted. 但是,如果数据库关闭,线程将一直运行,直到数据库返回并成功插入记录。 If I were to use UNIX_TIMESTAMP() on the column that stores when the ban was created, if the database went down, it would only start counting after the database came back. 如果要在创建禁令时存储的列上使用UNIX_TIMESTAMP(),则如果数据库关闭,则仅在数据库恢复后才开始计数。 So, breaking it down, what I want is a way to keep track of the time of the database, even if the database goes down and comes back up. 因此,分解它,即使数据库关闭并重新启动,我想要的也是一种跟踪数据库时间的方法。

Thanks in advance. 提前致谢。

Insert the time as calculated by the program (when the event really happened, not when the db "eventually" inserted the data) in UTC . 在UTC中插入程序计算的时间(事件真正发生时,而不是db“最终”插入数据时)。 It should be more or less the same regardless of the servers. 无论服务器是什么,它应该大致相同。 There could be a slight difference based on clock differences but you can minimize that via ntp to keep the servers' times synchronized. 根据时钟差异可能会略有差异,但是您可以通过ntp将其最小化,以保持服务器时间同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM