Lock dataset while other user is editing

Lock dataset while other user is editing

jaewebjaeweb Posts: 13Questions: 2Answers: 0
edited January 2020 in DataTables

Hi
We are trying to implement a dataset lock if another user is already editing the same datatset.

Because every user is logged in, we know the id of the user. This id should be written to the dataset on opening the editor and reset after submitting or closing the editor.

On submit this works perfectly. But on closing we have the issue, that if another row in the table is selected, another dataset is updated with the user lock.

Here is our edit button code:

{
    text: '<i class="fas fa-pen"></i>',
    action: function ( ) {

        var refID = adressenTable.rows( { selected: true } ).data()[0].viw_adressen.RefID;
        var KontakteID = adressenTable.rows( { selected: true } ).data()[0].viw_adressen.KontakteID;

        var adressenTableSelectedRows = adressenTable.rows( { selected: true } );
        var adressenTableSelectedRow = adressenTable.row( { selected: true } ).index();
        adressenTable.ajax.reload(function(e){

            // Fehlermeldung ausgeben wenn der Datensatz bereits bearbeitet wird
            if (adressenTableSelectedRows.data()[0].viw_adressen.useredit > 0){
                swal({
                    title: "Warnung",
                    text: "Dieser Datensatz wird bereits bearbeitet.",
                    type: "warning"
                });
            } else if (!$('#row_'+KontakteID).length){
                swal({
                    title: "Warnung",
                    text: "Der Status dieses Datensatzes ist in Bearbeitung. Bitte neu selektieren.",
                    type: "warning"
                });
            } else {

                editorFirma.edit( adressenTableSelectedRow, false )
                    .set( 'viw_adressen.useredit', 22 )
                    .submit(function(){

                    // Informationsbox mit Memo anzeigen falls Memo vorhanden
                    if (adressenTableSelectedRows.data()[0].viw_adressen.KontakteAlarmText){
                        swal({
                            title: "Information",
                            text: adressenTableSelectedRows.data()[0].viw_adressen.KontakteAlarmText,
                            type: "info"
                        });
                    }


                    // Firma bearbeiten
                    if (KontakteID == refID) {
                        editorFirma.edit('#row_'+refID).buttons( {
                            text: 'Speichern',
                            action: function () {
                                // Datensatz freigeben
                                this.set( 'viw_adressen.useredit', "" );
                                this.submit();
                            }
                        } );
                    } else {
                        // Firmenkontakt bearbeiten
                        editorKontakt.edit('#row_'+KontakteID).buttons( {
                            text: 'Speichern',
                            action: function () {
                                // Datensatz freigeben
                                this.set( 'viw_adressen.useredit', "" );
                                this.submit();
                            }
                        } );
                    }

                }); 

            }   

            // Beim schliessen den User wieder aus dem Datensatz austragen
            editorFirma.on( 'close', function ( e, json/*, data */) {
                editorFirma.edit( adressenTableSelectedRow, false )
                    .set( 'viw_adressen.useredit', "" )
                    .submit();

            } );
            editorKontakt.on( 'close', function ( e, json/*, data */) {
                editorKontakt.edit( adressenTableSelectedRow, false )
                    .set( 'viw_adressen.useredit', "" )
                    .submit();

            } );
        }, false);

    },
    titleAttr: 'Bearbeiten',
    enabled: false,
    className:  'space'
},

We also tried to disable pointer-events while processing, but it seems, that pointer-events is enabled in between closing and updating:

$('table')
    .on( 'processing.dt', function ( e, settings, processing ) {
        $('table').css( 'pointer-events', processing ? 'none' : 'auto' );
    } )
    .dataTable();

Thank you very much in advance!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    if another row in the table is selected, another dataset is updated with the user lock.

    When you say "dataset" do you mean the entire database table? So you are implementing table level locking, rather than row level locking?

    If that's the case, which dataset / table is being updated with the user lock? I don't really understand how that would happen I'm afraid - surely the release would just delete the lock from the locking table?

    Allan

  • jaewebjaeweb Posts: 13Questions: 2Answers: 0

    Hi allan
    Thank you very much for oyur reply.

    Sorry for not describing it properly: With dataset I mean 1 row in a database table (I meant record - false friend: German "datensatz" means "record" and not "dataset". :)

    So what we want is row level locking.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Ah! Thanks for the explanation. So slightly rewriting your first post:

    On submit this works perfectly. But on closing we have the issue, that if another row in the table is selected, another record is updated with the user lock.

    That sounds correct to me. On the Editor open (or row select if you prefer) then the lock should be applied. On close it should be released (regardless of if the close is caused by a successful submit or the form just being closed without saving by the end user).

    The open and close events are the ones I would suggest using for this.

    Allan

  • jaewebjaeweb Posts: 13Questions: 2Answers: 0
    edited January 2020

    Hi Allan

    Thank you very much for your explanation.

    We have the problem, that sometimes the wrong records are being unlocked. The record that has been actually edited stays locked. We don't know why this happens. I assume, that the event "on close" fires too late and that it is possible to select another record in the meantime. I've observed that the "please wait" popup is shown twice: Once for saving the editor form (mysql update) and after this once more for updating the user lock. Somehow it seams possible to change the record between these two updates. In this case, the record stays locked.

    Is there a way to prevent all user interaction before the "on close" event has finished updating the record?

    Last bu not least: Thank you very much for you great work!

  • jaewebjaeweb Posts: 13Questions: 2Answers: 0
    edited January 2020

    I've found a solution:

    editorKontakt.on( 'close', function( e, json, data ){
            console.log(e.target.s.fields["viw_adressen.KundenNummer"].s.multiIds[0]);
            editorKontakt.edit( '#'+e.target.s.fields["viw_adressen.KundenNummer"].s.multiIds[0], false )
                .set( 'viw_adressen.useredit', "" )
                .submit();
    
    });
    

    I'm using the function variable "e" to get the correct row. Now it works.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Nice one - thanks for posting back with your solution.

    Allan

This discussion has been closed.