func commitTimestamp(projectId, instanceId, databaseId string) error {
	ctx := context.Background()
	db, err := sql.Open("spanner", fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId))
	if err != nil {
		return err
	}
	defer db.Close()

	// Get a connection from the pool and start the transaction on that connection.
	conn, err := db.Conn(ctx)
	if err != nil {
		return fmt.Errorf("failed to get a connection from the pool: %v", err)
	}
	defer conn.Close()

	tx, err := conn.BeginTx(ctx, &sql.TxOptions{})
	if err != nil {
		return err
	}
	if _, err := tx.ExecContext(ctx, "INSERT INTO Singers (SingerId, Name) VALUES (@id, @name)", 123, "Bruce Allison"); err != nil {
		_ = tx.Rollback()
		return fmt.Errorf("failed to insert test record: %v", err)
	}
	if err := tx.Commit(); err != nil {
		return fmt.Errorf("failed to commit transaction: %v", err)
	}

	// Get the commit timestamp of the last transaction that was executed on the connection.
	var ct time.Time
	if err := conn.Raw(func(driverConn interface{}) (err error) {
		ct, err = driverConn.(spannerdriver.SpannerConn).CommitTimestamp()
		return err
	}); err != nil {
		return fmt.Errorf("failed to get commit timestamp: %v", err)
	}

	// The commit timestamp can also be obtained by executing the custom SQL statement `SHOW VARIABLE COMMIT_TIMESTAMP`
	var ct2 time.Time
	if err := conn.QueryRowContext(ctx, "SHOW VARIABLE COMMIT_TIMESTAMP").Scan(&ct2); err != nil {
		return fmt.Errorf("failed to execute SHOW VARIABLE COMMIT_TIMESTAMP")
	}

	// The commit timestamp that is obtained directly from the connection is in the local timezone.
	fmt.Printf("Transaction committed at                %v\n", ct)
	// The commit timestamp that is obtained through SHOW VARIABLE COMMIT_TIMESTAMP is in UTC.
	fmt.Printf("SHOW VARIABLE COMMIT_TIMESTAMP returned %v\n", ct2)

	return nil
}
// Example for getting the commit timestamp of a read/write transaction.
//
// Execute the sample with the command `go run main.go` from this directory.
Example for getting the commit timestamp of a read/write transaction.

Execute the sample with the command `go run main.go` from this directory.
// Example for getting the commit timestamp of a read / write transaction . // // Execute the sample with the command ` go run main . go ` from this directory .
func commitTimestamp(projectId, instanceId, databaseId string) error {
	ctx := context.Background()
	db, err := sql.Open("spanner", fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId))
	if err != nil {
		return err
	}
	defer db.Close()
	conn, err := db.Conn(ctx)
	if err != nil {
		return fmt.Errorf("failed to get a connection from the pool: %v", err)
	}
	defer conn.Close()
	tx, err := conn.BeginTx(ctx, &sql.TxOptions{})
	if err != nil {
		return err
	}
	if _, err := tx.ExecContext(ctx, "INSERT INTO Singers (SingerId, Name) VALUES (@id, @name)", 123, "Bruce Allison"); err != nil {
		_ = tx.Rollback()
		return fmt.Errorf("failed to insert test record: %v", err)
	}
	if err := tx.Commit(); err != nil {
		return fmt.Errorf("failed to commit transaction: %v", err)
	}
	var ct time.Time
	if err := conn.Raw(func(driverConn interface{}) (err error) {
		ct, err = driverConn.(spannerdriver.SpannerConn).CommitTimestamp()
		return err
	}); err != nil {
		return fmt.Errorf("failed to get commit timestamp: %v", err)
	}
	var ct2 time.Time
	if err := conn.QueryRowContext(ctx, "SHOW VARIABLE COMMIT_TIMESTAMP").Scan(&ct2); err != nil {
		return fmt.Errorf("failed to execute SHOW VARIABLE COMMIT_TIMESTAMP")
	}
	fmt.Printf("Transaction committed at                %v\n", ct)
	fmt.Printf("SHOW VARIABLE COMMIT_TIMESTAMP returned %v\n", ct2)
	return nil
}
func commitTimestamp ( projectId , instanceId , databaseId string ) error { ctx := context . Background ( ) db , err := sql . Open ( " " , fmt . Sprintf ( " " , projectId , instanceId , databaseId ) ) if err != nil { return err } defer db . Close ( ) conn , err := db . Conn ( ctx ) if err != nil { return fmt . Errorf ( " " , err ) } defer conn . Close ( ) tx , err := conn . BeginTx ( ctx , & sql . TxOptions { } ) if err != nil { return err } if _ , err := tx . ExecContext ( ctx , " " , 123 , " " ) ; err != nil { _ = tx . Rollback ( ) return fmt . Errorf ( " " , err ) } if err := tx . Commit ( ) ; err != nil { return fmt . Errorf ( " " , err ) } var ct time . Time if err := conn . Raw ( func ( driverConn interface { } ) ( err error ) { ct , err = driverConn . ( spannerdriver . SpannerConn ) . CommitTimestamp ( ) return err } ) ; err != nil { return fmt . Errorf ( " " , err ) } var ct2 time . Time if err := conn . QueryRowContext ( ctx , " " ) . Scan ( & ct2 ) ; err != nil { return fmt . Errorf ( " " ) } fmt . Printf ( " \n " , ct ) fmt . Printf ( " \n " , ct2 ) return nil }
Example for getting the commit timestamp of a read/write transaction.
Example for getting the commit timestamp of a read / write transaction .
// Get a connection from the pool and start the transaction on that connection. // Get the commit timestamp of the last transaction that was executed on the connection. // The commit timestamp can also be obtained by executing the custom SQL statement `SHOW VARIABLE COMMIT_TIMESTAMP` // The commit timestamp that is obtained directly from the connection is in the local timezone. // The commit timestamp that is obtained through SHOW VARIABLE COMMIT_TIMESTAMP is in UTC.