Skip to content

Lock and unlock writing data way

Nam Hoang edited this page Apr 1, 2024 · 2 revisions

Introduction

When the data is set up successfully on the server, we do not want to lose it by accident or by mistake of the someone who updates the data by testing the code. So, we need to lock the data to prevent the data from being updated by mistake. In this notebook, I will show you how to lock and unlock the data using the SQL command.

Connection to the database

The credential is in https://github.com/gs-gs/fa-ag-trace/wiki/DLR's-variables

How to lock/unlock the data

To lock or unlock the data, first, you need to connect to the database. Then you can use the SQL command to lock or unlock the data.

  1. Lock the data
BEGIN TRANSACTION

UPDATE
	GL
SET
	GL.LOCKED = 1,
	GL.DATE_LAST_UPDATED = CURRENT_TIMESTAMP
FROM
	[farm-dlr-db].DBO.GLOBAL_LOCKS AS GL
WHERE
	GL.NAME = 'WRITE';

UPDATE
	GL
SET
	GL.LOCKED = 1,
	GL.DATE_LAST_UPDATED = CURRENT_TIMESTAMP
FROM
	[feedlot-dlr-db].DBO.GLOBAL_LOCKS AS GL
WHERE
	GL.NAME = 'WRITE';

UPDATE
	GL
SET
	GL.LOCKED = 1,
	GL.DATE_LAST_UPDATED = CURRENT_TIMESTAMP
FROM
	[processor-dlr-db].DBO.GLOBAL_LOCKS AS GL
WHERE
	GL.NAME = 'WRITE';


COMMIT;
  1. Unlock the data
BEGIN TRANSACTION

UPDATE
	GL
SET
	GL.LOCKED = 0,
	GL.DATE_LAST_UPDATED = CURRENT_TIMESTAMP
FROM
	[farm-dlr-db].DBO.GLOBAL_LOCKS AS GL
WHERE
	GL.NAME = 'WRITE';

UPDATE
	GL
SET
	GL.LOCKED = 0,
	GL.DATE_LAST_UPDATED = CURRENT_TIMESTAMP
FROM
	[feedlot-dlr-db].DBO.GLOBAL_LOCKS AS GL
WHERE
	GL.NAME = 'WRITE';

UPDATE
	GL
SET
	GL.LOCKED = 0,
	GL.DATE_LAST_UPDATED = CURRENT_TIMESTAMP
FROM
	[processor-dlr-db].DBO.GLOBAL_LOCKS AS GL
WHERE
	GL.NAME = 'WRITE';


COMMIT;