views:

170

answers:

4

We have your bog standard Java app under development, and a lot of the records we're creating (Hibernate entities in MySQL) have 'created' and 'modified' timestamps on them.

Now, me and one of the developers disagree - I believe that both of those fields should have a MySQL default of CURRENT_TIMESTAMP, and then the modified can be changed by the app. He wants both managed by the app.

Is there a compelling reason for either decision? I can't see why you'd want to add more explicit steps to the code, unless for some reason you were concerned about your servers (db, application) having inconsistent timestamps.

+3  A: 

It makes sense to allocate all timestamps in the same tier for consistency.

This answer to a related question gives a nice example of how to do it in Hibernate.

Robert Christie
+7  A: 
  • use DB timestamps if you can sacrifise portability. Also, if very small differences would matter to the application and you are going to have more than one application server / a cluster, you can have problems with synchronizing the nodes.
  • use the application to generate the timestamps in case it is not certain the the chosen DB will stay, or if you are using multiple databases - for example MySQL for production, HSQLDB for unit-tests.

If none of these arguments apply, use the one that's easier for you (or the one that more developers vote for)

If you go for the application handling, either go for Pascal Thivent's suggestion with @PreUpdate, or have your field set with a default value, like:

private Calendar date = Calendar.getInstance();
Bozho
+1 but can you elaborate on how this matters to cluster.
Pascal Thivent
I still don't get the cluster stuff. With or without 2nd level cache, why would you see different things on different nodes. If you have something specific, please clarify because this just doesn't seem true to me: clustering your application doesn't force you to use db timestamp in any way.
Pascal Thivent
well, you can set it to exactly the same everywhere, but it is an overhead + the DST and timezone options + the need to set it again on each new node. To be honest, I've only had a very specific experience with this scenario which doesn't apply to the common case, and we are using a timestamp server.
Bozho
Updated my answer so that it's not misleading. thanks.
Bozho
Ohhh, what you mean is that you can have clock sync issues between nodes, now I get it. Note that this has never been a problem for me though (you can sync servers with NTP and I've never worked in environments where a nano second difference was so important). But, if that nano second is critical, then indeed, it may be better to do it at the database level. Thanks for the clarification.
Pascal Thivent
+3  A: 

I would handle this from the code and use Hibernate/JPA's @PrePersist and @PreUpdate callbacks:

@PreUpdate
@PrePersist
public void setTimeStamps() {
    modified = new Date();
    if (created==null) {
      created = new Date();
    }
}

Main reason: portability (i.e. this will work with another database, for example in a testing context, without any database magic, no DEFAULT, no trigger, nothing).

Pascal Thivent
A: 

I would use triggers and store procedures to maintain these fields. So put the logic in the DB.

chburd