473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Double lock on iseries db2 ado.net transaction


I'm seeing some odd locking behavior when using an DB2400 database and
running an ado.net transaction. My code -- in simplified form, appears
at the bottom.

I want to run several INSERT statements using the same iDB2Connection
and iDB2Command. Then I want to Commit() so that either all the data
goes in, or not.

What happens though is this:

1. After the first INSERT, a lock appears on the FILE
2. After the second INSERT, another lock appears on the FILE.
3. I can then add several more records, and no more locks appears
4. After the Commit(), one lock disappears but not the other.

I have run the code with every available IsolationLevel, including the
default, and the result is the same.

My questions are:

1. Where could this second lock come from?
2. Why does Commit() not remove it?
3. What additional properties or commands do I need to account for to
remove this lock?
4. I have read that there may be a journaling property that sets up an
'autocommit/rollback' in the event of a system crash -- could this be
what's causing it?


using System;
using IBM.Data.DB2.iSeries;
using System.Data;
using System.Diagnostics;

namespace simpleTtest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here

string connStr= "Data Source=192.168.NNN.NNN;User
Id=XXXXXX;Password=XXXXX;Default Collection=DataLibrary;";
iDB2Connection p_iconn =
new iDB2Connection(connStr);
iDB2Transaction itrans=null;
iDB2Command icmd=new iDB2Command("INSERT INTO " +
"$MANINV.PEM610WD"+
"(" +
"W1BATCH)" +
"VALUES('100')", p_iconn);
p_iconn.Open();
itrans = p_iconn.BeginTransaction(
IsolationLevel.RepeatableRead
);
icmd.Transaction=itrans;

try
{
icmd.ExecuteNonQuery();
//after this statement a lock is generated

icmd.ExecuteNonQuery();
//after this statement a second lock appears

icmd.ExecuteNonQuery();
//no more locks; there are still two(2) at this point

itrans.Commit();
// after commit, one lock is removed -- one remains! why...?
}
catch(Exception e)
{
Debug.WriteLine(e.ToString());
itrans.Rollback();
}
finally
{
p_iconn.Close();
}
}
}
}

--
Texeme Construct
http://texeme.com
Jul 21 '05 #1
7 5577
John Bailo wrote:

I'm seeing some odd locking behavior when using an DB2400 database and
running an ado.net transaction. My code -- in simplified form, appears
at the bottom.

I want to run several INSERT statements using the same iDB2Connection
and iDB2Command. Then I want to Commit() so that either all the data
goes in, or not.

What happens though is this:

1. After the first INSERT, a lock appears on the FILE
2. After the second INSERT, another lock appears on the FILE.
3. I can then add several more records, and no more locks appears
4. After the Commit(), one lock disappears but not the other.

I have run the code with every available IsolationLevel, including the
default, and the result is the same.

My questions are:

1. Where could this second lock come from?
2. Why does Commit() not remove it?
3. What additional properties or commands do I need to account for to
remove this lock?
4. I have read that there may be a journaling property that sets up an
'autocommit/rollback' in the event of a system crash -- could this be
what's causing it?


Just for testing, what if you create a second IDB2Command object, with
the same query and use that as the second insert. Does it create a
second lock then as well?

The behavior is pretty weird, you should get just 1 lock.

