Skip to content

Commit

Permalink
* db: tweaked all datetime related operations for virtual thread
Browse files Browse the repository at this point in the history
Signed-off-by: neo <[email protected]>
  • Loading branch information
neowu committed Dec 13, 2023
1 parent 0efcbcc commit fe7a376
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### 9.0.3 (12/12/2023 - )

* kafka: updated client to 3.6.1
* db: tweaked all datetime related operations for virtual thread
> use new date api if possible, mysql driver uses too many locks/sync for old Date/Timestamp impl
> pls make sure to map MySQL column type: LocalDate -> DATE, LocalDateTime -> DATETIME, ZonedDateTime -> TIMESTAMP(6)
### 9.0.2 (12/7/2023 - 12/12/2023)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private List<String> schemeStatements() {

// http://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html
private String columnType(Class<?> fieldClass, Size size, boolean json) {
if (json) return "LONGVARCHAR";
if (json) return "TEXT";
if (Integer.class.equals(fieldClass)) return "INT";
if (Long.class.equals(fieldClass)) return "BIGINT";
if (String.class.equals(fieldClass)) {
Expand All @@ -102,10 +102,13 @@ private String columnType(Class<?> fieldClass, Size size, boolean json) {
return "DOUBLE";
}
if (BigDecimal.class.equals(fieldClass)) {
return "DECIMAL(10,2)";
return "DECIMAL(20,6)";
}
if (LocalDateTime.class.equals(fieldClass) || ZonedDateTime.class.equals(fieldClass)) {
return "TIMESTAMP";
if (LocalDateTime.class.equals(fieldClass)) {
return "DATETIME";
}
if (ZonedDateTime.class.equals(fieldClass)) {
return "TIMESTAMP(6)";
}
if (LocalDate.class.equals(fieldClass)) {
return "DATE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDate;
Expand Down Expand Up @@ -200,9 +198,9 @@ private void setParams(PreparedStatement statement, Object... params) throws SQL
private void setParam(PreparedStatement statement, int index, Object param) throws SQLException {
switch (param) {
case String value -> statement.setString(index, value);
case Integer value -> statement.setInt(index, value);
case Enum<?> value -> statement.setString(index, enumMapper.getDBValue(value));
case LocalDateTime value -> statement.setTimestamp(index, Timestamp.valueOf(value));
case LocalDate value -> statement.setObject(index, value, Types.DATE);
case LocalDateTime value -> statement.setObject(index, value, Types.TIMESTAMP);
case ZonedDateTime value -> {
// https://dev.mysql.com/doc/refman/8.0/en/datetime.html,
// TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
Expand All @@ -219,13 +217,13 @@ private void setParam(PreparedStatement statement, int index, Object param) thro
// so on application level, if you can not ensure the range of input value, write its own utils to check
Instant instant = value.toInstant();
if (instant.getEpochSecond() <= 0) throw new Error("timestamp must be after 1970-01-01 00:00:00, value=" + param);
statement.setTimestamp(index, Timestamp.from(instant));
statement.setObject(index, instant, Types.TIMESTAMP);
}
case Boolean value -> statement.setBoolean(index, value);
case Integer value -> statement.setInt(index, value);
case Long value -> statement.setLong(index, value);
case Double value -> statement.setDouble(index, value);
case BigDecimal value -> statement.setBigDecimal(index, value);
case LocalDate value -> statement.setDate(index, Date.valueOf(value));
case null -> statement.setNull(index, Types.NULL); // both mysql/hsql driver are not using sqlType param
default -> throw new Error(format("unsupported param type, type={}, value={}", param.getClass().getCanonicalName(), param));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ Integer getInt(String column) throws SQLException {
}

Integer getInt(int index) throws SQLException {
int value = resultSet.getInt(index);
if (resultSet.wasNull()) return null;
return value;
return resultSet.getObject(index, Integer.class);
}

Boolean getBoolean(String column) throws SQLException {
Expand All @@ -74,9 +72,7 @@ Boolean getBoolean(String column) throws SQLException {
}

Boolean getBoolean(int index) throws SQLException {
boolean value = resultSet.getBoolean(index);
if (resultSet.wasNull()) return null;
return value;
return resultSet.getObject(index, Boolean.class);
}

Long getLong(String column) throws SQLException {
Expand All @@ -86,9 +82,7 @@ Long getLong(String column) throws SQLException {
}

Long getLong(int index) throws SQLException {
long value = resultSet.getLong(index);
if (resultSet.wasNull()) return null;
return value;
return resultSet.getObject(index, Long.class);
}

Double getDouble(String column) throws SQLException {
Expand All @@ -98,9 +92,7 @@ Double getDouble(String column) throws SQLException {
}

Double getDouble(int index) throws SQLException {
double value = resultSet.getDouble(index);
if (resultSet.wasNull()) return null;
return value;
return resultSet.getObject(index, Double.class);
}

String getString(String column) throws SQLException {
Expand Down Expand Up @@ -130,11 +122,7 @@ LocalDateTime getLocalDateTime(String column) throws SQLException {
}

LocalDateTime getLocalDateTime(int index) throws SQLException {
// mysql saves datetime or timestamp in UTC, com.mysql.cj.result.LocalDateTimeValueFactory use UTC value directly
// so here it has to convert back to application timezone, in cloud env it most likely still be UTC
OffsetDateTime time = resultSet.getObject(index, OffsetDateTime.class);
if (time == null) return null;
return LocalDateTime.ofInstant(time.toInstant(), ZoneId.systemDefault());
return resultSet.getObject(index, LocalDateTime.class);
}

LocalDate getLocalDate(String column) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RepositoryImplAutoIncrementIdEntityTest {
void createDatabase() {
database = new DatabaseImpl("db");
database.url("jdbc:hsqldb:mem:mysql;sql.syntax_mys=true");
database.execute("CREATE TABLE auto_increment_id_entity (id INT AUTO_INCREMENT PRIMARY KEY, string_field VARCHAR(20), double_field DOUBLE, enum_field VARCHAR(10), date_time_field TIMESTAMP, zoned_date_time_field TIMESTAMP)");
database.execute("CREATE TABLE auto_increment_id_entity (id INT AUTO_INCREMENT PRIMARY KEY, string_field VARCHAR(20), double_field DOUBLE, enum_field VARCHAR(10), date_time_field DATETIME, zoned_date_time_field TIMESTAMP)");
repository = database.repository(AutoIncrementIdEntity.class);
}

Expand Down

0 comments on commit fe7a376

Please sign in to comment.