Sunday, 7 April 2024

SQL Deadlock

Types of Deadlock

exclusive lock(X), shared lock(S), update lock (U), intent lock (I), schema lock (SCH) and bulk update lock (BU)

select * from sys.dm_tran_locks

 The first time a user sees the following message, the result of an unhandled deadlock error in SQL Server, it can come as quite a shock.

Msg 1205, Level 13, State 56, Line 10

Transaction (Process ID 62) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

Two or more sessions were waiting to acquire a lock on a resource already locked by another session in the same locking chain. The result is a 'circular chain' where no session can complete, and so SQL Server is forced to intervene to clear up the mess. It automatically chooses one of the sessions as the 'victim', kills it and rolls back its transaction. It means that the other sessions may proceed, but that is little comfort to the hapless victim, and if that victim happens to be a session running a critical business process, then it may soon be of little comfort to the DBA.

Deadlock monitoring Two Type of methods

Deadlock Fixing

Redux in react

 React Redux 8.x requires React 16.8.3 or later / React Native 0.59 or later, in order to make use of React Hooks.

React Redux includes a <Provider /> component, which makes the Redux store available to the rest of your app:

import React from 'react'

import ReactDOM from 'react-dom/client'

import { Provider } from 'react-redux'

import store from './store'

import App from './App'

// As of React 18

const root = ReactDOM.createRoot(document.getElementById('root'))

root.render(

  <Provider store={store}>

    <App />

  </Provider>,

)


useSelector reads a value from the store state and subscribes to updates, while useDispatch returns the store's dispatch method to let you dispatch actions.

import React from 'react'

import { useSelector, useDispatch } from 'react-redux'

import {

  decrement,

  increment,

  incrementByAmount,

  incrementAsync,

  selectCount,

} from './counterSlice'

import styles from './Counter.module.css'


export function Counter() {

  const count = useSelector(selectCount)

  const dispatch = useDispatch()


  return (

    <div>

      <div className={styles.row}>

        <button

          className={styles.button}

          aria-label="Increment value"

          onClick={() => dispatch(increment())}

        >

          +

        </button>

        <span className={styles.value}>{count}</span>

        <button

          className={styles.button}

          aria-label="Decrement value"

          onClick={() => dispatch(decrement())}

        >

          -

        </button>

      </div>

      {/* omit additional rendering output here */}

    </div>

  )

}

Redux Toolkit 

Error boundary React

 


A class component becomes an error boundary if it defines either (or both) of the lifecycle methods static getDerivedStateFromError() or componentDidCatch(). 

Use static getDerivedStateFromError() to render a fallback UI after an error has been thrown. Use componentDidCatch() to log error information.


example:


class ErrorBoundary extends React.Component {

  constructor(props) {

    super(props);

    this.state = { hasError: false };

  }


  static getDerivedStateFromError(error) {

    // Update state so the next render will show the fallback UI.

    return { hasError: true };

  }


  componentDidCatch(error, errorInfo) {

    // You can also log the error to an error reporting service

    logErrorToMyService(error, errorInfo);

  }


  render() {

    if (this.state.hasError) {

      // You can render any custom fallback UI

      return <h1>Something went wrong.</h1>;

    }


    return this.props.children; 

  }

}


<ErrorBoundary>

  <MyWidget />

</ErrorBoundary>

React Questions and awnsers

 Type of Components

Class(Statefull Components) - Error Handling mange session(Error Boundary), Life Cycle

Functional - Module Way -I will be little faster when use function (React hook , After react 16 Version)


Life Cycle

Mounting,Updating and Unmounting

Mounting

    constructor()

    getDerivedStateFromProps()

    render()

    componentDidMount()

Updating

    getDerivedStateFromProps()

    shouldComponentUpdate()

    render()

    getSnapshotBeforeUpdate()

    componentDidUpdate()

Unmounting

    componentWillUnmount()

React Hook

UseState,UseEffect,UseContext,UseRef,UseReducer,UseCallBack,UseMemo,Custom Hooks

UseState

useState accepts an initial state and returns two values:

The current state.

A function that updates the state.

UseEffect

