PDA

View Full Version : Lock strategy



pippo42
4th May 2010, 12:38
Hello,

I am using a QSqlTableModel and would like to have the following behaviour. If a user tries modify a record, we lock the record and if it was already locked (i. e. the lock fails) we issue an error message and prevent the user to edit the occurence.

I could to it the hard way by trying to detect when the user starts editing a field and do a SELECT fOR UPDATE NOWAIT at that moment but it seems messy and I assume there must be a natural way to do so in Qt.

So what would be the right way to do that in Qt?

Thanks!

pippo42
4th May 2010, 16:54
If there is no standard way...

Here is what I know.

The normal way in Qt is that if an occurence is modified in a transaction and that another transaction accesses it, it just freezes when the model tries to carry out the update.

There are several problems with that.

1. The user does not know, before the update, if his changes will be accepted. It would be better to react at the beginning of the edition.

2. I would have preferred it if the program did not access all the time the database.

3. I have a strange bug when I am not using the "on manual submit" strategy. When I edit a cell, another in the table is randomly modified. I have posted a message here but found no solution

4. It freezes. It would be better to have an error message like when you have done a SELECT FOR UPDATE NOWAIT

I am ready to accept 1 and 2. I am desperate about 3 but issue 4 is definitely a problem. It should not freeze.

The only solution I see would be to use the "on manual submit" strategy to avoid the strange bug (cf 3) and do a SELECT FROM UPDATE NOWAIT at the beginning of every modification or when I change of occurence. But the textChanged signal is emitted each time a character is typed and I would need to place something in the createEditor function in the item delegate. This way, I will be able to set the locks and to prevent the edition in case the query fails (ie if the record is already locked). It would be more natural to detect changes at the level of the model but Isee no signals I could use or events I could override.

It seems complicated...

At least, I would be glad if I could somehow that freeze. How could I do?

And, what would be the natural way to implement the locks in Qt? I assume there something for that. I do not ask for a ready made solution. But if someone could point me to a piece of documentation, give me a class name...

Thanks!

There i

pippo42
5th May 2010, 07:54
In case of a problem, there are usually knowledgeable people who reply helpfully and very quickly.

I guess that your silence means there is nothing in Qt that does that. There is no natural way that has been programmed to manage locks. So there is no way to build a normal, reliable database application.

This is incredible. In my professionnal life, I have basically two backgrounds: scienfific/mathematical programming in C++ and database applications in other languages. For this large project, I had the opportunity to choose the tools I wanted so I choose my favourite language C++ and Qt that I dod not know and looked promising. Since the beginning I have struggled against its shortcomings in terms of database management. The examples are numerous; QSqlRelationModel that does not handle left outer joins, QSqlTableModel that does not allow multiple sort fields and many others plus some strange things that occurs for which no one seems to have an aswer. There are other difficulties: the lack of classical events that prevent the programmer to do the things he can do in other frameworks. But this is not really a problem. It become a nightmare if you try to do things you are used to in other frameworks but, if you follow its logic,it can deliver a solution that is more elegant.

But the conclusion of the difficulties I have had and of the absence of clean mechanism to handle the locks are at least to detect the beginning of the edition of an occurence at the model level men that it is more or less impossible to do a clean, usual and reliable database application with Qt.

I have really tried to learn, to follow its logic and not to rant because it was lacking features present in other frameworks. But I am dicovering that some things are plainly impossible.

In total, I have wasted months because of this. I had for solutions (sometimes dodgy) for most of the problems but that lock issue is the end of it. I am writing an application handling medical data used by a large number of people and whet Qt offers is simply insufficient.

wysota
5th May 2010, 10:31
1. The user does not know, before the update, if his changes will be accepted. It would be better to react at the beginning of the edition.
That would be premature and inefficient. If someone starts editing an item and then leaves the computer before leaving edit mode, the record would be locked and nothing else could access it.


2. I would have preferred it if the program did not access all the time the database.
It's just a matter of submission strategy you set. You can tell the model to submit the data just once.


3. I have a strange bug when I am not using the "on manual submit" strategy. When I edit a cell, another in the table is randomly modified. I have posted a message here but found no solution
Provide more info, please.


4. It freezes. It would be better to have an error message like when you have done a SELECT FOR UPDATE NOWAIT
What freezes? In what conditions?


And, what would be the natural way to implement the locks in Qt? I assume there something for that. I do not ask for a ready made solution. But if someone could point me to a piece of documentation, give me a class name...
What is the exact problem you are having? You can issue SELECT FOR UPDATE, as you already said.


In case of a problem, there are usually knowledgeable people who reply helpfully and very quickly.
Yes but they like the question to be asked clearly. The more paragraphs (above 2-3) you write in your posts, the less answers you will get :) Answering your own posts leads to that too - i.e. I read mostly these threads that haven't been replied.


So there is no way to build a normal, reliable database application.
This is just trolling. It's easy to throw accusations, it's harder to find humility to look for problems in your own code.


The examples are numerous; QSqlRelationModel that does not handle left outer joins, QSqlTableModel that does not allow multiple sort fields and many others plus some strange things that occurs for which no one seems to have an aswer.
These are models that are meant to work for 90% of the people in 90% of the cases. Qt is not dedicated to database programming, remember that. If you're so good in C++ then subclassing proper classes and adding support for what you want shouldn't pose a problem.


There are other difficulties: the lack of classical events that prevent the programmer to do the things he can do in other frameworks.
What do you mean by "lack of classical events"? You can use both native platform events and Qt wrapped (or Qt generated) events without any problems. I don't know if any of these fall into the category of "classical events" but for me it would seem so.


But this is not really a problem. It become a nightmare if you try to do things you are used to in other frameworks but, if you follow its logic,it can deliver a solution that is more elegant.
It would be stupid to project habits from one framework onto another with completely different rules and architecture. Imagine going to a golf course with a tennis racket and blaming golf balls for lying on the ground all the time.


I have really tried to learn, to follow its logic and not to rant because it was lacking features present in other frameworks. But I am dicovering that some things are plainly impossible.
There is a very simple answer for that, actually... RTFM! I assure you you can write reliable database applications in Qt if anything because Qt doesn't prevent you from sending any query to the database or using any library available for C++.