31/05/2025

Introduction to SQL Server Transactions (Transaction Isolation Part 1)

What is a Transaction


First, let's take a look at what is a Transaction in SQL Server?

Imagine you need to perform several related database operations that must either all succeed or all fail together. For example, transferring money from one bank account to another. This involves two main steps:

1. Deduct the amount from the source account.

2. Add the amount to the destination account.

If step 1 succeeds but step 2 fails (maybe due to a network error or server crash), you'd end up with money disappearing from the source account but not appearing in the destination account! That's a disaster.

A SQL Server transaction groups these multiple steps into a single, logical unit of work. You tell SQL Server "Start a transaction here (BEGIN TRAN)", perform all the steps, and then say "Okay, everything worked, make it permanent (COMMIT TRAN)". If something goes wrong during the steps, you say "Cancel everything since I started (ROLLBACK TRAN)", and SQL Server undoes any changes made within that transaction.

You might already hear about ACID properties of a transaction. ACID properties are the 4 pillars of a transaction, which allows SQL Server (and other relational databases) to implement the feature correctly.

So what are these ACID properties. ACID stands for:

  • A - Atomicity
  • C - Consistency
  • I - Isolation
  • D - Durability

Atomicity

Think of it: "All or Nothing."

What it means: An atomic transaction is treated as a single, indivisible unit. Either all the operations within the transaction complete successfully and are permanently recorded in the database, or none of them are. If any part of the transaction fails, the entire transaction is cancelled, and the database is returned to the state it was in before the transaction started. This is called rolling back the transaction.

Why it's important: Prevents partial updates that could leave your data in an inconsistent or incorrect state.

SQL Server: SQL Server uses the transaction log to keep track of all changes made within a transaction. If a transaction needs to be rolled back, SQL Server uses the log to undo those changes. If an error occurs during a transaction, SQL Server will often automatically initiate a rollback, or you can explicitly issue a ROLLBACK TRAN command.

Example (Bank Transfer):
○ You start the transfer transaction (BEGIN TRAN).
○ You successfully deduct $100 from Account A.
○ BUT before you can add $100 to Account B, the database server crashes.
Because of Atomicity, when the server restarts and recovers, it sees that the transaction wasn't completed (it wasn't COMMITted). It will use the transaction log to undo the deduction from Account A. Account A's balance will be back to what it was before the transaction. It's as if the transaction never happened.


Consistency

Think of it: "Valid State to Valid State."
What it means: A transaction brings the database from one valid state to another valid state. This doesn't mean the transaction logic itself is perfect (you could accidentally transfer the wrong amount!), but it ensures that the transaction adheres to all defined database rules and constraints. These rules include things like:
○ Constraints: PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK constraints (e.g., Balance must be >= 0).
○ Data Types: Ensuring you don't try to put text into a number column.
○ Triggers: Database logic that automatically runs on inserts, updates, or deletes.
If a transaction would violate any of these rules, it cannot be committed, and it will be rolled back.
Why it's important: Maintains the integrity of your data and enforces the structure and business rules defined within the database schema.
SQL Server: SQL Server automatically checks for constraint violations and executes triggers as part of the transaction. If a violation occurs, the transaction fails.
Example (Bank Transfer):
○ Let's say you have a CHECK constraint on the Balance column of your Accounts table that says Balance >= 0.
○ You try to transfer $200 from Account A, which only has $150.
○ You start the transaction (BEGIN TRAN).
○ You deduct 200 from Account - A. Account A′s balance temporarily becomes −50 within the transaction's scope.
○ When you try to COMMIT TRAN, SQL Server checks all constraints. It sees that Account A's balance is negative, which violates the CHECK constraint.
○ Because of Consistency (and the constraint), the COMMIT fails, and the transaction is automatically rolled back. Account A's balance returns to $150. The database remains in a valid state where no account has a negative balance.


Isolation