FB
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jul 21 '05 #2
Frans Bouma [C# MVP] wrote:
Just for testing, what if you create a second IDB2Command object,
with the same query and use that as the second insert. Does it create a
second lock then as well?


I tried that this morning -- but the behavior is exactly the same!

I also tested removing the .Commit() and just closing the connection.

That still only removes one lock, but the second persists until my
program ends.

Is there anything in Visual Studio that would show me all the 'live'
connections between my app and the database?
Jul 21 '05 #3

Ok, I found a fix for this but I don't like it.

When I set

Pooling=false

in the DB2400 connection string, then it released the second lock after
closing the connection...which I kind of what I would expect, since I
might want to have multiple transactions on the same connection.

Bottom line, our network latency is very high...and I think that having
pooling was creating orphan threads or something that couldn't be closed
by the iDB2Connection.Close() method.

If anyone has other theories, or any kinder, gentler way of managing the
connection pool....

John Bailo wrote:
Frans Bouma [C# MVP] wrote:
Just for testing, what if you create a second IDB2Command object,
with the same query and use that as the second insert. Does it create
a second lock then as well?

I tried that this morning -- but the behavior is exactly the same!

I also tested removing the .Commit() and just closing the connection.

That still only removes one lock, but the second persists until my
program ends.

Is there anything in Visual Studio that would show me all the 'live'
connections between my app and the database?

Jul 21 '05 #4
John Bailo wrote:
Frans Bouma [C# MVP] wrote:
Just for testing, what if you create a second IDB2Command object,
with the same query and use that as the second insert. Does it create
a second lock then as well?

I tried that this morning -- but the behavior is exactly the same!


that's really strange. I then think of a setup issue in the as/400 but
I'm a complete newbie on that, so I can't help you with that. The
ADO.NET code you wrote should simply work.
I also tested removing the .Commit() and just closing the connection.
That still only removes one lock, but the second persists until my
program ends.
Close will rollback transactions still running, and therefore locks
should be lifted, but it's the same as with the commit I think.
Is there anything in Visual Studio that would show me all the 'live'
connections between my app and the database?


No, vs.net's just an editor, not a keeper of connections. The amount of
connections between client and server is typically transparent as
pooling is used by default by most providers (I'm not sure if the
iseries provider enables pooling by default).

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jul 21 '05 #5
May simply be working as designed. I know the iSeries tends to keep
tables open in case they are needed again.

The important thing to check is what _records_ are locked and are the
record locks released when you do the commit?

HTH,
Charles
In article <w9********************@speakeasy.net>, ja*****@texeme.com
says...

I'm seeing some odd locking behavior when using an DB2400 database and
running an ado.net transaction. My code -- in simplified form, appears
at the bottom.

I want to run several INSERT statements using the same iDB2Connection
and iDB2Command. Then I want to Commit() so that either all the data
goes in, or not.

What happens though is this:

1. After the first INSERT, a lock appears on the FILE
2. After the second INSERT, another lock appears on the FILE.
3. I can then add several more records, and no more locks appears
4. After the Commit(), one lock disappears but not the other.

I have run the code with every available IsolationLevel, including the
default, and the result is the same.

My questions are:

1. Where could this second lock come from?
2. Why does Commit() not remove it?
3. What additional properties or commands do I need to account for to
remove this lock?
4. I have read that there may be a journaling property that sets up an
'autocommit/rollback' in the event of a system crash -- could this be
what's causing it?

Jul 21 '05 #6
Hi John,
What happens though is this:
1. After the first INSERT, a lock appears on the FILE
2. After the second INSERT, another lock appears on the FILE.
3. I can then add several more records, and no more locks appears
4. After the Commit(), one lock disappears but not the other.
I'm studying your code but i can't understand this...
icmd.ExecuteNonQuery();
icmd.ExecuteNonQuery();
icmd.ExecuteNonQuery();
itrans.Commit();


So you're running the same command three times ???
Cause i can see no loop or do/while lus anywhere.

Not sure if you gonna get this fixed anyway, but here is an example
of how i cleanup a database using the .net managed driver...

Private Sub cleanDB()
Dim conn As New iDB2Connection
Dim conStr As String = "DataSource = as400;UserId=" +
Environment.UserName
conn.ConnectionString = conStr
Try
conn.Open()
Catch ex As Exception
MessageBox.Show("ERROR: " + ex.Message, "Er is een fout opgetreden")
End Try
Dim acmd As New iDB2Command("SELECT ARNRVL, STLKVL FROM
ODLIBF46L.ARLOCLEEG ORDER BY ARNRVL", conn)
Dim adr As iDB2DataReader = acmd.ExecuteReader
Dim ocmd As New iDB2Command("DELETE FROM ODLIBF46L.ODVL WHERE ARNRVL =
@arnr AND STLKVL = " + _
"@loc AND FMKDVL = 'STF' AND MGNRVL = 'MHZ' AND STOKVL = 0", conn)
Dim Aantal As Integer = 0
Me.Hide()
Me.setText("Bezig met opschonen Database ODLIBF46L.ODVL...")
Me.Show()
'Application.DoEvents()
While adr.Read()
ocmd.DeriveParameters()
ocmd.Parameters("@arnr").Value = adr.GetValue(0)
ocmd.Parameters("@loc").Value = adr.GetValue(1)
ocmd.ExecuteNonQuery()
Aantal += 1
End While
Me.Hide()
MessageBox.Show(Aantal.ToString + " records doorlopen", "Opschoning
Voltooid")
Try
adr.Close()
conn.Close()
Catch ex As Exception
MessageBox.Show("ERROR: " + ex.Message, "Er is een fout opgetreden
!")
End Try
System.Environment.Exit(0)
End Sub

Okay, it is vb instead of C# but i think you can easily read the code.

Best Regards,

Patrick Bielen
MCP / SCJP

Jul 21 '05 #7

Turns out the problem seemed to be related to connection pooling.

I had to step down the pooling to using only a single connection.

With multiple connections, it seemed to be leaving a connection open,
which held the lock so the second part of the transaction could not
complete.

Patrick Bielen wrote:
Hi John,

What happens though is this:
1. After the first INSERT, a lock appears on the FILE
2. After the second INSERT, another lock appears on the FILE.
3. I can then add several more records, and no more locks appears
4. After the Commit(), one lock disappears but not the other.

I'm studying your code but i can't understand this...

icmd.ExecuteNonQuery();
icmd.ExecuteNonQuery();
icmd.ExecuteNonQuery();
itrans.Commit();

So you're running the same command three times ???
Cause i can see no loop or do/while lus anywhere.

Not sure if you gonna get this fixed anyway, but here is an example
of how i cleanup a database using the .net managed driver...

Private Sub cleanDB()
Dim conn As New iDB2Connection
Dim conStr As String = "DataSource = as400;UserId=" +
Environment.UserName
conn.ConnectionString = conStr
Try
conn.Open()
Catch ex As Exception
MessageBox.Show("ERROR: " + ex.Message, "Er is een fout opgetreden")
End Try
Dim acmd As New iDB2Command("SELECT ARNRVL, STLKVL FROM
ODLIBF46L.ARLOCLEEG ORDER BY ARNRVL", conn)
Dim adr As iDB2DataReader = acmd.ExecuteReader
Dim ocmd As New iDB2Command("DELETE FROM ODLIBF46L.ODVL WHERE ARNRVL =
@arnr AND STLKVL = " + _
"@loc AND FMKDVL = 'STF' AND MGNRVL = 'MHZ' AND STOKVL = 0", conn)
Dim Aantal As Integer = 0
Me.Hide()
Me.setText("Bezig met opschonen Database ODLIBF46L.ODVL...")
Me.Show()
'Application.DoEvents()
While adr.Read()
ocmd.DeriveParameters()
ocmd.Parameters("@arnr").Value = adr.GetValue(0)
ocmd.Parameters("@loc").Value = adr.GetValue(1)
ocmd.ExecuteNonQuery()
Aantal += 1
End While
Me.Hide()
MessageBox.Show(Aantal.ToString + " records doorlopen", "Opschoning
Voltooid")
Try
adr.Close()
conn.Close()
Catch ex As Exception
MessageBox.Show("ERROR: " + ex.Message, "Er is een fout opgetreden
!")
End Try
System.Environment.Exit(0)
End Sub

Okay, it is vb instead of C# but i think you can easily read the code.

Best Regards,

Patrick Bielen
MCP / SCJP

Jul 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Anita | last post by:
Hi All, I have a question about lock hint for you : If the first user currently run a select command with share lock and hold it. What kind of lock (lock hint) should be used by the second...
4
by: Christoph Zeltner | last post by:
hi, i am writing a thesis on the concurrency control in db2 udb and oracle. I found in the documentary of db2, that a resource holding a (U)pdate-Lock can still be requested by a read-lock...
0
by: John Bailo | last post by:
I'm seeing some odd locking behavior when using an DB2400 database and running an ado.net transaction. My code -- in simplified form, appears at the bottom. I want to run several INSERT...
8
by: John Bailo | last post by:
I'm seeing some odd locking behavior when using an DB2400 database and running an ado.net transaction. My code -- in simplified form, appears at the bottom. I want to run several INSERT...
5
by: Uwe C. Schroeder | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, maybe my mind is stuck, but here's something strange. This is the classic "counter" thing, where you can't / won't use sequences....
8
by: Komandur Kannan | last post by:
We have a smart device application running on handhelds(Symbol MC9000G). The backend is Oracle and a middle tier web services development done in Vb.net. We use pessimistic Locking due to...
0
by: ftapia | last post by:
hello friends. I´m working with DB2 on Iseries and i need lock a row for generate a sequence number. the first user must generate id = 1 , the sencond must generate id = 2 and so on. I use ...
2
by: simonZ | last post by:
I create a transaction: sqlTran=sqlConn.BeginTransaction(IsolationLevel.Serializable); Then, I insert some data into report table with sqlCommand object: oCmd = new...
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.