forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit475c145
committed
Attached is a set of patches for a couple of bugs dealing with
timestamps in JDBC.Bug#1) Incorrect timestamp stored in DB if client timezone differentthan DB.The buggy implementation of setTimestamp() in PreparedStatement simplyused the toString() method of the java.sql.Timestamp object to convertto a string to send to the database. The format of this is yyyy-MM-ddhh:mm:ss.SSS which doesn't include any timezone information. Thereforethe DB assumes its timezone since none is specified. That is OK if thetimezone of the client and server are the same, however if they aredifferent the wrong timestamp is received by the server. For example ifthe client is running in timezone GMT and wants to send the timestampfor noon to a server running in PST (GMT-8 hours), then the server willreceive 2000-01-12 12:00:00.0 and interprete it as 2000-01-1212:00:00-08 which is 2000-01-12 04:00:00 in GMT. The fix is to send aformat to the server that includes the timezone offset. For simplicitysake the fix uses a SimpleDateFormat object with its timezone set to GMTso that '+00' can be used as the timezone for postgresql. This is doneas SimpleDateFormat doesn't support formating timezones in the waypostgresql expects.Bug#2) Incorrect handling of partial seconds in getting timestamps fromthe DBWhen the SimpleDateFormat object parses a string with a format likeyyyy-MM-dd hh:mm:ss.SS it expects the fractional seconds to be threedecimal places (time precision in java is miliseconds = three decimalplaces). This seems like a bug in java to me, but it is unlikely to befixed anytime soon, so the postgresql code needed modification tosupport the java behaviour. So for example a string of '2000-01-1212:00:00.12-08' coming from the database was being converted to atimestamp object with a value of 2000-01-12 12:00:00.012GMT-08:00. Thefix was to check for a '.' in the string and if one is found append onan extra zero to the fractional seconds part.Bug#3) Performance problemsIn fixing the above two bugs, I noticed some things that could beimproved. In PreparedStatement.setTimestamp(),PreparedStatement.setDate(), ResultSet.getTimestamp(), andResultSet.getDate() these methods were creating a new SimpleDateFormatobject everytime they were called. To avoid this unnecessary objectcreation overhead, I changed the code to use static variables forkeeping a single instance of the needed formating objects.Also the code used the + operator for string concatenation. As everyoneshould know this is very inefficient and the use of StringBuffers isprefered.I also did some cleanup in ResultSet.getTimestamp(). This method hashad multiple patches applied some of which resulted in code that was nolonger needed. For example the ISO timestamp format that postgresqluses specifies the timezone as an offset like '-08'. Code was added atone point to convert the postgresql format to the java one which isGMT-08:00, however the old code was left around which did nothing. Sothere was code that looked for yyyy-MM-dd hh:mm:sszzzzzzzzz andyyyy-MM-dd hh:mm:sszzz. This second format would never be encounteredbecause zzz (i.e. -08) would be converted into the former (also notethat the SimpleDateFormat object treats zzzzzzzzz and zzz the same, thenumber of z's does not matter).There was another problem/fix mentioned on the email lists today bymcannon@internet.com which is also fixed by this patch:Bug#4) Fractional seconds lost when getting timestamp from the DBA patch by Jan Thomea handled the case of yyyy-MM-dd hh:mm:sszzzzzzzzzbut not the fractional seconds version yyyy-MM-dd hh:mm:ss.SSzzzzzzzzz.The code is fixed to handle this case as well.Barry Lind1 parent20dfd50 commit475c145
File tree
4 files changed
+117
-57
lines changed- src/interfaces/jdbc/org/postgresql
- jdbc1
- jdbc2
4 files changed
+117
-57
lines changedLines changed: 13 additions & 6 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
310 | 310 |
| |
311 | 311 |
| |
312 | 312 |
| |
| 313 | + | |
313 | 314 |
| |
314 | 315 |
| |
315 |
| - | |
316 |
| - | |
317 |
| - | |
318 |
| - | |
| 316 | + | |
| 317 | + | |
319 | 318 |
| |
320 | 319 |
| |
321 | 320 |
| |
| |||
349 | 348 |
| |
350 | 349 |
| |
351 | 350 |
| |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
352 | 357 |
| |
353 |
| - | |
354 |
| - | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
355 | 362 |
| |
356 | 363 |
| |
357 | 364 |
| |
|
Lines changed: 46 additions & 18 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
412 | 412 |
| |
413 | 413 |
| |
414 | 414 |
| |
415 |
| - | |
416 | 415 |
| |
417 |
| - | |
| 416 | + | |
418 | 417 |
| |
419 | 418 |
| |
420 | 419 |
| |
| |||
457 | 456 |
| |
458 | 457 |
| |
459 | 458 |
| |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
460 | 464 |
| |
461 | 465 |
| |
462 | 466 |
| |
463 | 467 |
| |
464 | 468 |
| |
465 |
| - | |
466 |
| - | |
467 |
| - | |
468 |
| - | |
469 |
| - | |
470 |
| - | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
471 | 498 |
| |
472 |
| - | |
473 |
| - | |
474 |
| - | |
475 |
| - | |
476 |
| - | |
477 |
| - | |
478 |
| - | |
479 |
| - | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
480 | 508 |
| |
481 |
| - | |
| 509 | + | |
482 | 510 |
| |
483 |
| - | |
| 511 | + | |
484 | 512 |
| |
485 | 513 |
| |
486 | 514 |
| |
|
Lines changed: 13 additions & 6 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
310 | 310 |
| |
311 | 311 |
| |
312 | 312 |
| |
| 313 | + | |
313 | 314 |
| |
314 | 315 |
| |
315 |
| - | |
316 |
| - | |
317 |
| - | |
318 |
| - | |
| 316 | + | |
| 317 | + | |
319 | 318 |
| |
320 | 319 |
| |
321 | 320 |
| |
| |||
349 | 348 |
| |
350 | 349 |
| |
351 | 350 |
| |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
352 | 357 |
| |
353 |
| - | |
354 |
| - | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
355 | 362 |
| |
356 | 363 |
| |
357 | 364 |
| |
|
Lines changed: 45 additions & 27 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
415 | 415 |
| |
416 | 416 |
| |
417 | 417 |
| |
418 |
| - | |
419 | 418 |
| |
420 |
| - | |
| 419 | + | |
421 | 420 |
| |
422 | 421 |
| |
423 | 422 |
| |
| |||
460 | 459 |
| |
461 | 460 |
| |
462 | 461 |
| |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
463 | 467 |
| |
464 | 468 |
| |
465 | 469 |
| |
466 | 470 |
| |
467 | 471 |
| |
468 |
| - | |
469 |
| - | |
470 |
| - | |
471 |
| - | |
472 |
| - | |
473 |
| - | |
474 |
| - | |
475 |
| - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
476 | 497 |
| |
477 |
| - | |
478 |
| - | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
479 | 501 |
| |
480 | 502 |
| |
481 |
| - | |
482 |
| - | |
483 |
| - | |
484 |
| - | |
485 |
| - | |
486 |
| - | |
487 |
| - | |
488 |
| - | |
489 |
| - | |
490 |
| - | |
491 |
| - | |
492 |
| - | |
493 |
| - | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
494 | 511 |
| |
495 |
| - | |
| 512 | + | |
496 | 513 |
| |
497 |
| - | |
| 514 | + | |
498 | 515 |
| |
499 | 516 |
| |
500 | 517 |
| |
501 | 518 |
| |
502 | 519 |
| |
503 | 520 |
| |
| 521 | + | |
504 | 522 |
| |
505 | 523 |
| |
506 | 524 |
| |
|
0 commit comments
Comments
(0)