Bug #102435 java.time.LocalDateTime cannot be cast to class java.lang.String
Submitted: 1 Feb 2021 9:55 Modified: 1 Feb 2021 12:58
Reporter: Patrik Schwieger Email Updates:
Status: Not a Bug Impact on me:
Category:Connector / J Severity:S2 (Serious)
Version:8.0.23 OS:Any
Assigned to: CPU Architecture:Any

[1 Feb 2021 9:55] Patrik Schwieger
I'm using Keycloak with Liquibase and since MySQL Connector/J version 8.0.23 there is a ClassCastException.

This is different to https://bugs.mysql.com/bug.php?id=101413 where
"cannot be cast to java.sql.Timestamp" occured.
Here we have a:
"cannot be cast to class java.lang.String"

related part of stack trace:
Caused by: java.lang.ClassCastException: class java.time.LocalDateTime cannot be cast to class java.lang.String (java.time.LocalDateTime and java.lang.String are in module java.base of loader 'bootstrap')
        at org.liquibase//liquibase.changelog.StandardChangeLogHistoryService.getRanChangeSets(StandardChangeLogHistoryService.java:287)
        at org.liquibase//liquibase.database.AbstractJdbcDatabase.getRanChangeSetList(AbstractJdbcDatabase.java:1124)
        at org.liquibase//liquibase.changelog.DatabaseChangeLog.validate(DatabaseChangeLog.java:257)
        at org.liquibase//liquibase.Liquibase.listUnrunChangeSets(Liquibase.java:1189)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)

All products using Liquibase seems to have this problem and there are two issues on Github:

But both where closed in a manner, that the problem seems to be in MySQL Connector/J

How to repeat:
Install Keycloak and use MySQL Module with Connector/J
In version <= 8.0.22 --> it's ok
in version == 8.0.23 --> ClassCastException: class java.time.LocalDateTime cannot be cast to class java.lang.String

Suggested fix:
If you are not willing to change back to old behaviour, state it in release note somehow as incompatible change.
[1 Feb 2021 12:56] Alexander Soklakov
Hi Patrik,

Yes, that's the intended change, and we already added the note to the changelog https://dev.mysql.com/doc/relnotes/connector-j/8.0/en/news-8-0-23.html:

Functionality Added or Changed -> Important -> "Also, with the implementation of the new mechanism, a getObject(columnIndex) call on a DATETIME column returns a LocalDateTime object now instead of a String. To receive a String like before, use getObject(columnIndex, String.class) instead."
[23 Feb 2021 1:07] Scott Jibben
This change breaks the MySQL implementation in Adobe ColdFusion.

For query resultsets, a java.time.LocalDateTime object is returned instead of a datetime string.

Reverting to 8.0.22 fixed this problem.
[2 Mar 2021 22:06] Adrian with an A
I used Spring Boot Parent Starter 2.4.3, which contains MySQL 8.0.23.

Scenario 1:
A clean database + start Spring Boot project = OK.

Scenario 2:
A clean database + start Spring Boot project = OK.
Shut down project.
Database has schema with data + start Spring Boot project = fail. Same problem as OP.

Revert Spring Boot Starter Parent to 2.4.2.
Scenario 1:
Start and stop the project as many times as you would like. It will always work.