sql

lepistane 2023-03-27T21:06:05.922459Z

I am having quite a weird behavior with next.jdbc. Maria db timestamp is the field. i store UTC date and when i query i get #inst that's -1h ? Why is that and how can i fix it ?

lepistane 2023-03-30T15:07:57.139749Z

solved with

:jvm-opts ["-Duser.timezone=UTC"]

👍🏻 1
2023-03-27T21:07:17.805459Z

if you aren't storing a timezone with the date, you need to make sure the jvm's timezone is the same as the databases

2023-03-27T21:08:43.102859Z

user=> (.getID (java.util.TimeZone/getDefault))
"America/Los_Angeles"
user=>

lepistane 2023-03-27T21:10:20.305649Z

how can i check if i am storing timezone in the db ?

lepistane 2023-03-27T21:11:04.076399Z

Oke so mariadb timestamp is UTC always

lepistane 2023-03-27T21:11:40.979749Z

My current is GMT+2

lepistane 2023-03-27T21:12:11.092059Z

stored in db: 2023-01-02 13:52:00.000 i query it it becomes 2023-01-02 12:52:00.000

2023-03-27T21:15:29.537369Z

the other thing to keep in mind is regardless of the timezone of a data object, the #inst printer in clojure normalizes it to utc

2023-03-27T21:16:14.399579Z

so if you are constructing dates like (java.util.Date.) it will print in utc, but actually be in whatever timezone the jvm is running in

lepistane 2023-03-27T21:17:06.186459Z

shouldn't this solve all problems?

(defn ->time-str [s]
  (time/format
   (time/with-zone (time/formatter "yyyy-MM-dd HH:mm:ss") "UTC")
   (.toInstant s)))

2023-03-27T21:17:21.298609Z

solve which problem?

lepistane 2023-03-27T21:17:50.402819Z

stored in db: 2023-01-02 135200.000 i query it it becomes 2023-01-02 125200.000

2023-03-27T21:18:06.067559Z

the solution to this problem is to make sure the jvm process has the same timezone as the database

lepistane 2023-03-27T21:18:35.709859Z

how do i do this?

2023-03-27T21:18:37.859209Z

some jdbc drivers (I dunno about maria) do timezone conversion internally on dates

lepistane 2023-03-27T21:19:15.064899Z

Is there a next.jdbc option that offers 'here everything is in UTC' ?

2023-03-27T21:19:22.026179Z

there is some jvm property you can set I believe, generally on unix like systems you can set the TZ environment variable for the process, etc

2023-03-27T21:19:31.526989Z

it isn't something next.jdbc is doing

lukasz 2023-03-27T21:19:37.396259Z

-Duser.timezone=UTC is the Java option that you can use

2023-03-27T21:19:45.643349Z

it is something the mariadb jdbc driver is doing

2023-03-27T21:26:16.617949Z

there are some properties mentioned on https://jira.mariadb.org/browse/CONJ-433 that you can set on your connection, but I haven't used them, and I am not sure how to pass them to next.jdbc so it passes them down to the mariadb jdbc driver

seancorfield 2023-03-27T21:31:01.102359Z

If you're using a db-spec hash map, just add them to that. If you're using a JDBC URL, add them to that.

seancorfield 2023-03-27T21:31:24.997319Z

If you're using a connection pooling library, you might need to do something different.

seancorfield 2023-03-27T21:33:06.905679Z

Bottom line: you'll always get weird TZ behavior if you don't have your JVM and your DB set to the same timezone. It's why we have all our QA and production servers set to UTC at the O/S level so the DB and the JVM inherit that.

lepistane 2023-03-27T21:33:54.027139Z

@seancorfield I was looking into https://mariadb.com/kb/en/about-mariadb-connector-j/#timezone-consideration no connection pool lib i tried adding it to url and also map it failed. Example

"localhost/?timezone=UTC"
it failed What do you do with local development? Use java option for UTC timezone?

seancorfield 2023-03-27T21:38:36.654549Z

"i tried adding it to url and also map it failed" -- what exactly did you try and how exactly did it "fail"?

seancorfield 2023-03-27T21:38:48.966499Z

Also, where is your DB running for development?

jumar 2023-03-28T05:28:46.645659Z

If you are interested in learning more, this is a great article: https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/

👍 1
👍🏼 1
lepistane 2023-03-28T09:57:59.540639Z

@seancorfield i tried adding time zone like i mentioned from described in the link https://mariadb.com/kb/en/about-mariadb-connector-j/#timezone-consideration like so

{:classname "org.mariadb.jdbc.MySQLDataSource",
 :dbname "dev",
 :dbtype "mariadb",
 :host "localhost/?timezone=UTC",
 :port 3307,
 :password "admin",
 :user "admin"}
and then when i try to execute
{:select [:*]
                                          :from [[:fixture :fms]]}
i get
Execution error (SQLException) at org.mariadb.jdbc.plugin.authentication.standard.CachingSha2PasswordPlugin/process (CachingSha2PasswordPlugin.java:141).
RSA public key is not available client side (option serverRsaPublicKeyFile not set)
So most likely i added this wrong I added it like
:timezone "UTC"
and same error happened. Everything i do i local. So i have DB running locally (UTC +2) JVM process is also local Values in DB are in UTC. Once queried they lose 1 hour.

seancorfield 2023-03-28T19:03:23.222359Z

"and same error happened." -- well, the error you showed about password plugin / RSA public key sounds like you're still passing the wrong stuff to the driver, so perhaps share the actual db-spec hash map you're trying now and we'll see what's wrong with it.

lepistane 2023-03-28T21:06:42.044169Z

@seancorfield it appears you were right.

{:classname "org.mariadb.jdbc.MySQLDataSource",
 :dbname "dev",
 :dbtype "mariadb",
 :host "localhost",
 :timezone "GMT+1",
 :port 3307,
 :password "admin",
 :user "admin"}
does produce proper results. Problem is i dont want any conversion which will have to tinker with tomorrow. Thank you for your time and patience

👍🏻 1
seancorfield 2023-03-29T00:20:58.624429Z

The docs for that :timezone setting seem to say you only need it if your client and server are in different timezones -- which shouldn't be the case when you're running both locally on your machine. Are you running both actually on your machine or is either of them in a VM? If so, you need to ensure that your VM(s) are all in the same TZ.

lepistane 2023-03-29T06:22:50.284849Z

DB is actually docker container which is running on my machine Clojure is on the machine itself. Do you think that could cause the difference?

seancorfield 2023-03-29T15:44:53.338539Z

Yes, that was exactly what I said above: ensure your Docker container is running in the same TZ as your host machine.

👍 1