SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Database locking (prevent inconsistent data)



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SQL and Database Changes
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sat Oct 13, 2007 2:00 pm    Post subject: Database locking (prevent inconsistent data) Reply with quote

SAP provides you with the ability to restrict access to data while the table is being updated. This is fairly simple to implement via the use of a lock object (Created in SE11).

Step 1 - Create Lock object (SE11)



Step 2 - ABAP code to lock table entries
Add the following code in-order to create the table lock. This function module must be called before any update takes place. If a lock has already been taken out it will display the appropriate message.

Code:
  CALL FUNCTION 'ENQUEUE_EZ_ZTABLENAME'
      EXPORTING
           mode_ZTABLENAME = 'E'
           mandt              = sy-mandt
           KEYFIELD1           = "Value
           KEYFIELD2           = "Value
           KEYFIELD3           = "Value
                   ...
*         X_KEYFIELD1            = ' '
*         X_KEYFIELD2            = ' '
*         X_KEYFIELD3            = ' '
                   ...
*         _SCOPE             = '2'
*         _WAIT              = ' '
*         _COLLECT           = ' '
*   If exceptions are not used, message is displayed within FM
    EXCEPTIONS
         FOREIGN_LOCK       = 1
         SYSTEM_FAILURE     = 2
         OTHERS             = 3.
             
  IF sy-subrc <> 0.
*   Retrieve message displayed within Function Module
    message id     sy-msgid
              type   'I'
              number sy-msgno
              with   sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    EXIT.
  ENDIF.


Step 3 - ABAP code to Remove table lock(s)
The following code will remove the lock for the specific table entries.
Code:
CALL FUNCTION 'DEQUEUE_EZ_ZTABLENAME'
    EXPORTING
         MODE_ZTABLENAME = 'E'
         MANDT              = SY-MANDT
           mandt              = sy-mandt
           KEYFIELD1           = "Value
           KEYFIELD2           = "Value
           KEYFIELD3           = "Value
                   ...
*         X_KEYFIELD1            = ' '
*         X_KEYFIELD2            = ' '
*         X_KEYFIELD3            = ' '
                   ...
*         _SCOPE             = '3'
*         _SYNCHRON          = ' '
*         _COLLECT           = ' '
          .
 
Back to top
View user's profile Send private message
vga
Мастер
Мастер


Age: 90
Joined: 04 Oct 2007
Posts: 1218
Location: Санкт-Петербург

PostPosted: Tue Feb 28, 2012 3:31 pm    Post subject: Reply with quote

Code:
*&---------------------------------------------------------------------*
*&      Form  SAVE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM save USING p_confirm TYPE as4flag.

  CONSTANTS: lc_save TYPE c VALUE '1'.

  DATA: l_answer TYPE c.
  DATA: ls_hist TYPE zeruf_ndssb_hist,
        lt_hist TYPE TABLE OF zeruf_ndssb_hist.
  FIELD-SYMBOLS <fs> TYPE t_sb.

  READ TABLE gt_alvdata WITH KEY changed = 'X'.
  CHECK sy-subrc IS INITIAL.

  IF NOT p_confirm IS INITIAL.
    CALL FUNCTION 'POPUP_TO_CONFIRM'
      EXPORTING
        titlebar              = 'Confirm'(301)
        text_question         = 'Data was changed. Are you want to save?'(302)
        text_button_1         = 'Yes'
        text_button_2         = 'No'
        default_button        = '1'
        display_cancel_button = ''
      IMPORTING
        answer                = l_answer
      EXCEPTIONS
        text_not_found        = 1
        OTHERS                = 2.
  ELSE.
    l_answer = lc_save.
  ENDIF.

  IF l_answer = lc_save.
    LOOP AT gt_alvdata ASSIGNING <fs> WHERE NOT changed IS INITIAL.
      MOVE-CORRESPONDING <fs> TO ls_hist.
      ls_hist-bukrs = p_bukrs.
      APPEND ls_hist TO lt_hist.
    ENDLOOP.

    DATA: l_garg  LIKE seqg3-garg,
          lt_enq  LIKE seqg3 OCCURS 1 WITH HEADER LINE,
          l_ebeln TYPE rseg-ebeln,
          l_lock.

    DO 30 TIMES.
      CLEAR l_lock.
      REFRESH lt_enq.

      CONCATENATE sy-mandt '*' INTO l_garg.
      CALL FUNCTION 'ENQUEUE_READ'
        EXPORTING
          gclient               = sy-mandt
          gname                 = 'ZERUF_NDSSB_HIST'
          garg                  = l_garg
          guname                = sy-uname
        TABLES
          enq                   = lt_enq
        EXCEPTIONS
          communication_failure = 1
          system_failure        = 2
          OTHERS                = 3.
      IF NOT sy-subrc IS INITIAL OR NOT lt_enq[] IS INITIAL.
        l_lock = 'X'.
      ENDIF.
      IF NOT l_lock IS INITIAL.
        WAIT UP TO 2 SECONDS.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.

    CALL FUNCTION 'ENQUEUE_EZ_NDSSB_HIST'
      EXPORTING
        mode_zeruf_ndssb_hist = 'E'
        mandt                 = sy-mandt
      EXCEPTIONS
        foreign_lock          = 1
        system_failure        = 2
        OTHERS                = 3.

    IF sy-subrc <> 0.
*   Retrieve message displayed within Function Module
      MESSAGE ID     sy-msgid
                TYPE   'I'
                NUMBER sy-msgno
                WITH   sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      EXIT.
    ENDIF.

    MODIFY zeruf_ndssb_hist FROM TABLE lt_hist.
    COMMIT WORK AND WAIT.
    IF sy-subrc IS INITIAL.
      CLEAR gt_alvdata-changed.
      MODIFY gt_alvdata FROM gt_alvdata TRANSPORTING changed
        WHERE NOT changed IS INITIAL.
    ENDIF.

    CALL FUNCTION 'DEQUEUE_EZ_NDSSB_HIST'
      EXPORTING
        mode_zeruf_ndssb_hist = 'E'
        mandt                 = sy-mandt.

  ENDIF.
ENDFORM.                    " SAVE

_________________
Молитва - это запрос разработчику на изменение кода программы.
Back to top
View user's profile Send private message Blog Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SQL and Database Changes All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.