简体   繁体   English

在MySQL数据库中存储微秒

[英]Storing microseconds in a MySQL database

I have a Play Framework application with ebeans.To synchronise between the main system and an offline system I use an addDate field to identify unique records. 我有一个带ebeans的Play Framework应用程序。要在主系统和脱机系统之间进行同步,我使用addDate字段来标识唯一记录。

This works perfect while developing and initial testing (development with jdbc:h2:mem:play;MODE=MYSQL) and testing with a limited dataset. 这在开发和初始测试(使用jdbc:h2:mem:play; MODE = MYSQL进行开发)以及使用有限的数据集进行测试时非常理想。

But in production, with concurrent users I discovered that in the MySQL database milliseconds are not stored. 但是在生产中,对于并发用户,我发现在MySQL数据库中不会存储毫秒。 I have the fields created as datetime(6), but a query like: 我将字段创建为datetime(6),但查询如下:

SELECT UNIX_TIMESTAMP(add_date) FROM `user` WHERE 1 

returns: 收益:

1499722493.000000
1499772800.000000
1499777225.000000
1499790922.000000
1499875868.000000
1499954855.000000
1499977124.000000
1499981148.000000
1499986822.000000

(ps. just selecting the add_date field in MySQL return 2015-12-17 16:15:50.000000, showing the precision is there) (ps。只需在MySQL返回2015-12-17 16:15:50.000000中选择add_date字段,就可以看到精度了)

So milliseconds are not stored. 因此不存储毫秒。 In Java I use the normal Date class, so they are in there (and so in the H2 men test database). 在Java中,我使用常规的Date类,因此它们位于其中(因此在H2 men test数据库中也是如此)。

protected Date addDate;

How can I have the milliseconds stored in the Database with my current configuration without, hopefully, having to rewrite too much code? 如何用当前配置将毫秒存储在数据库中,而又不必重写太多代码?

[Edit:] [编辑:]

I am using MySQL on Debian: Ver 14.14 Distrib 5.6.35
sbt.version=0.13.11
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.16")
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "3.0.2")
mysql-connector-java % 5.1.43

java.time java.time

The troublesome java.util.Date class is now supplanted by the java.time classes. 麻烦的java.util.Date类现在已由java.time类取代。

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). Instant类以UTC表示时间轴上的时刻,分辨率为纳秒 (最多十进制的九(9)位数字)。

Capturing the current moment is limited to milliseconds in Java 8 but expanded to up to nanoseconds in Java 9 and later with a new implementation of Clock . 在Java 8中,捕获当前时刻仅限于毫秒 ,但在Java 9中,捕获时延至纳秒,后来又有了Clock新实现 To be clear, I'll repeat: Both Java 8 and Java 9 can store a value with nanoseconds, but only Java 9 and later can capture the current moment in finer than milliseconds resolution. 为了清楚起见,我将重复一遍:Java 8和Java 9都可以存储纳秒级的值,但是只有Java 9和更高版本才能以毫秒级以上的精度捕获当前时刻。

Instant instant = Instant.now() ;  // Captures milliseconds in Java 8 but nanoseconds in Java 9+.

Use a JDBC driver that complies with JDBC 4.2 or later, to work directly with the java.time types rather than the hacked java.sql types. 使用符合JDBC 4.2或更高版本的JDBC驱动程序直接使用java.time类型而不是被黑的java.sql类型。 Call the PreparedStatement::setObject and ResultSet::getObject methods. 调用PreparedStatement::setObjectResultSet::getObject方法。

myPreparedStatement.setObject( … , instant ) ;

…and… …和…

Instant instant = myResultSet.getObject( … , Instant.class ) ; // Transfers a value with nanoseconds if present.

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310

Where to obtain the java.time classes? 在哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

In java you can not accurate to the microsecond (nanosecond) without taking a lot of system resources. 在Java中,如果不占用大量系统资源,则无法精确到微秒(纳秒)。 Your date is inserted in milliseconds because java does it in milliseconds. 您的日期以毫秒为单位插入,因为java以毫秒为单位。

If you want to use microseconds from SQL you must use the SQL NOW() on insertion. 如果要使用SQL中的微秒,则必​​须在插入时使用SQL NOW()。

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

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