Transactions and Locking

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rami

    #1

    Transactions and Locking

    I have some requirement for an automated payment system. The system has
    four machines setup as follows:
    1- Two machines have a clustered database.
    2- Two machines have a .net business logic component that will handle
    payment transactions with certain external component (Payment Gateway)


    My query is regarding transaction handling inside the business logic
    component. Because this component is running on two machines and these
    two instances of the component are accessing the same transactions
    table. I need these two components to work together without blocking.
    So transaction isolation level "Serialized " will not work with this
    model.

    I am not clear about how to lock the specific rows that are being
    handled by certain instance. If I set a flag (column) to lock the row,
    then that instance may fail before resetting the flag then this row
    will never be handled. In such a scenario I would expect the other
    instance to take over and handle whatever was locked previously by the
    other instance.

    Is there any ideas regarding arrangement between such components which
    access the same table?

    Thanks a lot for the help,
    Rami AlHasan

  • Uri Dimant

    #2
    Re: Transactions and Locking

    Rami
    What do the queries do? Do the transactions UPDATE/INSEERT/DELETE operation?
    How big are the tables? Do you have indexes defined on the tables?



    "Rami" <ramialhasan@gm ail.comwrote in message
    news:1168859468 .535588.297560@ 51g2000cwl.goog legroups.com...
    >I have some requirement for an automated payment system. The system has
    four machines setup as follows:
    1- Two machines have a clustered database.
    2- Two machines have a .net business logic component that will handle
    payment transactions with certain external component (Payment Gateway)
    >
    >
    My query is regarding transaction handling inside the business logic
    component. Because this component is running on two machines and these
    two instances of the component are accessing the same transactions
    table. I need these two components to work together without blocking.
    So transaction isolation level "Serialized " will not work with this
    model.
    >
    I am not clear about how to lock the specific rows that are being
    handled by certain instance. If I set a flag (column) to lock the row,
    then that instance may fail before resetting the flag then this row
    will never be handled. In such a scenario I would expect the other
    instance to take over and handle whatever was locked previously by the
    other instance.
    >
    Is there any ideas regarding arrangement between such components which
    access the same table?
    >
    Thanks a lot for the help,
    Rami AlHasan
    >

    Comment

    • dmarkle

      #3
      Re: Transactions and Locking

      Rami:

      I think you have a conceptual error in your design that you need to
      examine. You said that you want the components to work together
      without blocking, yet you say that you want to lock a row. When you
      "lock" a row, you essentially are blocking another process from
      operating on that row (it depends on the type of lock).

      -D

      Rami wrote:
      I have some requirement for an automated payment system. The system has
      four machines setup as follows:
      1- Two machines have a clustered database.
      2- Two machines have a .net business logic component that will handle
      payment transactions with certain external component (Payment Gateway)
      >
      >
      My query is regarding transaction handling inside the business logic
      component. Because this component is running on two machines and these
      two instances of the component are accessing the same transactions
      table. I need these two components to work together without blocking.
      So transaction isolation level "Serialized " will not work with this
      model.
      >
      I am not clear about how to lock the specific rows that are being
      handled by certain instance. If I set a flag (column) to lock the row,
      then that instance may fail before resetting the flag then this row
      will never be handled. In such a scenario I would expect the other
      instance to take over and handle whatever was locked previously by the
      other instance.
      >
      Is there any ideas regarding arrangement between such components which
      access the same table?
      >
      Thanks a lot for the help,
      Rami AlHasan

      Comment

      • Rami

        #4
        Re: Transactions and Locking

        Thanks Uri,

        Yes transactions do all opertaions like UPDATE/INSEERT/DELETE. Tables
        will be growing fast becuase these are payment transactions. Indexes
        are defined on the columns used too much in WHERE clauses.



        Uri Dimant wrote:
        Rami
        What do the queries do? Do the transactions UPDATE/INSEERT/DELETE operation?
        How big are the tables? Do you have indexes defined on the tables?
        >
        >
        >
        "Rami" <ramialhasan@gm ail.comwrote in message
        news:1168859468 .535588.297560@ 51g2000cwl.goog legroups.com...
        I have some requirement for an automated payment system. The system has
        four machines setup as follows:
        1- Two machines have a clustered database.
        2- Two machines have a .net business logic component that will handle
        payment transactions with certain external component (Payment Gateway)


        My query is regarding transaction handling inside the business logic
        component. Because this component is running on two machines and these
        two instances of the component are accessing the same transactions
        table. I need these two components to work together without blocking.
        So transaction isolation level "Serialized " will not work with this
        model.

        I am not clear about how to lock the specific rows that are being
        handled by certain instance. If I set a flag (column) to lock the row,
        then that instance may fail before resetting the flag then this row
        will never be handled. In such a scenario I would expect the other
        instance to take over and handle whatever was locked previously by the
        other instance.

        Is there any ideas regarding arrangement between such components which
        access the same table?

        Thanks a lot for the help,
        Rami AlHasan

        Comment

        • Rami

          #5
          Re: Transactions and Locking

          Thanks dmarkle,

          Perhaps I didn't describe it very well. I meant that I need the two
          instances of the components to handle part of the payment
          transactions(ro ws), and the other instance handles the other part. This
          is what I meant by no one should block the other.

          dmarkle wrote:
          Rami:
          >
          I think you have a conceptual error in your design that you need to
          examine. You said that you want the components to work together
          without blocking, yet you say that you want to lock a row. When you
          "lock" a row, you essentially are blocking another process from
          operating on that row (it depends on the type of lock).
          >
          -D
          >
          Rami wrote:
          I have some requirement for an automated payment system. The system has
          four machines setup as follows:
          1- Two machines have a clustered database.
          2- Two machines have a .net business logic component that will handle
          payment transactions with certain external component (Payment Gateway)


          My query is regarding transaction handling inside the business logic
          component. Because this component is running on two machines and these
          two instances of the component are accessing the same transactions
          table. I need these two components to work together without blocking.
          So transaction isolation level "Serialized " will not work with this
          model.

          I am not clear about how to lock the specific rows that are being
          handled by certain instance. If I set a flag (column) to lock the row,
          then that instance may fail before resetting the flag then this row
          will never be handled. In such a scenario I would expect the other
          instance to take over and handle whatever was locked previously by the
          other instance.

          Is there any ideas regarding arrangement between such components which
          access the same table?

          Thanks a lot for the help,
          Rami AlHasan

          Comment

          • Uri Dimant

            #6
            Re: Transactions and Locking

            Rami
            You have to wrap the transactions with BEGIN TRAN ...COMMIT TRAN make
            sure that if you get a value an later on update it , use lockin hints as
            the below example
            DECLARE @ord INT
            BEGIN TRAN

            SELECT @ord=MAX(OrderI d) FROM Order WITH (UPDLOCK,HOLDLO CK)
            UPDATE Table SET orderid =@ord WHERE.........

            COMMIT TRAN

            "Rami" <ramialhasan@gm ail.comwrote in message
            news:1168917890 .263676.286700@ a75g2000cwd.goo glegroups.com.. .
            Thanks Uri,
            >
            Yes transactions do all opertaions like UPDATE/INSEERT/DELETE. Tables
            will be growing fast becuase these are payment transactions. Indexes
            are defined on the columns used too much in WHERE clauses.
            >
            >
            >
            Uri Dimant wrote:
            >Rami
            >What do the queries do? Do the transactions UPDATE/INSEERT/DELETE
            >operation?
            >How big are the tables? Do you have indexes defined on the tables?
            >>
            >>
            >>
            >"Rami" <ramialhasan@gm ail.comwrote in message
            >news:116885946 8.535588.297560 @51g2000cwl.goo glegroups.com.. .
            >I have some requirement for an automated payment system. The system has
            four machines setup as follows:
            1- Two machines have a clustered database.
            2- Two machines have a .net business logic component that will handle
            payment transactions with certain external component (Payment Gateway)
            >
            >
            My query is regarding transaction handling inside the business logic
            component. Because this component is running on two machines and these
            two instances of the component are accessing the same transactions
            table. I need these two components to work together without blocking.
            So transaction isolation level "Serialized " will not work with this
            model.
            >
            I am not clear about how to lock the specific rows that are being
            handled by certain instance. If I set a flag (column) to lock the row,
            then that instance may fail before resetting the flag then this row
            will never be handled. In such a scenario I would expect the other
            instance to take over and handle whatever was locked previously by the
            other instance.
            >
            Is there any ideas regarding arrangement between such components which
            access the same table?
            >
            Thanks a lot for the help,
            Rami AlHasan
            >
            >

            Comment

            • Rami

              #7
              Re: Transactions and Locking

              Thanks again,

              This seems to be a very interesting idea... But I want to clarify, this
              means that one instance of the component will execute this select
              statement and take the lock. Then the other instance may execute the
              same statement again but get a failure because the rows are already
              locked. So the second instance will be blocked from handling
              transactions. Is this correct?

              In my case I want each instance to select 100 rows for example and
              process them completely before releasing them. But at the same time, I
              want the other instance of the component to lock another 100 rows and
              work on them exclusively.

              Any help on this? and thanks a lot for your valuable answer.
              Rami



              Uri Dimant wrote:
              Rami
              You have to wrap the transactions with BEGIN TRAN ...COMMIT TRAN make
              sure that if you get a value an later on update it , use lockin hints as
              the below example
              DECLARE @ord INT
              BEGIN TRAN
              >
              SELECT @ord=MAX(OrderI d) FROM Order WITH (UPDLOCK,HOLDLO CK)
              UPDATE Table SET orderid =@ord WHERE.........
              >
              COMMIT TRAN
              >
              "Rami" <ramialhasan@gm ail.comwrote in message
              news:1168917890 .263676.286700@ a75g2000cwd.goo glegroups.com.. .
              Thanks Uri,

              Yes transactions do all opertaions like UPDATE/INSEERT/DELETE. Tables
              will be growing fast becuase these are payment transactions. Indexes
              are defined on the columns used too much in WHERE clauses.



              Uri Dimant wrote:
              Rami
              What do the queries do? Do the transactions UPDATE/INSEERT/DELETE
              operation?
              How big are the tables? Do you have indexes defined on the tables?
              >
              >
              >
              "Rami" <ramialhasan@gm ail.comwrote in message
              news:1168859468 .535588.297560@ 51g2000cwl.goog legroups.com...
              I have some requirement for an automated payment system. The system has
              four machines setup as follows:
              1- Two machines have a clustered database.
              2- Two machines have a .net business logic component that will handle
              payment transactions with certain external component (Payment Gateway)


              My query is regarding transaction handling inside the business logic
              component. Because this component is running on two machines and these
              two instances of the component are accessing the same transactions
              table. I need these two components to work together without blocking.
              So transaction isolation level "Serialized " will not work with this
              model.

              I am not clear about how to lock the specific rows that are being
              handled by certain instance. If I set a flag (column) to lock the row,
              then that instance may fail before resetting the flag then this row
              will never be handled. In such a scenario I would expect the other
              instance to take over and handle whatever was locked previously by the
              other instance.

              Is there any ideas regarding arrangement between such components which
              access the same table?

              Thanks a lot for the help,
              Rami AlHasan

              Comment

              • Uri Dimant

                #8
                Re: Transactions and Locking

                Rami
                This seems to be a very interesting idea... But I want to clarify, this
                means that one instance of the component will execute this select
                statement and take the lock. Then the other instance may execute the
                same statement again but get a failure because the rows are already
                locked. So the second instance will be blocked from handling
                transactions. Is this correct?
                It does not block readers , it does block writers.
                In my case I want each instance to select 100 rows for example and
                process them completely before releasing them. But at the same time, I
                want the other instance of the component to lock another 100 rows and
                work on them exclusively.
                Read about setting transaction isolation level in the BOL








                "Rami" <ramialhasan@gm ail.comwrote in message
                news:1168937506 .272849.4080@51 g2000cwl.google groups.com...
                Thanks again,
                >
                This seems to be a very interesting idea... But I want to clarify, this
                means that one instance of the component will execute this select
                statement and take the lock. Then the other instance may execute the
                same statement again but get a failure because the rows are already
                locked. So the second instance will be blocked from handling
                transactions. Is this correct?
                >
                In my case I want each instance to select 100 rows for example and
                process them completely before releasing them. But at the same time, I
                want the other instance of the component to lock another 100 rows and
                work on them exclusively.
                >
                Any help on this? and thanks a lot for your valuable answer.
                Rami
                >
                >
                >
                Uri Dimant wrote:
                >Rami
                >You have to wrap the transactions with BEGIN TRAN ...COMMIT TRAN make
                >sure that if you get a value an later on update it , use lockin hints as
                >the below example
                >DECLARE @ord INT
                >BEGIN TRAN
                >>
                >SELECT @ord=MAX(OrderI d) FROM Order WITH (UPDLOCK,HOLDLO CK)
                >UPDATE Table SET orderid =@ord WHERE.........
                >>
                >COMMIT TRAN
                >>
                >"Rami" <ramialhasan@gm ail.comwrote in message
                >news:116891789 0.263676.286700 @a75g2000cwd.go oglegroups.com. ..
                Thanks Uri,
                >
                Yes transactions do all opertaions like UPDATE/INSEERT/DELETE. Tables
                will be growing fast becuase these are payment transactions. Indexes
                are defined on the columns used too much in WHERE clauses.
                >
                >
                >
                Uri Dimant wrote:
                >Rami
                >What do the queries do? Do the transactions UPDATE/INSEERT/DELETE
                >operation?
                >How big are the tables? Do you have indexes defined on the tables?
                >>
                >>
                >>
                >"Rami" <ramialhasan@gm ail.comwrote in message
                >news:116885946 8.535588.297560 @51g2000cwl.goo glegroups.com.. .
                >I have some requirement for an automated payment system. The system
                >has
                four machines setup as follows:
                1- Two machines have a clustered database.
                2- Two machines have a .net business logic component that will
                handle
                payment transactions with certain external component (Payment
                Gateway)
                >
                >
                My query is regarding transaction handling inside the business logic
                component. Because this component is running on two machines and
                these
                two instances of the component are accessing the same transactions
                table. I need these two components to work together without
                blocking.
                So transaction isolation level "Serialized " will not work with this
                model.
                >
                I am not clear about how to lock the specific rows that are being
                handled by certain instance. If I set a flag (column) to lock the
                row,
                then that instance may fail before resetting the flag then this row
                will never be handled. In such a scenario I would expect the other
                instance to take over and handle whatever was locked previously by
                the
                other instance.
                >
                Is there any ideas regarding arrangement between such components
                which
                access the same table?
                >
                Thanks a lot for the help,
                Rami AlHasan
                >
                >
                >

                Comment

                • Rami

                  #9
                  Re: Transactions and Locking

                  One last question:

                  I updated your sample query in the following form:

                  DECLARE @ord INT
                  SET TRANSACTION ISOLATION LEVEL READ COMMITTED
                  BEGIN TRAN
                  SELECT @ord=MAX(ORDER_ ID) FROM Order WITH (UPDLOCK, READPAST)
                  UPDATE Order SET ORDER_STATUS= something WHERE (ORDER_ID = @ord)
                  COMMIT TRAN

                  The basic change here is that I removed the HOLDLOCK and put the
                  READPAST hint instead. I think the READPAST hint, will help in
                  filtering out those locked transactions. so each instance will see
                  unlocked transactiosns. But I removed HOLDLOCK because there was an
                  error generated if I included it with READPAST.

                  For me this seems to be working, is there any problem with this or any
                  hidden implications?

                  Thanks,
                  Rami



                  Uri Dimant wrote:
                  Rami
                  This seems to be a very interesting idea... But I want to clarify, this
                  means that one instance of the component will execute this select
                  statement and take the lock. Then the other instance may execute the
                  same statement again but get a failure because the rows are already
                  locked. So the second instance will be blocked from handling
                  transactions. Is this correct?
                  >
                  It does not block readers , it does block writers.
                  >
                  In my case I want each instance to select 100 rows for example and
                  process them completely before releasing them. But at the same time, I
                  want the other instance of the component to lock another 100 rows and
                  work on them exclusively.
                  >
                  Read about setting transaction isolation level in the BOL
                  >
                  >
                  >
                  >
                  >
                  >
                  >
                  >
                  "Rami" <ramialhasan@gm ail.comwrote in message
                  news:1168937506 .272849.4080@51 g2000cwl.google groups.com...
                  Thanks again,

                  This seems to be a very interesting idea... But I want to clarify, this
                  means that one instance of the component will execute this select
                  statement and take the lock. Then the other instance may execute the
                  same statement again but get a failure because the rows are already
                  locked. So the second instance will be blocked from handling
                  transactions. Is this correct?

                  In my case I want each instance to select 100 rows for example and
                  process them completely before releasing them. But at the same time, I
                  want the other instance of the component to lock another 100 rows and
                  work on them exclusively.

                  Any help on this? and thanks a lot for your valuable answer.
                  Rami



                  Uri Dimant wrote:
                  Rami
                  You have to wrap the transactions with BEGIN TRAN ...COMMIT TRAN make
                  sure that if you get a value an later on update it , use lockin hints as
                  the below example
                  DECLARE @ord INT
                  BEGIN TRAN
                  >
                  SELECT @ord=MAX(OrderI d) FROM Order WITH (UPDLOCK,HOLDLO CK)
                  UPDATE Table SET orderid =@ord WHERE.........
                  >
                  COMMIT TRAN
                  >
                  "Rami" <ramialhasan@gm ail.comwrote in message
                  news:1168917890 .263676.286700@ a75g2000cwd.goo glegroups.com.. .
                  Thanks Uri,

                  Yes transactions do all opertaions like UPDATE/INSEERT/DELETE. Tables
                  will be growing fast becuase these are payment transactions. Indexes
                  are defined on the columns used too much in WHERE clauses.



                  Uri Dimant wrote:
                  Rami
                  What do the queries do? Do the transactions UPDATE/INSEERT/DELETE
                  operation?
                  How big are the tables? Do you have indexes defined on the tables?
                  >
                  >
                  >
                  "Rami" <ramialhasan@gm ail.comwrote in message
                  news:1168859468 .535588.297560@ 51g2000cwl.goog legroups.com...
                  I have some requirement for an automated payment system. The system
                  has
                  four machines setup as follows:
                  1- Two machines have a clustered database.
                  2- Two machines have a .net business logic component that will
                  handle
                  payment transactions with certain external component (Payment
                  Gateway)


                  My query is regarding transaction handling inside the business logic
                  component. Because this component is running on two machines and
                  these
                  two instances of the component are accessing the same transactions
                  table. I need these two components to work together without
                  blocking.
                  So transaction isolation level "Serialized " will not work with this
                  model.

                  I am not clear about how to lock the specific rows that are being
                  handled by certain instance. If I set a flag (column) to lock the
                  row,
                  then that instance may fail before resetting the flag then this row
                  will never be handled. In such a scenario I would expect the other
                  instance to take over and handle whatever was locked previously by
                  the
                  other instance.

                  Is there any ideas regarding arrangement between such components
                  which
                  access the same table?

                  Thanks a lot for the help,
                  Rami AlHasan

                  Comment

                  • Uri Dimant

                    #10
                    Re: Transactions and Locking

                    Rami
                    Why do you need READPAST hint? In very busy enviroment you can get DEADLOCK.
                    BOL says
                    READPAST
                    Skip locked rows. This option causes a transaction to skip rows locked by
                    other transactions that would ordinarily appear in the result set, rather
                    than block the transaction waiting for the other transactions to release
                    their locks on these rows. The READPAST lock hint applies only to
                    transactions operating at READ COMMITTED isolation and will read only past
                    row-level locks. Applies only to the SELECT statement.

                    "Rami" <ramialhasan@gm ail.comwrote in message
                    news:1169006809 .536360.205140@ q2g2000cwa.goog legroups.com...
                    One last question:
                    >
                    I updated your sample query in the following form:
                    >
                    DECLARE @ord INT
                    SET TRANSACTION ISOLATION LEVEL READ COMMITTED
                    BEGIN TRAN
                    SELECT @ord=MAX(ORDER_ ID) FROM Order WITH (UPDLOCK, READPAST)
                    UPDATE Order SET ORDER_STATUS= something WHERE (ORDER_ID = @ord)
                    COMMIT TRAN
                    >
                    The basic change here is that I removed the HOLDLOCK and put the
                    READPAST hint instead. I think the READPAST hint, will help in
                    filtering out those locked transactions. so each instance will see
                    unlocked transactiosns. But I removed HOLDLOCK because there was an
                    error generated if I included it with READPAST.
                    >
                    For me this seems to be working, is there any problem with this or any
                    hidden implications?
                    >
                    Thanks,
                    Rami
                    >
                    >
                    >
                    Uri Dimant wrote:
                    >Rami
                    This seems to be a very interesting idea... But I want to clarify, this
                    means that one instance of the component will execute this select
                    statement and take the lock. Then the other instance may execute the
                    same statement again but get a failure because the rows are already
                    locked. So the second instance will be blocked from handling
                    transactions. Is this correct?
                    >>
                    >It does not block readers , it does block writers.
                    >>
                    In my case I want each instance to select 100 rows for example and
                    process them completely before releasing them. But at the same time, I
                    want the other instance of the component to lock another 100 rows and
                    work on them exclusively.
                    >>
                    >Read about setting transaction isolation level in the BOL
                    >>
                    >>
                    >>
                    >>
                    >>
                    >>
                    >>
                    >>
                    >"Rami" <ramialhasan@gm ail.comwrote in message
                    >news:116893750 6.272849.4080@5 1g2000cwl.googl egroups.com...
                    Thanks again,
                    >
                    This seems to be a very interesting idea... But I want to clarify, this
                    means that one instance of the component will execute this select
                    statement and take the lock. Then the other instance may execute the
                    same statement again but get a failure because the rows are already
                    locked. So the second instance will be blocked from handling
                    transactions. Is this correct?
                    >
                    In my case I want each instance to select 100 rows for example and
                    process them completely before releasing them. But at the same time, I
                    want the other instance of the component to lock another 100 rows and
                    work on them exclusively.
                    >
                    Any help on this? and thanks a lot for your valuable answer.
                    Rami
                    >
                    >
                    >
                    Uri Dimant wrote:
                    >Rami
                    >You have to wrap the transactions with BEGIN TRAN ...COMMIT TRAN
                    >make
                    >sure that if you get a value an later on update it , use lockin hints
                    >as
                    >the below example
                    >DECLARE @ord INT
                    >BEGIN TRAN
                    >>
                    >SELECT @ord=MAX(OrderI d) FROM Order WITH (UPDLOCK,HOLDLO CK)
                    >UPDATE Table SET orderid =@ord WHERE.........
                    >>
                    >COMMIT TRAN
                    >>
                    >"Rami" <ramialhasan@gm ail.comwrote in message
                    >news:116891789 0.263676.286700 @a75g2000cwd.go oglegroups.com. ..
                    Thanks Uri,
                    >
                    Yes transactions do all opertaions like UPDATE/INSEERT/DELETE.
                    Tables
                    will be growing fast becuase these are payment transactions. Indexes
                    are defined on the columns used too much in WHERE clauses.
                    >
                    >
                    >
                    Uri Dimant wrote:
                    >Rami
                    >What do the queries do? Do the transactions UPDATE/INSEERT/DELETE
                    >operation?
                    >How big are the tables? Do you have indexes defined on the tables?
                    >>
                    >>
                    >>
                    >"Rami" <ramialhasan@gm ail.comwrote in message
                    >news:116885946 8.535588.297560 @51g2000cwl.goo glegroups.com.. .
                    >I have some requirement for an automated payment system. The
                    >system
                    >has
                    four machines setup as follows:
                    1- Two machines have a clustered database.
                    2- Two machines have a .net business logic component that will
                    handle
                    payment transactions with certain external component (Payment
                    Gateway)
                    >
                    >
                    My query is regarding transaction handling inside the business
                    logic
                    component. Because this component is running on two machines and
                    these
                    two instances of the component are accessing the same
                    transactions
                    table. I need these two components to work together without
                    blocking.
                    So transaction isolation level "Serialized " will not work with
                    this
                    model.
                    >
                    I am not clear about how to lock the specific rows that are being
                    handled by certain instance. If I set a flag (column) to lock the
                    row,
                    then that instance may fail before resetting the flag then this
                    row
                    will never be handled. In such a scenario I would expect the
                    other
                    instance to take over and handle whatever was locked previously
                    by
                    the
                    other instance.
                    >
                    Is there any ideas regarding arrangement between such components
                    which
                    access the same table?
                    >
                    Thanks a lot for the help,
                    Rami AlHasan
                    >
                    >
                    >
                    >

                    Comment

                    • Rami

                      #11
                      Re: Transactions and Locking

                      I need it because I need each instance of my component to handle
                      different set of transactions, So if the first instance selected 50
                      transactions to handle, then it will lock them.using the SELECT
                      WITH(UPDLOCK, READPAST). But the second instance should not get the
                      same 50, so I used the READPAST to filter out those who already been
                      locked. Does this make since?

                      But I didn't understand why deadlocks may happen?

                      Regards,
                      Rami


                      Uri Dimant wrote:
                      Rami
                      Why do you need READPAST hint? In very busy enviroment you can get DEADLOCK.
                      BOL says
                      READPAST
                      Skip locked rows. This option causes a transaction to skip rows locked by
                      other transactions that would ordinarily appear in the result set, rather
                      than block the transaction waiting for the other transactions to release
                      their locks on these rows. The READPAST lock hint applies only to
                      transactions operating at READ COMMITTED isolation and will read only past
                      row-level locks. Applies only to the SELECT statement.
                      >
                      "Rami" <ramialhasan@gm ail.comwrote in message
                      news:1169006809 .536360.205140@ q2g2000cwa.goog legroups.com...
                      One last question:

                      I updated your sample query in the following form:

                      DECLARE @ord INT
                      SET TRANSACTION ISOLATION LEVEL READ COMMITTED
                      BEGIN TRAN
                      SELECT @ord=MAX(ORDER_ ID) FROM Order WITH (UPDLOCK, READPAST)
                      UPDATE Order SET ORDER_STATUS= something WHERE (ORDER_ID = @ord)
                      COMMIT TRAN

                      The basic change here is that I removed the HOLDLOCK and put the
                      READPAST hint instead. I think the READPAST hint, will help in
                      filtering out those locked transactions. so each instance will see
                      unlocked transactiosns. But I removed HOLDLOCK because there was an
                      error generated if I included it with READPAST.

                      For me this seems to be working, is there any problem with this or any
                      hidden implications?

                      Thanks,
                      Rami



                      Uri Dimant wrote:
                      Rami
                      This seems to be a very interesting idea... But I want to clarify, this
                      means that one instance of the component will execute this select
                      statement and take the lock. Then the other instance may execute the
                      same statement again but get a failure because the rows are already
                      locked. So the second instance will be blocked from handling
                      transactions. Is this correct?
                      >
                      It does not block readers , it does block writers.
                      >
                      In my case I want each instance to select 100 rows for example and
                      process them completely before releasing them. But at the same time, I
                      want the other instance of the component to lock another 100 rows and
                      work on them exclusively.
                      >
                      Read about setting transaction isolation level in the BOL
                      >
                      >
                      >
                      >
                      >
                      >
                      >
                      >
                      "Rami" <ramialhasan@gm ail.comwrote in message
                      news:1168937506 .272849.4080@51 g2000cwl.google groups.com...
                      Thanks again,

                      This seems to be a very interesting idea... But I want to clarify, this
                      means that one instance of the component will execute this select
                      statement and take the lock. Then the other instance may execute the
                      same statement again but get a failure because the rows are already
                      locked. So the second instance will be blocked from handling
                      transactions. Is this correct?

                      In my case I want each instance to select 100 rows for example and
                      process them completely before releasing them. But at the same time, I
                      want the other instance of the component to lock another 100 rows and
                      work on them exclusively.

                      Any help on this? and thanks a lot for your valuable answer.
                      Rami



                      Uri Dimant wrote:
                      Rami
                      You have to wrap the transactions with BEGIN TRAN ...COMMIT TRAN
                      make
                      sure that if you get a value an later on update it , use lockin hints
                      as
                      the below example
                      DECLARE @ord INT
                      BEGIN TRAN
                      >
                      SELECT @ord=MAX(OrderI d) FROM Order WITH (UPDLOCK,HOLDLO CK)
                      UPDATE Table SET orderid =@ord WHERE.........
                      >
                      COMMIT TRAN
                      >
                      "Rami" <ramialhasan@gm ail.comwrote in message
                      news:1168917890 .263676.286700@ a75g2000cwd.goo glegroups.com.. .
                      Thanks Uri,

                      Yes transactions do all opertaions like UPDATE/INSEERT/DELETE.
                      Tables
                      will be growing fast becuase these are payment transactions. Indexes
                      are defined on the columns used too much in WHERE clauses.



                      Uri Dimant wrote:
                      Rami
                      What do the queries do? Do the transactions UPDATE/INSEERT/DELETE
                      operation?
                      How big are the tables? Do you have indexes defined on the tables?
                      >
                      >
                      >
                      "Rami" <ramialhasan@gm ail.comwrote in message
                      news:1168859468 .535588.297560@ 51g2000cwl.goog legroups.com...
                      I have some requirement for an automated payment system. The
                      system
                      has
                      four machines setup as follows:
                      1- Two machines have a clustered database.
                      2- Two machines have a .net business logic component that will
                      handle
                      payment transactions with certain external component (Payment
                      Gateway)


                      My query is regarding transaction handling inside the business
                      logic
                      component. Because this component is running on two machines and
                      these
                      two instances of the component are accessing the same
                      transactions
                      table. I need these two components to work together without
                      blocking.
                      So transaction isolation level "Serialized " will not work with
                      this
                      model.

                      I am not clear about how to lock the specific rows that are being
                      handled by certain instance. If I set a flag (column) to lock the
                      row,
                      then that instance may fail before resetting the flag then this
                      row
                      will never be handled. In such a scenario I would expect the
                      other
                      instance to take over and handle whatever was locked previously
                      by
                      the
                      other instance.

                      Is there any ideas regarding arrangement between such components
                      which
                      access the same table?

                      Thanks a lot for the help,
                      Rami AlHasan

                      Comment

                      • Uri Dimant

                        #12
                        Re: Transactions and Locking

                        locked. Does this make since?
                        If it depends on your business requieremnts, it's OK
                        But I didn't understand why deadlocks may happen?
                        Open more than three connection and run this script , well , in than
                        case you are going to get Primary Key Violation

                        --create table people (id int not null primary key, name char(1))


                        declare @id int
                        set @id=100
                        begin tran
                        if not exists (select * from people WITH ( updlock,readpas t ) where id=@id )
                        begin
                        waitfor delay '00:00:30'
                        insert into people (id, name) values (@id ,'h')
                        end
                        commit tran


                        "Rami" <ramialhasan@gm ail.comwrote in message
                        news:1169016520 .932286.63540@3 8g2000cwa.googl egroups.com...
                        >I need it because I need each instance of my component to handle
                        different set of transactions, So if the first instance selected 50
                        transactions to handle, then it will lock them.using the SELECT
                        WITH(UPDLOCK, READPAST). But the second instance should not get the
                        same 50, so I used the READPAST to filter out those who already been
                        locked. Does this make since?
                        >
                        But I didn't understand why deadlocks may happen?
                        >
                        Regards,
                        Rami
                        >
                        >
                        Uri Dimant wrote:
                        >Rami
                        >Why do you need READPAST hint? In very busy enviroment you can get
                        >DEADLOCK.
                        >BOL says
                        >READPAST
                        >Skip locked rows. This option causes a transaction to skip rows locked by
                        >other transactions that would ordinarily appear in the result set, rather
                        >than block the transaction waiting for the other transactions to release
                        >their locks on these rows. The READPAST lock hint applies only to
                        >transactions operating at READ COMMITTED isolation and will read only
                        >past
                        >row-level locks. Applies only to the SELECT statement.
                        >>
                        >"Rami" <ramialhasan@gm ail.comwrote in message
                        >news:116900680 9.536360.205140 @q2g2000cwa.goo glegroups.com.. .
                        One last question:
                        >
                        I updated your sample query in the following form:
                        >
                        DECLARE @ord INT
                        SET TRANSACTION ISOLATION LEVEL READ COMMITTED
                        BEGIN TRAN
                        SELECT @ord=MAX(ORDER_ ID) FROM Order WITH (UPDLOCK, READPAST)
                        UPDATE Order SET ORDER_STATUS= something WHERE (ORDER_ID = @ord)
                        COMMIT TRAN
                        >
                        The basic change here is that I removed the HOLDLOCK and put the
                        READPAST hint instead. I think the READPAST hint, will help in
                        filtering out those locked transactions. so each instance will see
                        unlocked transactiosns. But I removed HOLDLOCK because there was an
                        error generated if I included it with READPAST.
                        >
                        For me this seems to be working, is there any problem with this or any
                        hidden implications?
                        >
                        Thanks,
                        Rami
                        >
                        >
                        >
                        Uri Dimant wrote:
                        >Rami
                        This seems to be a very interesting idea... But I want to clarify,
                        this
                        means that one instance of the component will execute this select
                        statement and take the lock. Then the other instance may execute the
                        same statement again but get a failure because the rows are already
                        locked. So the second instance will be blocked from handling
                        transactions. Is this correct?
                        >>
                        >It does not block readers , it does block writers.
                        >>
                        In my case I want each instance to select 100 rows for example and
                        process them completely before releasing them. But at the same time,
                        I
                        want the other instance of the component to lock another 100 rows
                        and
                        work on them exclusively.
                        >>
                        >Read about setting transaction isolation level in the BOL
                        >>
                        >>
                        >>
                        >>
                        >>
                        >>
                        >>
                        >>
                        >"Rami" <ramialhasan@gm ail.comwrote in message
                        >news:116893750 6.272849.4080@5 1g2000cwl.googl egroups.com...
                        Thanks again,
                        >
                        This seems to be a very interesting idea... But I want to clarify,
                        this
                        means that one instance of the component will execute this select
                        statement and take the lock. Then the other instance may execute the
                        same statement again but get a failure because the rows are already
                        locked. So the second instance will be blocked from handling
                        transactions. Is this correct?
                        >
                        In my case I want each instance to select 100 rows for example and
                        process them completely before releasing them. But at the same time,
                        I
                        want the other instance of the component to lock another 100 rows
                        and
                        work on them exclusively.
                        >
                        Any help on this? and thanks a lot for your valuable answer.
                        Rami
                        >
                        >
                        >
                        Uri Dimant wrote:
                        >Rami
                        >You have to wrap the transactions with BEGIN TRAN ...COMMIT TRAN
                        >make
                        >sure that if you get a value an later on update it , use lockin
                        >hints
                        >as
                        >the below example
                        >DECLARE @ord INT
                        >BEGIN TRAN
                        >>
                        >SELECT @ord=MAX(OrderI d) FROM Order WITH (UPDLOCK,HOLDLO CK)
                        >UPDATE Table SET orderid =@ord WHERE.........
                        >>
                        >COMMIT TRAN
                        >>
                        >"Rami" <ramialhasan@gm ail.comwrote in message
                        >news:116891789 0.263676.286700 @a75g2000cwd.go oglegroups.com. ..
                        Thanks Uri,
                        >
                        Yes transactions do all opertaions like UPDATE/INSEERT/DELETE.
                        Tables
                        will be growing fast becuase these are payment transactions.
                        Indexes
                        are defined on the columns used too much in WHERE clauses.
                        >
                        >
                        >
                        Uri Dimant wrote:
                        >Rami
                        >What do the queries do? Do the transactions
                        >UPDATE/INSEERT/DELETE
                        >operation?
                        >How big are the tables? Do you have indexes defined on the
                        >tables?
                        >>
                        >>
                        >>
                        >"Rami" <ramialhasan@gm ail.comwrote in message
                        >news:116885946 8.535588.297560 @51g2000cwl.goo glegroups.com.. .
                        >I have some requirement for an automated payment system. The
                        >system
                        >has
                        four machines setup as follows:
                        1- Two machines have a clustered database.
                        2- Two machines have a .net business logic component that will
                        handle
                        payment transactions with certain external component (Payment
                        Gateway)
                        >
                        >
                        My query is regarding transaction handling inside the business
                        logic
                        component. Because this component is running on two machines
                        and
                        these
                        two instances of the component are accessing the same
                        transactions
                        table. I need these two components to work together without
                        blocking.
                        So transaction isolation level "Serialized " will not work with
                        this
                        model.
                        >
                        I am not clear about how to lock the specific rows that are
                        being
                        handled by certain instance. If I set a flag (column) to lock
                        the
                        row,
                        then that instance may fail before resetting the flag then
                        this
                        row
                        will never be handled. In such a scenario I would expect the
                        other
                        instance to take over and handle whatever was locked
                        previously
                        by
                        the
                        other instance.
                        >
                        Is there any ideas regarding arrangement between such
                        components
                        which
                        access the same table?
                        >
                        Thanks a lot for the help,
                        Rami AlHasan
                        >
                        >
                        >
                        >
                        >

                        Comment

                        • Alex Kuznetsov

                          #13
                          Re: Transactions and Locking


                          Rami wrote:
                          I have some requirement for an automated payment system. The system has
                          four machines setup as follows:
                          1- Two machines have a clustered database.
                          2- Two machines have a .net business logic component that will handle
                          payment transactions with certain external component (Payment Gateway)
                          >
                          >
                          My query is regarding transaction handling inside the business logic
                          component. Because this component is running on two machines and these
                          two instances of the component are accessing the same transactions
                          table. I need these two components to work together without blocking.
                          So transaction isolation level "Serialized " will not work with this
                          model.
                          >
                          I am not clear about how to lock the specific rows that are being
                          handled by certain instance. If I set a flag (column) to lock the row,
                          then that instance may fail before resetting the flag then this row
                          will never be handled. In such a scenario I would expect the other
                          instance to take over and handle whatever was locked previously by the
                          other instance.
                          >
                          Is there any ideas regarding arrangement between such components which
                          access the same table?
                          >
                          Thanks a lot for the help,
                          Rami AlHasan
                          Sounds like a good scenario to use a service broker.

                          -----------------------
                          Alex Kuznetsov



                          Comment

                          Working...