Tuesday, March 12, 2024

What is deadlock priority and how to address in SQL Server

In SQL Server, deadlock priority is a mechanism that allows you to influence the selection of the transaction that will be chosen as the deadlock victim when a deadlock occurs. You can use deadlock priority to specify the importance of individual transactions in the event of a deadlock.

To address deadlock priority in SQL Server, you can consider the following:

  1. Setting deadlock priority:
    • You can use the SET DEADLOCK_PRIORITY statement to specify the priority of a session or transaction.
    • The priority levels range from -10 to 10, where -10 is the lowest priority and 10 is the highest.
    • By setting the deadlock priority, you can influence the selection of the victim transaction when a deadlock occurs.
  2. Here's an example of how to set the deadlock priority for a session:

    SET DEADLOCK_PRIORITY LOW; -- Set the deadlock priority to low
      
  3. Adjusting transaction logic:

    • Design your transaction logic to handle the potential impact of being chosen as the deadlock victim based on the assigned deadlock priority.
    • Consider implementing retry logic for transactions with lower deadlock priority after being chosen as the deadlock victim.
  4. Analyzing and tuning deadlock priority:

    • Evaluate the impact of deadlock priority settings on your application's transactions and overall performance.
    • Tune the deadlock priority based on the specific requirements and characteristics of your application to effectively manage deadlocks.

It's important to carefully consider the implications of deadlock priority settings in SQL Server and design your transaction logic to handle deadlock situations appropriately. Understanding the behavior of deadlock priority in SQL Server is crucial for effectively addressing and managing deadlocks.

Monday, March 11, 2024

Understanding In-Memory Caching in .NET Core with IMemoryCache Interface

1. In-Memory Caching in .NET Core:
- In-Memory Caching is used to provide faster response to incoming requests by retrieving data from cache rather than the original source.
- Data Caching allows retrieval of data from cache as long as it doesn't expire.

2. Terms Related to Caching:
- Cache Hit refers to the requested data being in the cache, while Cache Miss refers to the data not being in the cache.
- In the case of Cache Miss, data is fetched from the data source and written back into the cache.

3. In-Memory Cache in .NET Core:
- In .NET Core, data can be written to cache, read, or deleted using the IMemoryCache interface in the Microsoft.Extensions.Caching.Memory library.
- Various options such as AbsoluteExpiration, ExpirationTokens, Priority, Size, and SlidingExpiration can be used to manage the cache.

4. Usage in a Project:
- Memory Cache is enabled in the ConfigureServices method in the startup.cs class by adding services.AddMemoryCache().
- The IMemoryCache interface is injected in the related controller to use 'In-Memory Cache'.

Convert String to Title case using Javascript

Here is the function to convert string to title case, which can handle spaces and underscores. Below function will remove underscores from the string.

// Import the function
function convertToTitleCase(input) {
  return input.toLowerCase().replace(/_/g, ' ').replace(/\b\w/g, function(match) {
    return match.toUpperCase();
  });
}

You can call the convertToTitleCase function in HTML by including a script tag with the function definition, and then using JavaScript to call the function and display the result.

Here's an example of how you can call the convertToTitleCase function in HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Convert to Title Case</title>
</head>
<body>

<p id="output"></p>

<script>
// Function definition
function convertToTitleCase(input) {
  return input.toLowerCase().replace(/_/g, ' ').replace(/\b\w/g, function(match) {
    return match.toUpperCase();
  });
}

  // Call the function and display the result
  let input = "Nagasai_Srinivas_Mudara";
  let convertedString = convertToTitleCase(input);
  document.getElementById("output").innerHTML = convertedString;
</script>

</body>
</html>

In this JavaScript function, the replace method is used with a regular expression to match the underscores and lowercase letters and convert the lowercase letters to uppercase when preceded by an underscore or at the beginning of the string.

You can use the convertToTitleCase function to convert any input string to title case in a generic and reusable way.

Wednesday, March 06, 2024

How to implement retry logic for DB Transactions

In SQL Server, you can implement retry logic for transactions using T-SQL and error handling. Here's an example of how you can create a stored procedure that includes retry logic for handling deadlock errors:

CREATE PROCEDURE usp_RetryTransaction
AS
BEGIN
    DECLARE @retryCount INT = 0
    DECLARE @maxRetries INT = 3

    WHILE @retryCount < @maxRetries
    BEGIN
        BEGIN TRY
            BEGIN TRANSACTION
            -- Your transactional logic goes here
            COMMIT TRANSACTION
            RETURN
        END TRY
        BEGIN CATCH
            IF ERROR_NUMBER() = 1205  -- Deadlock error number
            BEGIN
                ROLLBACK TRANSACTION
                SET @retryCount = @retryCount + 1
                WAITFOR DELAY '00:00:01'  -- Wait for 1 second before retrying
            END
            ELSE
            BEGIN
                -- Handle other types of errors
                THROW
            END
        END CATCH
    END
    -- If the maximum number of retries is reached, handle the situation as needed
    -- For example, raise an error or log the issue
END
  

In this example, the stored procedure attempts the transaction logic within a retry loop, and if a deadlock error (error number 1205) occurs, it rolls back the transaction, increments the retry count, and waits for a short duration before retrying the transaction. If the maximum number of retries is reached, you can handle the situation as needed based on your application's requirements.

You can then call this stored procedure whenever you need to perform a transaction with retry logic for deadlock handling.

Tuesday, March 05, 2024

How to check if string exists in JQuery

In jQuery, you can use the indexOf method to check if a string contains another string. Here's an example:

var mainString = "Hello, world";
var subString = "world";

if (mainString.indexOf(subString) !== -1) {
    // subString is found in mainString
    console.log("Substring found");
} else {
    // subString is not found in mainString
    console.log("Substring not found");
}
  

In this example, the indexOf method returns the index of the first occurrence of the subString within the mainString. If the subString is not found, indexOf returns -1. You can use this to check if a string contains another string in jQuery.