Solved

Dll CMO-Objekt with 2 clients

Posted on 2002-06-25
5
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 7107699
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
ID: 7107753
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 70

Expert Comment

by:Éric Moreau
ID: 7107820
Which error?

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

Expert Comment

by:AzraSound
ID: 7107824
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
ID: 7107850
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

739 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