Lock a database!!!

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!!!
cplauAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
DhrubajyotiConnect With a Mentor Commented:

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
 
V_BapatCommented:
Listening.
0
 
cplauAuthor Commented:
Edited text of question.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
cplauAuthor Commented:
Edited text of question.
0
 
b_samratCommented:
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
 
V_BapatCommented:
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
 
cplauAuthor Commented:
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
 
V_BapatCommented:
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
 
b_samratCommented:
hi
get a pointer to ur CDaoWorkspace through m_pWorkspace of ur CDaoDatabase object.Use BeginTrans and ComitTrans to
lock and unlock.

samrat
0
 
cplauAuthor Commented:
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
 
cplauAuthor Commented:
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
 
V_BapatCommented:
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
 
DhrubajyotiCommented:
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
All Courses

From novice to tech pro — start learning today.