Search code examples
javadatabaselineh2last-modified

Finding out when a line has been modified in a H2 database table


Let's imagine I have two processes A and B performing transactions on a H2 database table T.

Process A performs CRUD (Create, Read, Update, Delete) on T.

Process B wants to know when line L in T has been last modified (i.e., B provides a System.currentTimeMillis() value for example).

One could create a column in T registering the last modification moment for each line, but I was wondering whether H2 was already holding this information somewhere and whether it can be accessed.


Solution

  • For my knowledge there is no such feature in H2 (and probably not in any RDBMS). The reason is simple - extra 4 or 8 bytes per each record might have a huge impact on overall database size, especially with small records. Also there will be a slight performance impact.

    But it is relatively simple to implement this feature by using extra column and on update trigger. Also some databases might simplify it even further, like MySQL:

    ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP
    

    Also please distinguish between database server clock, process A clock and process B clock. In real world they most likely won't be the same.