-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OffsetDateTimeTypeHandler or ZonedDateTimeTypeHandler loses time zone information #1081
Comments
I agree. These cases are not well covered at the moment. Not sure if TypeHandlers support parameters, but I like the idea you proposed. JDBC 4.2 (Java 8 only) does support There is no support for AFAIK if you need to support clients in different timezones there is no way around storing all time related data types in UTC and have the clients do the proper conversion to their local time. Edit Could create a proper pull request but I would like to get some more feedback on this issue. Reference: https://github.com/raupachz/mybatis-3/tree/ISSUE-1081 |
Sorry for the late reply but here is some feedback from me: I think the Also, the current JUnit test "covering" this type handler does not have a fixed date that is being converted but simply uses the current date (and time zone) from the computer that is executing the JUnit test. Of course it will work then! So at the very least we should:
Here are the two alternative implementations I would suggest:
What do you think? I can create a pull request for the second one and maybe you for the first one? |
I agree with In my opinion the best approach in storing both Temporals as String columns in the database: most portable and straight forward. In the TypeHandler one should use
Since this change could break applications it is probably a fix for a MyBatis 3.5.x release. |
Currently working w/h MSSQL 2017 here and I can confirm that the JDBC 4.2 I tend to agree with @Treppenhouse's comment and I'd like to add that IMHO the I know this could be a breaking change, but it seems to me the most logical. Again, that being said as a humble user opinion. Just for reference. New JDBC 4.2 supported types as documented in: |
Hi @harawata oh wow I almost forgot about this branch. Yes, I will rebase and send a PR. Thanks! |
@raupachz , @petarov ,
My environment:
|
@harawata @raupachz No need to provide a gist, because the exception you posted is totally real! I need to apologise for my misleading comment above. I checked my logs yesterday and I did see the same exception, but I only got to review what's going on today. So as it turns out Microsoft's JDBC did in fact implement the Needless to say, this defeats the purpose of a uniform For anyone that is more interested in the topic this is what I found.
|
@petarov Thanks for the detailed investigation! I always thought with JDBC 4.2 this should be easily done. Guess, not. Maybe, we better close the PR. Doesn't make sense to add a feature if only half of the db vendors implement the spec. Side note: I checked the source of MariaDB Connector/J they implement |
@petarov , @raupachz , I'm going to test |
Anyway, I'm actually not a big fan of that approach. I'm curious about what other devs think. [1] Not with MS SQL Server right now, but @petarov shared a decent workaround. |
I personally see this as a separate handler, something like I'm also not sure what's to be done with Btw, big thanks for your test results in #1368. This is awesome! 🍺 |
Adding another type handler that maps This is just my personal view, but type handlers for these classes (i.e. As this is just my personal opinion and could be impractical, I would like to wait for other committers' comment before we proceed.
You're very welcome. I learned a lot from this discussion, too. =) FYI, I noticed that there is a bug in mssql-jdbc's |
@harawata Nice! I hope they pull it in. It would save me from using a custom handler. It's currently all over my mappers. 😉 Btw, this was my routine to convert from dto to odt. private OffsetDateTime toOffsetDateTime(DateTimeOffset dto) {
if (dto != null) {
return OffsetDateTime.ofInstant(dto.getTimestamp().toInstant(),
ZoneOffset.ofHours(dto.getMinutesOffset() / 60));
}
return null;
} |
That's my plan B if they don't like me modifying
It might not be what you meant, but instead of specifying |
…d ResultSet#getObject() Related to mybatis#1081
… and ResultSet#getObject() Related to mybatis#1081
OffsetTimeTypeHandler #1437 and ZonedDateTimeHandler #1438 have been updated to use |
@harawata I've tested the Thanks a lot for this! 🍻 |
@petarov Thank you for verifying the fix! |
I just upgraded to mybatis 3.5.0 and the ZonedDateTimeTypeHandler is not working. |
…d ResultSet#getObject() Related to mybatis#1081
… and ResultSet#getObject() Related to mybatis#1081
Hello,
I would assume that people using your implementation of a "ZonedDateTimeTypeHandler" expect it to convert dates with timezone information in their database to EQUIVALENT Java objects, which it does not (really).
It merely converts them to objects that, technically point to the same moment in time, however with the zone or offset information completely lost. All the
ZonedDateTime
orOffsetDateTime
objects that the handlers create always refer to the system default time zone, never to the one of the actual entry!Instead of only using the
getTimestamp()
method of theResultSet
(which can only return ajava.sql.Timestamp
with the zone information already lost) it would be cool if you could at least offer an alternative implementation that tries to retain the offset/timezone information by using thegetString()
method instead.An example for the OffsetDateTimeTypeHandler could be:
I found the idea here. Of course, this would assume that the String representation of the databases for those "timestamp with time zone" datatypes comply with the ISO format. Instead of guessing users could hand over the format-mask to a method that converts to the String representation of that column in the select statement. E.g. in Oracle that could be:
SELECT to_char(table0.MY_DATE_COLUMN, 'YYYY-MM-DD"T"HH24:MI:SSFFTZH:TZM') as MY_DATE_COLUMN)
. Such a remark could be added in the class documentation of the type handler, for example.Even if you do not want to add such handlers, you should at least mention in your documentation (class documentation and README.MD) that the handlers you provide do NOT retain the information of the original timezone or the original offset but do always convert them to the system's default.
Cheers!
Note
This issue moved from mybatis/typehandlers-jsr310#39.
The text was updated successfully, but these errors were encountered: