Symbian
Symbian OS Library

FAQ-1058 I have a database I would like to share, how do I do this ?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: DBMS
Created: 05/25/2004 Modified: 06/18/2004
Number: FAQ-1058
Platform: Symbian OS v6.0, Symbian OS v6.1, Symbian OS v7.0, Symbian OS v7.0s, Symbian OS v8.0, Symbian OS v8.0a, Symbian OS v8.0b

Question:
I have created a database and would like to share it between different threads, how can I do this ?

Answer:
Each thread needs to access the database using the RDbs class. To ensure that several threads do not make changes to the database simultaneously use the methods Begin() and Commit(). After Begin() has been called the database is locked; if another thread attempts to call Begin() on the same database the error KErrLocked is returned. Calling Commit() un-locks the database. If the KErrLocked error is ignored any attempts to change the database using the RDbView class will Leave or Panic.

If you find the database is locked you can use the RDbNotifier class to get notification of when the database has been unlocked. This class has a method NotifyUnlock(TRequestStatus& aStatus) which can be used to notification when the database is no longer locked.

For definitions of the APIs mentioned in this FAQ and a little more information refer to the Developer Library under the "DBMS Sharing Database" section.

The code below shows an example of using the shared database APIs:

_LIT(KDb,"MyDb.db"); // database name
_LIT(KDbPath,"C:\\MyFolder\\Data\\"); // path for the .db file
RDbNamedDatabase db;
RFs rfs;
User::LeaveIfError(rfs.Connect());
CleanupClosePushL(rfs);
TParse name;
rfs.Parse(KDb,KDbPath,name);
RDbs dbs;
User::LeaveIfError(dbs.Connect());
CleanupClosePushL(dbs);
User::LeaveIfError(db.Open(dbs,name.FullName()));
CleanupClosePushL(db);

// do some manipulating of the database
if(db.Begin() == KErrNone) // Locks the database from other threads
{
// change something
User::LeaveIfError(db.Commit()); // Unlocks the database for other threads, Leave if there is an error
}

CleanupStack::PopAndDestroy(3); // rfs, dbs, db