#1775 - MySql 8 by default wants JDBC Date set and read with JVM Calendar (timezone)

This commit is contained in:
rob bygrave
2019-07-25 21:25:38 +12:00
parent b04f887bc3
commit 5a39dcb39c
12 changed files with 66 additions and 13 deletions
+3 -4
View File
@@ -141,7 +141,7 @@
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-migration</artifactId>
<version>11.18.2</version>
<version>11.17.1</version>
</dependency>
<dependency>
@@ -229,7 +229,7 @@
<dependency>
<groupId>io.ebean.test</groupId>
<artifactId>ebean-test-docker</artifactId>
<version>2.5.1</version>
<version>2.8.1</version>
<scope>test</scope>
</dependency>
@@ -273,8 +273,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- Do not upgrade to 6.0.6: https://bugs.mysql.com/bug.php?id=82896 -->
<version>5.1.43</version>
<version>8.0.17</version>
<scope>test</scope>
</dependency>
@@ -45,6 +45,7 @@ import io.ebeaninternal.server.cluster.ClusterManager;
import io.ebeaninternal.server.core.bootup.BootupClasses;
import io.ebeaninternal.server.core.timezone.CloneDataTimeZone;
import io.ebeaninternal.server.core.timezone.DataTimeZone;
import io.ebeaninternal.server.core.timezone.LocalDataTimeZone;
import io.ebeaninternal.server.core.timezone.NoDataTimeZone;
import io.ebeaninternal.server.core.timezone.SimpleDataTimeZone;
import io.ebeaninternal.server.deploy.BeanDescriptorManager;
@@ -366,6 +367,10 @@ public class InternalConfiguration {
return bootupClasses;
}
private Platform getPlatform() {
return getDatabasePlatform().getPlatform();
}
public DatabasePlatform getDatabasePlatform() {
return serverConfig.getDatabasePlatform();
}
@@ -509,9 +514,12 @@ public class InternalConfiguration {
String tz = serverConfig.getDataTimeZone();
if (tz == null) {
if (getPlatform() == Platform.MYSQL) {
return new LocalDataTimeZone();
}
return new NoDataTimeZone();
}
if (getDatabasePlatform().getPlatform() == Platform.ORACLE) {
if (getPlatform() == Platform.ORACLE) {
return new CloneDataTimeZone(tz);
} else {
return new SimpleDataTimeZone(tz);
@@ -0,0 +1,20 @@
package io.ebeaninternal.server.core.timezone;
import java.util.Calendar;
/**
* Implementation of DataTimeZone when single Calendar instance is used with local timezone.
*/
public class LocalDataTimeZone implements DataTimeZone {
protected final Calendar zone;
public LocalDataTimeZone() {
this.zone = Calendar.getInstance();
}
@Override
public Calendar getTimeZone() {
return zone;
}
}
@@ -141,7 +141,12 @@ public class DataBind {
}
public void setDate(java.sql.Date v) throws SQLException {
pstmt.setDate(++pos, v);
Calendar timeZone = dataTimeZone.getTimeZone();
if (timeZone != null) {
pstmt.setDate(++pos, v, timeZone);
} else {
pstmt.setDate(++pos, v);
}
}
public void setTimestamp(Timestamp v) throws SQLException {
@@ -106,7 +106,12 @@ public class RsetDataReader implements DataReader {
@Override
public Date getDate() throws SQLException {
return rset.getDate(pos());
Calendar cal = dataTimeZone.getTimeZone();
if (cal != null) {
return rset.getDate(pos(), cal);
} else {
return rset.getDate(pos());
}
}
@Override
@@ -20,7 +20,7 @@ public abstract class ScalarTypeBaseDate<T> extends ScalarTypeBase<T> {
protected final JsonConfig.Date mode;
public ScalarTypeBaseDate(JsonConfig.Date mode, Class<T> type, boolean jdbcNative, int jdbcType) {
ScalarTypeBaseDate(JsonConfig.Date mode, Class<T> type, boolean jdbcNative, int jdbcType) {
super(type, jdbcNative, jdbcType);
this.mode = mode;
}
+2 -2
View File
@@ -7,7 +7,7 @@ public class StartMySql {
public static void main(String[] args) {
MySqlConfig config = new MySqlConfig("5.7");
MySqlConfig config = new MySqlConfig("8.0");
config.setDbName("unit");
config.setUser("unit");
config.setPassword("unit");
@@ -23,6 +23,6 @@ public class StartMySql {
// config.setCharacterSet("utf8mb4");
MySqlContainer container = new MySqlContainer(config);
container.start();
container.startWithDropCreate();
}
}
@@ -19,7 +19,7 @@ import static org.junit.Assert.assertNotNull;
public class TestAggregateFormula extends BaseTestCase {
@IgnorePlatform(Platform.SQLSERVER)
@IgnorePlatform({Platform.SQLSERVER, Platform.MYSQL})
@Test
public void minDistinctOrderByNulls() {
@@ -5,6 +5,7 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
import java.nio.file.Path;
import java.sql.Date;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
@@ -45,6 +46,8 @@ public class SomeNewTypesBean {
@Column(name = "month_day")
MonthDay monthDay;
java.sql.Date sqlDate;
LocalDate localDate;
LocalDateTime localDateTime;
@@ -123,6 +126,14 @@ public class SomeNewTypesBean {
this.monthDay = monthDay;
}
public Date getSqlDate() {
return sqlDate;
}
public void setSqlDate(Date sqlDate) {
this.sqlDate = sqlDate;
}
public LocalDate getLocalDate() {
return localDate;
}
@@ -42,6 +42,7 @@ public class TestNewTypes extends BaseTestCase {
bean.setOffsetDateTime(OffsetDateTime.now());
bean.setZonedDateTime(ZonedDateTime.now());
bean.setLocalTime(LocalTime.now());
bean.setSqlDate(java.sql.Date.valueOf("2019-07-21"));
bean.setInstant(Instant.now());
bean.setYear(Year.now());
bean.setMonth(Month.APRIL);
@@ -77,6 +78,9 @@ public class TestNewTypes extends BaseTestCase {
list = DB.find(SomeNewTypesBean.class).where().lt("zonedDateTime", ZonedDateTime.now()).findList();
assertTrue(!list.isEmpty());
list = DB.find(SomeNewTypesBean.class).where().eq("sqlDate", bean.getSqlDate()).findList();
assertTrue(!list.isEmpty());
list = DB.find(SomeNewTypesBean.class).where().le("localTime", LocalTime.now()).findList();
assertTrue(!list.isEmpty());
@@ -113,6 +117,7 @@ public class TestNewTypes extends BaseTestCase {
assertEquals(bean.getZoneOffset(), fetched.getZoneOffset());
assertEquals(bean.getMonth(), fetched.getMonth());
assertEquals(bean.getYear(), fetched.getYear());
assertEquals(bean.getSqlDate(), fetched.getSqlDate());
assertEquals(bean.getYearMonth(), fetched.getYearMonth());
assertEquals(bean.getMonthDay(), fetched.getMonthDay());
assertEquals(bean.getLocalDate(), fetched.getLocalDate());
@@ -121,7 +121,7 @@ public class TestSqlUpdateUpsert extends BaseTestCase {
EPersonOnline found2 = Ebean.find(EPersonOnline.class, key2);
assertThat(found2).isNotNull();
assertThat(found2.getId()).isEqualTo(key);
assertThat(found2.getId().toString()).isEqualTo(key.toString());
assertThat(found2.getEmail()).isEqualTo("bar@one.com");
assertThat(found2.isOnlineStatus()).isFalse();
+1 -1
View File
@@ -138,7 +138,7 @@ datasource.mysql.databaseUrl=jdbc:mysql://127.0.0.1:4306/unit
#datasource.mysql.username=test_ebean
#datasource.mysql.password=test
#datasource.mysql.databaseUrl=jdbc:mysql://127.0.0.1:4306/test_ebean
datasource.mysql.databaseDriver=com.mysql.jdbc.Driver
datasource.mysql.databaseDriver=com.mysql.cj.jdbc.Driver
datasource.oracle.username=test_ebean