useEffect runs on every render. That means that when the count changes, a render happens, which then triggers another effect.

API Using in React

Axios - Json by default

Axios has the ability to inercept HTTP Request

Axioshas built-in support for download progress

Fetch

Fetch by default ,Do not provide a way to intercept request

Fetch does not support uload progress

How to use transfer the one comp to another

Parent to child - using props

child to parent - using call back function

Monday, 12 February 2024

Lifetimes Dependency Injection

 There are 3 types of lifetimes supported by ASP.NET Core for the dependency injection,

1.Transient Service

2.Scoped Service

3.Singleton Service

Let’s have a look in depth at how it works


transient -each action one object will be create 

not the data will be passed

external utiltiy ofn previous t

scoped - keep the session through the request

registion reportiry 


shopping cart- scope

order it send the mail - transient (not need any data)

Sunday, 15 May 2022

React JS Comments

 Creating a new application

Create React App provides multiple ways to create React application.

Using npx script.

npx create-react-app <react-app-name>

npx create-react-app hello-react-app

Using npm package manager.

npm init react-app <react-app-name>

npm init react-app hello-react-app

Using yarn package manager.

yarn init react-app <react-app-name>

yarn init react-app hello-react-app

Selecting a template

Create React App creates React application using default template. Template refers the initial code with certain build-in functionality. There are hundreds of template with many advanced features are available in npm package server. Create React App allows the users to select the template through –template command line switch.

create-react-app my-app --template typescript

Installing a dependency

Using npm package manager.

npm install --save react-router-dom

Using yarn package manager.

yarn add react-router-dom


Running the application

React application can be started using npm or yarn command depending on the package manager used in the project.

Using npm package manager.

npm start

Using yarn package manager.

yarn start

To run the application in secure mode (HTTPS), set an environment variable, HTTPS and set it to true before starting the application. For example, in windows command prompt (cmd.exe), the below command set HTTPS and starts the application is HTTPS mode.

cmd

set HTTPS=true && npm start


npm start and  npm run are same further check with below link

https://docs.npmjs.com/cli/v8/commands/npm-run-script

Building

A single command is enough to create a production build of the application.

npm run build

> expense-manager@0.1.0 build path\to\expense-manager

> react-scripts build

Creating an optimized production build...

Compiled with warnings.

File sizes after gzip:

   41.69 KB   build\static\js\2.a164da11.chunk.js

    2.24 KB   build\static\js\main.de70a883.chunk.js

    1.4  KB   build\static\js\3.d8a9fc85.chunk.js

    1.17 KB   build\static\js\runtime-main.560bee6e.js

  493     B   build\static\css\main.e75e7bbe.chunk.css


The project was built assuming it is hosted at /.

You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.

You may serve it with a static server:

   npm install -g serve

   serve -s build


Real DOM and Virtual DOM

 What is DOM?

“The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.”

The DOM is an abstraction of a page’s HTML structure. It takes HTML elements and wraps them in an object with a tree-structure — maintaining the parent/child relationships of those nested HTML elements. This provides an API that allows us to traverse nodes (HTML elements) and manipulate them in a number of ways — such as adding nodes, removing nodes, editing a node’s content, etc.


Real DOMVirtual DOM
DOM is a language-neutral interface allowing programs and scripts to dynamically access and update multiple objects like content, structure, and style of a document.Is a collection of modules designed to provide a declarative way to represent the DOM for an application.
The DOM represents the document as nodes and objects.A virtual DOM object is a representation of a DOM object, like a lightweight copy.
It is an object-oriented representation of a web page, modified with a scripting language like JavaScript.Virtual DOM is ideal for mobile-first applications.





How does updates work in React?

On the first load, ReactDOM.render() will create the Virtual DOM tree and real DOM tree.

As React works on Observable patterns, when any event(like key press, left click, api response, etc.) occurred, Virtual DOM tree nodes are notified for props change, If the properties used in that node are updated, the node is updated else left as it is.

React compares Virtual DOM with real DOM and updates real DOM. This process is called Reconciliation. React uses Diffing algorithm techniques of Reconciliation.

Updated real DOM is repainted on browser