[ Web Proxy ]
URL:
Viewing: https://cloud.google.com/solutions/using-spanner-commit-timestamp [Back]  [Original]

Create a change log with JDBC using the Spanner commit timestamp feature  |  Solutions  |  Google Cloud Documentation Skip to main content
Google Cloud Documentation [Google Cloud Documentation]
Send feedback

Create a change log with JDBC using the Spanner commit timestamp feature Stay organized with collections Save and categorize content based on your preferences.

This document describes how to use the commit timestamp feature to track the date and time when changes are made to your database records. When you have a large database with many transactions that change records and you want to capture changes made to the datasets, the commit timestamp feature simplifies this task.

Through a series of code samples, this document shows you how to rewrite existing data manipulation language (DML) to create a change log. The document extends Using Cloud Spanner commit timestamps to create a change log with Go by demonstrating the methods using Java and JDBC. The logic is represented in SQL rather than as Spanner client objects.

Note: An alternative to the solutions proposed on this page is Spanner change streams. Spanner change streams watch and stream out a Spanner database's data changes to other services. For more information, see About change streams.

How the tables work

Assume you have a facts table called msgs and a history table called msg_history. Each time the facts table gets modified, you want to concurrently save a record in the msg_history table. You can use the contents of the history table later for other usesfor example, for auditing or as an event source.

The msgs table holds the facts, represented by a transaction ID (id) and the message (msg). The data definition language (DDL) statement that you use to create the table looks like the following:

CREATE TABLE msgs (
    id INT64,
    msg STRING(MAX),
) PRIMARY KEY (id)

The msg_history table holds the transaction history. The following DDL creates the history table. The column ts stores the commit timestamp.

CREATE TABLE msgs_history (
    id INT64,
    ts TIMESTAMP OPTIONS (allow_commit_timestamp=true),
    previous_msg STRING(MAX)
) PRIMARY KEY (ts, id)

The examples

Instead of writing only to the facts table, you must do the operation to the history table in the same transaction. Spanner's JDBC driver supports declaration of the start and end of a transaction, which is a standard JDBC operation.

Step 1: Rewrite the insert operations

The first step is to rewrite the insert operations from the following form:

insert into msgs (id, msg) values (1, 'a real msg')

Step 2: Rewrite the update operations

Next, you rewrite the update operations from the following form:

update msgs set msg = 'new message' where id = 1

Step 3: Rewrite the delete operations

Finally, you rewrite the delete operations from the following form:

delete from msgs where id = 1

Using the history table

What's next

Send feedback

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-08-11 UTC.

Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-08-11 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page