Think of it: "Transactions Don't Step on Each Other's Toes."
What it means: Multiple transactions running at the same time should not interfere with each other. From the perspective of one transaction, it should appear as if it is the only transaction running on the database. This prevents various concurrency problems (like one transaction reading data that another transaction is changing but hasn't committed yet, or two transactions trying to update the same data simultaneously in a conflicting way).
Why it's important: Allows multiple users or applications to access and modify the database concurrently without causing errors or returning incorrect results.
SQL Server: SQL Server provides different Isolation Levels (like READ COMMITTED, SNAPSHOT, etc.) which control how much isolation you get. Higher isolation levels provide stronger guarantees but can sometimes impact performance because SQL Server might need to use more locks or versioning to keep transactions separate. The default level in SQL Server is READ COMMITTED, which prevents reading data that another transaction has modified but not yet committed (known as "Dirty Reads").
Example (Bank Transfer):
○ Transaction 1 starts to transfer $100 from Account A to Account B. (BEGIN TRAN, deducts from A, prepares to add to B).
○ At the exact same time, Transaction 2 starts to read the balances of Account A and Account B to generate a report.
○ Because of Isolation, Transaction 2 will typically not see the temporary state where Account A has been debited but Account B hasn't been credited yet (especially with the default READ COMMITTED isolation level). Transaction 2 will likely see the balances as they were before Transaction 1 started, or it might wait until Transaction 1 is fully committed before reading. This ensures Transaction 2 gets a consistent view of the data, even though it's running concurrently with a transaction that's modifying that data.


Durability

Think of it: "Changes are Permanent, Even After a Crash."
What it means: Once a transaction has been successfully committed, its changes are permanent and will survive even if the database server crashes, loses power, or restarts immediately after the commit. The committed data is stored in a way that guarantees it won't be lost.
Why it's important: Ensures that once a user or application gets confirmation that a transaction is complete (e.g., "Your transfer is successful"), they can trust that the changes have truly been saved and won't disappear.
SQL Server: When you COMMIT TRAN, SQL Server ensures that the record of the transaction's changes is written to the transaction log on disk. Writing to the log is typically faster than writing the actual data pages to disk. Even if the server crashes after the log record is safely written but before the changes are written to the main data files, SQL Server can use the transaction log during startup recovery to redo the committed transaction and ensure the data files reflect the committed state.
Example (Bank Transfer):
○ You successfully complete the transfer: $100 is deducted from Account A, and $100 is added to Account B.
○ You issue COMMIT TRAN. SQL Server confirms the commit back to your application.
○ Immediately after receiving the confirmation, the power goes out, and the server shuts down.
○ Because of Durability, when the server is restarted, SQL Server performs a recovery process. It reads the transaction log, sees that your transfer transaction was committed, and ensures that the changes (deducting from A and adding to B) are fully applied to the actual data files on disk. When you next check the balances, they will correctly reflect the transfer.


Next


ACID properties are the backbone of reliable database systems like SQL Server

Out of all 4 of these properties, Isolation is the property we can configure mostly on SQL Server.

Atomicity and Durability is automatically implemented in the SQL server core code to make sure all transactions are adhere to them (otherwise no proper transaction). Although we define rules (business rules via constraints and triggers) for Consistency, enforcing them (once defined and enabled) is automatic (no intervention required from us).

However, Isolation property is much more configurable. This is because there is a trade-off between high Isolation and performances. Therefore, SQL server allows user to choose several pre-defined Isolation levels based on performance requirements.

So let's explore more on Isolation levels in our next module.


13/05/2025

Issue Opening OneDrive Vault - Solution

Recently I have an issue where my OneDrive "Vault" is not opening.

This is the error I was getting:


It says "We couldn't unlock your Personal Vault", "We encountered an unexpected error and could not unlock your Personal Vault. Please try again. You can also access you Personal Vault on OneDrive.com".

As per error message, I couldn't open (Unlock) my OneDrive Vault on desktop (windows desktop app). Rest of the OneDrive was working fine, I could sync rest of the folder through desktop app without any issue. Online access also fine, and vault was opening on Online app. So it is clear, something was not ok on desktop app.

Unfortunately there were no error log on Event viewer or any other usual places.

Googled for solution, but couldn't find anything directly relate to this issue. Most issues were relate to OneDrive desktop app not being able to sync.

There fore, I have tried few things mentioned on those articles.


Unlink and Re-Link

First thing I tried was "Unlink" account from desktop app and re-add it. You can do this in Settings of the app in Account section (see below). However, note this will re-sync all your files. So if you have lot of files in your OneDrive prepare for long sync time.


Un-install and Re-Install

Second option was to un-install the OneDrive desktop app and re-install it.

You can un-install the OneDrive desktop app using the usual way you un-install any other app in windows.

Go to Settings > Apps > Installed Apps and un-install the app. Once completely un-installed, re-start the machine and install it by downloading the setup file (here is a link to OneDrive releases).


Un-block Network

Then you got to try un-block your network connection, in case there are something blocking the authentication to "Vault". For example, try disabling firewall and test (make sure to re-enable it again if it didn't work, if it work you will have to find which port/application getting blocked by the firewall).

Another advice is to try disabling anti-virus software and test. Again make sure to re-enable it again.

Make sure proxies or vpn you connected are not blocking the OneDrive.

For me none of these worked. So my final option was to contact Microsoft Support (I'm a paid Office 365 customer, so I'm able to get support from them).

First few letters from Microsoft support was not very useful as I have already done basic things which I covered above.


Clear Credential Cache

Next what Microsoft has suggested is following:

  1. Press the Windows key  + R to open a "Run" dialog.
  2. Enter the path %localappdata%\Microsoft\OneDrive\settings and select OK.
  3. Delete the PreSignInSettingsConfig.json file.
  4. Restart the machine
They said this would clear the credential cache.
But for me it didn't really work.


Bypass the Issue

Ultimately they have acknowledge that the problem I'm facing is part of ongoing issue, which their Engineering team is trying to resolve.

Until fix is implemented they have suggested below steps:

  1. Exit the OneDrive desktop app.
  2. Open command line via start menu and run the command- REG DELETE HKCU\Software\Microsoft\OneDrive\Accounts\Personal /v VaultBackupGuid
    1. Its Ok if it returns Error: The system was unable to find the specified registry key or value.
  3. Open credentials manager via start menu.
  4. Select the Windows credential tab.
  5. Find the credential named “Microsoft OneDrive Generic Data- Personal vault VHD info” and click Remove.
  6. Restart OneDrive and then try to unlock the vault. 

This has actually fixed my issue.

Hope this will help someone going through the same issue.





01/05/2025

DBeaver Series - Part 2 - Connecting to Database

In my last blog post on DBeaver series - Part1, I was confused why my custom database was not shown on the DBeaver, even though I have connected to the localhost server.

After trying few things, I realized that connections in DBeaver is per database.

So here is how you create a connection to new database.

In DBeaver, in the tool bar, click on the new connection icon (or you can use File > New menu items to do the same).


You will be presented with new connection dialog box:


Select the type of the database you want to connect. I have selected PostgreSQL. Then click next.

Connection settings dialog box opens:


Note that this is a very complex connection setting dialog box with hundreds of configuration settings. However you only require to configure very few things for default connections.

Settings dialog has for 5 tabs.

  • Main
  • PostgreSQL
  • Driver Settings
  • SSH
  • SSL
We only consider "Main" tab in this blog, in future date we will discover what other settings are.

In "Main" tab, first thing you need to specify is "Server", i.e. which server you are going to connect to. You can do this via two way:
  • Host - specify host name and port as separate settings
  • Url - specify the connection as Url
Interestingly there is "Show All Databases" tick box in there, we'll tick this and see what will happen.

As host, you need to specify host name, database name and port number.

In Authentication section, we select Database Native mode. Other options are not considered in this blog post scope.

As authentication parameters, you need specify user name and password. You have the option to save the password.

Once you done with all you settings, click on the "Finish" button.

If all settings are correct you will be connected to the database:


Note that since we ticked "Show All Databases" box, connection shows all databases, but you can only connect to the database you specified.

How to find usage information on Github Copilot

Most of you already know, Github copilot is very nice addition to VS code and Visual Studio IDE. Past couple of months, it has been very goo...