-
1. Re: Persistence of Date values depends on default time zone
epbernard Feb 16, 2006 4:58 AM (in response to oglueck)Your JDBC driver does the translation
Check your drivers documentation to work around that
or inverse the effect through a @Type usertype -
2. Re: Persistence of Date values depends on default time zone
maxandersen Feb 16, 2006 5:18 AM (in response to oglueck)
@TemporalType(TIMESTAMP)
Date getX()
tells it to use resultset.setTimestamp() instead of resultset.setDate() -
3. Re: Persistence of Date values depends on default time zone
oglueck Feb 16, 2006 5:22 AM (in response to oglueck)This is partly true only! JDBC offers methods:
PreparedStatement.setDate(int, Date, Calendar)
ResultSet.getDate(int, Calendar)
that can be used to control time zone conversions. I have done that when I used JDBC in the past. So the persistence layer clearly has the possibility to address this problem. I see this as a poor excuse for a programming error in org.hibernate.type.DateType and CalendarDateType.
I am only observing entity beans (not the DB directly). I shouldn't need to care if the persistence uses JDBC, XML or does all in-memory. The persistence layer must hide such implementation details from the user. -
4. Re: Persistence of Date values depends on default time zone
oglueck Feb 16, 2006 5:33 AM (in response to oglueck)Max, what you mean is
@Basic(temporalType=TemporalType.TIMESTAMP)
but as Timestamp follows the exact same semantics in JDBC as Date and Calendar it's persistence is as broken. -
5. Re: Persistence of Date values depends on default time zone
maxandersen Feb 16, 2006 6:38 AM (in response to oglueck)afaik SQL TIMESTAMP is stored as UTC.
"Therefore, datetime data types that contain time fields (TIME and TIMESTAMP) are maintained in Universal Coordinated Time (UTC), with
an explicit or implied time zone part." -
6. Re: Persistence of Date values depends on default time zone
epbernard Feb 16, 2006 7:28 AM (in response to oglueck)I'll ignore the fact that we spend time on a free forum answering people just to check that our code is buggy and give poor excuse to justify our shitty work.
The actual time zone that is useful is your client timezone. So you should take the Date, and appy the timezone offset diff between your server timezone and your user time zone more or less in the presentation layer. 2 different users might have 2 different timezones. I can 2nd level cache my entity beans and thus share them across several PC and thus several users (actually not the same reference, but that's another story). So I can't compute this timezone diff on a per entity bean basis, only on a per user rendering basis -
7. Re: Persistence of Date values depends on default time zone
oglueck Feb 16, 2006 7:41 AM (in response to oglueck)Emmanuel, the Date class does not imply any time zone semantics at all. It stores a point in time in an abstract way. It took Sun a little while to figure that out. This is why all methods in Date that implicitly imply a time zone have been deprecated years ago. The getTime and setTime methods convert this abstract representation to a millisecond value based on GMT. Adding a GMT time-offset to this value is therefore complete nonsense and a beginner's mistake at best.
-
8. Re: Persistence of Date values depends on default time zone
maxandersen Feb 16, 2006 8:02 AM (in response to oglueck)and why are we still discussing this when a SQL TIMESTAMP actually stores the UTC time ? (not GMT which is technically something different at times ;)
-
9. Re: Persistence of Date values depends on default time zone
oglueck Feb 16, 2006 8:06 AM (in response to oglueck)Max, I have tested this quickly with PostgreSQL (used for development) and found Timestamp behaves the same as Date and Calendar - the VM's default Tz is used to apply some conversion. I still need to verify this with Oracle (used for production DB).
-
10. Re: Persistence of Date values depends on default time zone
maxandersen Feb 16, 2006 8:25 AM (in response to oglueck)and getTime() returns different results ?
-
11. Re: Persistence of Date values depends on default time zone
oglueck Feb 16, 2006 8:52 AM (in response to oglueck)Yes. This is the test case:
IGenericEntityManagement dataManager = lookup(IGenericEntityManagement.class); ITestBean testBean = lookup(ITestBean.class); TimeZone defaultTz = testBean.getDefaultTz(); Parameter p1 = new Parameter(); p1.setDate(new Date()); p1 = dataManager.createEntity(p1); TimeZone weirdTz = TimeZone.getTimeZone("GMT+09:37"); testBean.setDefaultTz(weirdTz); Parameter p2 = dataManager.getEntity(p1.getId()); assertEquals("Persistence depends on server's default time zone", p1.getDate().getTime(), p2.getDate().getTime());
(dataManager and testBean are two session beans whose method names should be self-explanatory) -
12. Re: Persistence of Date values depends on default time zone
maxandersen Feb 16, 2006 9:18 AM (in response to oglueck)i cannot make this fail with TIMESTAMP.
-
13. Re: Persistence of Date values depends on default time zone
oglueck Feb 16, 2006 9:21 AM (in response to oglueck)what DB are you using, Max?
-
14. Re: Persistence of Date values depends on default time zone
epbernard Feb 16, 2006 10:08 AM (in response to oglueck)java.util.Date is TimeZone agnostic since 1.2, but JDBC getDate and setDate use the default VM timezone. so the offet should be defined between your user time zone and your VM default timezone.