Solved

Dll CMO-Objekt with 2 clients

Posted on 2002-06-25
5
178 Views
Last Modified: 2010-05-02
I have written a COM-dll in VB to write records in a
access database. This dll is used by 2 applications
simultaneous. Now I got the problem, that whenever both
applications want to write to the DB an error occurs.
I tried to lock the DB calling function as long as one
application writes to the DB, with a global variable like
this:
  Do While FunctionLocked = True
    Sleep (100)
  Loop
  FunctionLocked = True
  ProtocolWrite = AddProtRecord(ByVal ProtType, ByVal  IDType, ByVal InfoCount, vaInfo)
  FunctionLocked = False

  (In AddProtRecord are the database functions)



Does anyone know a solution for this problem. I know
I have to program a semaphore (maybe a C-programmer could help me) but I don not know how.

Some additional informations:
I connect the DB with:

cnDB.Provider = "Microsoft.Jet.OLEDB.4.0"
cnDB.Open PathDB
...

I open the recordset and write to the DB:
  With rsDB
    On Local Error GoTo CanNotOpenErr
    .CursorType = adOpenStatic
    .Open strName, cnDB, 3, 3
    If rsDB.RecordCount > 0 Then
      .MoveFirst
      .Fields(1).Value = Now
      .Fields(2).Value = ProtType
      .Fields(3).Value = IDType
      .Fields(4).Value = 88
      .Fields(5).Value = InfoCount
      .Fields(6).Value = ProtText
      .Update
    Else
      .AddNew
      .Fields(1).Value = Now
      .Fields(2).Value = ProtType
      .Fields(3).Value = IDType
      .Fields(4).Value = 88
      .Fields(5).Value = InfoCount
      .Fields(6).Value = ProtText
      .Update
    End If
   
    ' Recordset Connection schliesen
    .Close
  End With
...

Must I add additional information to the cnDB
(Connectionset Object) or the rsDB (Recordset Object)?


Greetings
   alpha
0
Comment
Question by:_alpha
5 Comments
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
What are you using to update the database?  Have you tried letting the database manage the locking by setting pessimistic locking on the recordset object?  You can then check for errors from one user trying to update a locked record, etc.
0
 
LVL 3

Expert Comment

by:PNJ
Comment Utility
You can't use a DLL to "share" variables. Each program gets a copy of the DLL in its own process space. So "FunctionLocked" is only ever "local" to each program. ie Two programs, two "FunctionLocked"'s

Sorry, can't help with the Access bits though. I'm fairly certain that it can be done. Anyone else know about record locking in Access between two or more programs?
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
Which error?

I hope that you are using INSERT SQL statement (instead of the AddNew method).
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
There is a white paper here you can read to better understand the record locking techniques/performance of Microsoft Jet DB Engine:

"ACC: Microsoft Jet Utilities Available in Download Center (Q176670)"
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q176670
0
 
LVL 18

Accepted Solution

by:
mdougan earned 500 total points
Comment Utility
You will have to be more specific about the contention.  Namely, how are you opening the Access database in the DLL?  Are you opening it Exclusively?  

Are you specifying a locktype?  Pessimistic, Optimistic?  

Are the two programs trying to update the same exact rows in the database table?  Or are they updating rows that might be physically near one another?

Are you using updatable recordsets or are you issuing Update SQL Statements?

For the greatest concurrency, you would normally open Read-Only recordsets, and then issue SQL Update statements to update the database.

If you want to be able to update the recordsets, then you should specify Optimistic Locking, and trap for errors relating to a record that has been updated by another program while the first program has been actively trying to update the row (you then have the option of letting the first program overlay the changes made by the other program or abandoning the changes).

With Pessimistic locking, Access will not only lock the particular record that you are editing, but also all other rows that exist on the same data page.  This might include several records if your row length is small.  For that reason, it is not advisable except in very specific circumstances.

You might want to tell us what data access methods you are using (ADO, DAO etc) and perhaps post a bit of code of how you open the database/connection and how you open the recordset and/or perform your updates.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 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

12 Experts available now in Live!

Get 1:1 Help Now