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
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