Link to home
Start Free TrialLog in
Avatar of josevillar
josevillar

asked on

locking DB2 table using ODBC

Hello all:

I'm having a little trouble with LOCK TABLE statement. It does not work. I'm using VBasic and ODBC to
do it.

LOCK TABLE mytable in EXCLUSIVE MODE

but after that any other user can acces this table without any problem.

Any idea?

thanks


Avatar of jdc0724
jdc0724

Howdy, you need to specify the appropriate options on your "openConnection()" method.  Use the following code to connect to DB2 and lock the table:

Dim wsRepository as DAO.Workspace
Dim conDb2 as DAO.Connection
dim recDb2 as DAO.Recordset

Set wsRepository = CreateWorkspace("", "", "", dbUseODBC)
set conDb2 = wsRepository.OpenConnection("DB2P", dbDriverNoPrompt, False, "ODBC;UID=DPWGIS00;PWD=INST8BER")

The parms in the OpenConnection method are as follows:
1. A registered ODBC Datasource Name
2. Indicating to ODBC that we will fill in all necessary connection information (password, ect) in the OpenConnection method instead of in the ODBC DSN entry.
3. This indicates you want Read/Write access to the DB2 table.
4. These are the connection parms used by ODBC to establish a connection.  These will be put in place by your local DBA or IT security administrator.

Once you have gotten this far you can specify many different combinations to Lock the data in your particular table right in the "openRecordset()" method.  This is a much better approach.  You can allow other users to view the data without the ability to change it.  For instance you can use the constant "dbDenyRead" in the options parameter of your openRecordset() method.  There is also a "dbDenyWrite".  

For example:  Set recDb2 = conDb2.OpenRecordset(strSql, ,dbDenyRead, dbSqlPassThrough);

You might consider only locking the records you intend to be updating.  It is inefficient to lock a complete DB2 table from others unless you absolutely have to.  Anyway, using the approach above, you can chose to do it anyway you want.

Hope this helps.
JDC0724
Avatar of josevillar

ASKER

Thank you for your answer but I forgot to tell I'm using DAO not ADO.

sConnectString= "ODBC;UID=" + sUser + ";" + "PWD=" + sPassword + ";DSN=" + sDataBase
Set Base = OpenDatabase(sDataBase, dbDriverNoPrompt, False, sConnectString)

Do you know the way now???

Thank you

The way I showed you is also using DAO.  The only difference is that you are using a Database Object instead of a Connection Object.  If you look at the parms you specified in your OpenDatabase call they are almost identical to the ones in the Connection object.  I believe that by using the Database object, which you are, you are are forced to use JET instead of ODBC.  They question now is.... What database are you using??  If you are using Oracle, DB2, MySQL, etc, you should use the approach I have shown you above.  In fact, the only time I use a database object is when I am working with FoxPro files (.dbf) or sometimes with MSAccess.
Another thing to keep in mind that using access you cannot lock at the row level like you can in Oracle and Db2.

Just one more thing, in your last example:
   >>> sConnectString= "ODBC;UID=" + sUser + ";" + "PWD=" + sPassword + ";DSN=" + sDataBase

You should definitely use the concatenation character "&" instead of "+".  It happens to work for you in this scenario but could reek havac had their been any numerical values in your string...  Just a thought.  Let me know what database you are using.

Thanks,
JDC

I have tried your example using Connection objetct instead of Database but when I try to open the recordset I get "3001 invalid argument" error.

I'm using DB2 database

Is it a problem in your Sql string?  Send me the code from creating the Workspace to opening the recordset so I can see it.
This is my code:

sConnectString = "ODBC;UID=" + sUser + ";" + "PWD=" + sPassword + ";DSN=" + sDataBase

Set wsRepository = CreateWorkspace("", "", "", dbUseODBC)

Set conDb2 = wsRepository.OpenConnection(sDataBase, dbDriverNoPrompt, False, sConnectString)

sSQL = "select * from db2admin.maximo"

Set recDb2 = conDb2.OpenRecordset(sSQL, , dbSQLPassThrough, dbDenyRead)



ASKER CERTIFIED SOLUTION
Avatar of jdc0724
jdc0724

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanRollins
Hi josevillar,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept jdc0724's comment(s) as an answer.

josevillar, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange