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