Synclock

F

fred

I have a VB application that is using MS Access as its database.
To avoid connection delays the application creates one connection to the
database at start-up and maintains that single connection throughout until
the application closes.
I now want to use multithreading to improve the feel of the application but
I need some help understanding Synclock. The aim here is to prevent any two
threads from trying to use the same connection at the same time.

The application has a main form which contains the connection to the
database declared as:
Private mConn as OleDB.OledbConnection

Any routines within the main Form's class I can use:
Synclock mConn
...do any database access here using mConn
End Synclock

But whenever I need to access the database from routines outside the scope
of the main form I cannot Synclock onto mConn directly so I need to provide
access to this Oledbconnection.
I list some methods below and I wonder if anyone can tell me if there are
any problems using any of the methods listed and if there is any preference.

1/
Provide public access to the connection by creating a Public property of the
main form:
Public Readonly Property myConnection as OleDB.OledbConnection
Get
Return mConn
End Get
End Property
then in other routines I can use
Synclock mainForm.myConnection
...do any database access here using mainForm.myConnection
End Synclock

Does this method work? That is will a thread using "Synclock
mainForm.myConnection" block other threads using "Synclock mConn" or
"Synclock mainForm.myConnection"

2/
Pass the connection as a parameter in routines outside the scope of the main
form:
Sub SomeDBAccess(ByVal DBConn as Oledb.OledbConnection)
....
Synclock DBConn
do any database access here using DBConn
End Synclock
....
End Sub

Does this method work? That is will a thread using "Synclock DBConn" block
other threads using "Synclock mConn" or "Synclock DBConn"
Also, using this method does it make any difference to Synclock if the
parameter is passed ByVal or ByRef?

3/
Pass the connection as a parameter in routines outside the scope of the main
form but pass it as an Object:
Sub SomeDBAccess(ByVal aObj as Object)
....
Synclock aObj
do any database access here using Ctype(aObj, OledbConnection)
End Synclock
....
End Sub

Does this method work? That is will a thread using "Synclock aObj" block
other threads using "Synclock mConn" or "Synclock aObj"
Would it make any difference if I used Synclock Ctype(aObj, OledbConnection)
instead.
And again, using this method does it make any difference to Synclock if this
parameter is passed ByVal or ByRef?


Thanks for any assistance
Fred
 
C

Cor Ligthert

Fred,

In my opinion is the synclock very good describted on this page.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmsynclock.asp

So when you synclock the constructor of the connection. In that time other
threads cannot reach the data involved in *that* connection while the
connection is contructed. After that the data is normal usable. AFAIK is
there no changement by more threads used data involved in the construction
of the connection.

Moreover, you can have in dotNet 2002/2003 only have one connection open at
a time, so it does in my opinion nothing.

I try to avoid Synclock as much as possible by instance by creating the data
seperated, where at the end of the thread I update the data in memory.

Just some thoughts,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top