Solved

Lock a database!!!

Posted on 2000-04-25
13
259 Views
Last Modified: 2013-11-20
Hi,

I have the following question:

If I have developed two applications and opened the same database by these two applications, I  afraid the the same record at the same database will be modified/deleted at the same time.

Will this suitation occur? If yes, will any error popup/ will the applicaiton die?

If the application will die, how can I prevent this?

I hope I can lock the record in order to  prevent the same record at the same database may be modified by two users at the same time.

Note I am using the CDaoDatabase & CDaoRecordset classes to perfrom different database operataions
(add/edit/delete).

Thanks for your opinion!!!
0
Comment
Question by:cplau
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2747327
Listening.
0
 

Author Comment

by:cplau
ID: 2747371
Edited text of question.
0
 

Author Comment

by:cplau
ID: 2749837
Edited text of question.
0
 

Expert Comment

by:b_samrat
ID: 2750227
hi
Yes , u can lock the database(or a table in the database) when one application is using it and unlock it after the use.
for this u have to open the database Exclusively and have to use pessimistic locking or optimestic locking of tables according to ur need.

all the best
samrat
0
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2750285
Samrat:
I don't know much, but this is what MSDN (October 1999) says about opening a database exclusively:

"Not supported in this version of the class library. Currently, an assertion fails if this parameter is TRUE. The data source is always opened as shared (not exclusive)."

cplau:
You can use synchronization objects. For example, mutex object. MFC provides CMutex class for this.

Vicky
0
 

Author Comment

by:cplau
ID: 2750415
Sorry for not describing the problem clearly.

First, I really need to open the same database by two applications at the same time. Thus, to open the database Exclusively is not a good method to me.

Further, I have an question about using CMutex. If
one user is editing one record at the database, I am not sure whether another user can read the database at this moment or not!!!

I am really worring about if the same record at the database is being modified/deleted by two users at the same time, will any exception error occur and force the program die?

I don't mind there will be some exception errors popup but I don't want the program will die in this suitation?
Can anyone tell me more detail about this?

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:V_Bapat
ID: 2750503
For more info about how mutex objects work, type "mutex objects" in the index of MSDN. There a sample about using the same database from multiple threads.
0
 

Expert Comment

by:b_samrat
ID: 2750652
hi
get a pointer to ur CDaoWorkspace through m_pWorkspace of ur CDaoDatabase object.Use BeginTrans and ComitTrans to
lock and unlock.

samrat
0
 

Author Comment

by:cplau
ID: 2750664
Hi V_Bapat,

I still have a question in using Mutex objects. If I open the same database by two applications which had been installed at different PCs, will CMutex class is still useful for me to lock the database.

Note, one application may open a database which is located at another PC's harddisk through the LAN!!!

Thank you!!!
0
 
LVL 1

Accepted Solution

by:
Dhrubajyoti earned 70 total points
ID: 2751000

in case of deleteing same record it should give an error and application will not terminate . But some time it may hang.
so before inserting record into databasa you should call the begintrnas method after the end of transaction you  should call committrans(of database ) .if any error occure then it will give an error .
  you should also keep the whole block in to a try catch block
 

try {
     //transaction statement
}
      catch(CDBException *e)
      {
            AfxMessageBox("This is an error,Err.Code ="+e->m_strError +" You must quit this session. All changes will be roled back!" , MB_ICONSTOP);
            pDatabase->Rollback();
                  }  

this will prevent the dead lock situation.

 you can also lock the table you are using for transaction

you can use setlockingmode function of cdaorecordset to lock the database.
 
0
 

Author Comment

by:cplau
ID: 2751827
Hi Dhrubajyoti,

After reading your answer, I have a question.
Do you mean there will exist an error if two applications are trying to add record at the same database?

If I use the begintrnas method, will only one application can use the database at the same time. How about the next application? If this applicaiton also wants to add/edit/delete a record at the same time, will this request ignore or wait the previous application finish using the database?

Thanks!!!!
0
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2754494
Mutex objects will work only when both the apps are in the same m/c.

Can you explain the setup? I mean, where is the database residing and where are the machines on which the apps are accessing the database?

It may be a good idea to have an interface for accessing a resource(table/recordset) in the database. For example, whenever an app wants to update/delete/insert a record into a table/recordset, it has to get the permission for do so. If some other app wants to work on the same table/recordset, it will wait till the 1st app releases the table/recordset. You may use the windows registry(where database resides) to store the information about the availability of the resource.

Think about it.
0
 
LVL 1

Expert Comment

by:Dhrubajyoti
ID: 2758148
Begintrans means recording the transaction . so when you are commiting then the changes will reflect on the database. so two user perform the same kind of operation then the fist one will have no problem and for the second person will get a error so it will go to catch block .but ur application will not stop.now if you are looking the the database using setlock mode then it will be safe transaction as no one is accessing the locked recordset.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now