diff --git a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java index 3290d7125f2f..500124e8ff12 100644 --- a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java +++ b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java @@ -78,6 +78,7 @@ import com.cloud.utils.component.ComponentMethodInterceptable; import com.cloud.utils.crypt.DBEncryptionUtil; import com.cloud.utils.db.SearchCriteria.SelectType; +import com.cloud.utils.db.datetime.SqlDateTimeUtil; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.Ip; import com.cloud.utils.net.NetUtils; @@ -1720,7 +1721,7 @@ protected void insertElementCollection(T entity, Attribute idAttribute, ID id, M if (d == null) { pstmt.setNull(1, Types.TIMESTAMP); } else { - pstmt.setTimestamp(1, new Timestamp(d.getTime()), gmtCalendar()); + pstmt.setTimestamp(1, SqlDateTimeUtil.toWholeSecondTimestamp(d.getTime()), gmtCalendar()); } } else { pstmt.setObject(1, en.nextElement()); @@ -1831,9 +1832,9 @@ protected void prepareAttribute(final int j, final PreparedStatement pstmt, fina if (attr.is(Attribute.Flag.Date)) { pstmt.setDate(j, new java.sql.Date(date.getTime()), gmtCalendar()); } else if (attr.is(Attribute.Flag.TimeStamp)) { - pstmt.setTimestamp(j, new Timestamp(date.getTime()), gmtCalendar()); + pstmt.setTimestamp(j, SqlDateTimeUtil.toWholeSecondTimestamp(date.getTime()), gmtCalendar()); } else if (attr.is(Attribute.Flag.Time)) { - pstmt.setTime(j, new java.sql.Time(date.getTime()), gmtCalendar()); + pstmt.setTime(j, new java.sql.Time(SqlDateTimeUtil.truncateToSeconds(date.getTime())), gmtCalendar()); } } else if (attr.field.getType() == Calendar.class) { final Calendar cal = (Calendar)value; @@ -1844,9 +1845,9 @@ protected void prepareAttribute(final int j, final PreparedStatement pstmt, fina if (attr.is(Attribute.Flag.Date)) { pstmt.setDate(j, new java.sql.Date(cal.getTimeInMillis()), gmtCalendar()); } else if (attr.is(Attribute.Flag.TimeStamp)) { - pstmt.setTimestamp(j, new Timestamp(cal.getTimeInMillis()), gmtCalendar()); + pstmt.setTimestamp(j, SqlDateTimeUtil.toWholeSecondTimestamp(cal.getTimeInMillis()), gmtCalendar()); } else if (attr.is(Attribute.Flag.Time)) { - pstmt.setTime(j, new Time(cal.getTimeInMillis()), gmtCalendar()); + pstmt.setTime(j, new Time(SqlDateTimeUtil.truncateToSeconds(cal.getTimeInMillis())), gmtCalendar()); } } else if (attr.field.getType().isEnum()) { final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class); diff --git a/framework/db/src/main/java/com/cloud/utils/db/datetime/SqlDateTimeUtil.java b/framework/db/src/main/java/com/cloud/utils/db/datetime/SqlDateTimeUtil.java new file mode 100644 index 000000000000..a930836844a8 --- /dev/null +++ b/framework/db/src/main/java/com/cloud/utils/db/datetime/SqlDateTimeUtil.java @@ -0,0 +1,41 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.utils.db.datetime; + +import java.sql.Timestamp; + +/** + * Helpers for binding dates to DATETIME/TIME columns, which have no fractional seconds. + * The MySQL driver sends milliseconds to the server, which rounds rather than truncates them + * (e.g. 10:59:59.999 is stored as 11:00:00), so values are cut to whole seconds before binding. + */ +public final class SqlDateTimeUtil { + + private SqlDateTimeUtil() { + } + + /** + * Drops the sub-second part of an epoch-millis value, rounding towards the past. + */ + public static long truncateToSeconds(long millis) { + return Math.floorDiv(millis, 1000L) * 1000L; + } + + public static Timestamp toWholeSecondTimestamp(long millis) { + return new Timestamp(truncateToSeconds(millis)); + } +} diff --git a/framework/db/src/test/java/com/cloud/utils/db/datetime/SqlDateTimeUtilTest.java b/framework/db/src/test/java/com/cloud/utils/db/datetime/SqlDateTimeUtilTest.java new file mode 100644 index 000000000000..b6f65617de99 --- /dev/null +++ b/framework/db/src/test/java/com/cloud/utils/db/datetime/SqlDateTimeUtilTest.java @@ -0,0 +1,45 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.utils.db.datetime; + +import java.sql.Timestamp; + +import org.junit.Assert; +import org.junit.Test; + +public class SqlDateTimeUtilTest { + + @Test + public void truncateToSecondsDropsMillisInsteadOfRounding() { + Assert.assertEquals(1_795_000_859_000L, SqlDateTimeUtil.truncateToSeconds(1_795_000_859_999L)); + Assert.assertEquals(1_795_000_859_000L, SqlDateTimeUtil.truncateToSeconds(1_795_000_859_000L)); + Assert.assertEquals(1_795_000_859_000L, SqlDateTimeUtil.truncateToSeconds(1_795_000_859_001L)); + } + + @Test + public void truncateToSecondsFloorsNegativeEpochValues() { + Assert.assertEquals(-1000L, SqlDateTimeUtil.truncateToSeconds(-1L)); + } + + @Test + public void toWholeSecondTimestampHasNoFractionalPart() { + Timestamp ts = SqlDateTimeUtil.toWholeSecondTimestamp(1_795_000_859_999L); + + Assert.assertEquals(1_795_000_859_000L, ts.getTime()); + Assert.assertEquals(0, ts.getNanos()); + } +}