473,387 Members | 1,553 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,387 software developers and data experts.

Function on IBM web page not thread-safe?

On the following page:

http://www-128.ibm.com/developerwork...ang/index.html

IBM discusses a surrogate key generation function, along with a
listing in Java (Appendix A at the end of the web page). However it
seems the function is not thread-safe!

Suppose for a certain Table X, the SURROGATEKEYVALUE is currently set
to 100, and INCREMENT is set to 1. If 2 threads, A and B, enter
getSurrogateKey at nearly the same time It is possible for the
following sequence to occur:

1. Thread A executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 101.

2. Thread B executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 102.

3. Thread A executes:
ResultSet rs = getKeyStmt.executeQuery();

This will return 102.

4. Thread B executes:
ResultSet rs = getKeyStmt.executeQuery();

This will also return 102 !

Am I missing something here?

Feb 13 '07 #1
4 1579
Larry wrote:
On the following page:

http://www-128.ibm.com/developerwork...ang/index.html
>
IBM discusses a surrogate key generation function, along with a
listing in Java (Appendix A at the end of the web page). However it
seems the function is not thread-safe!

Suppose for a certain Table X, the SURROGATEKEYVALUE is currently set
to 100, and INCREMENT is set to 1. If 2 threads, A and B, enter
getSurrogateKey at nearly the same time It is possible for the
following sequence to occur:

1. Thread A executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 101.

2. Thread B executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 102.

3. Thread A executes:
ResultSet rs = getKeyStmt.executeQuery();

This will return 102.

4. Thread B executes:
ResultSet rs = getKeyStmt.executeQuery();

This will also return 102 !

Am I missing something here?
Yes, fortunately you do. ;-)

The "updateKeyStmt" is an UPDATE statement which will result in an X-lock to
be set on the KEY table for a specific row. Thus, thread B must wait at
step 2 until thread A has issued a COMMIT or ROLLBACK statement and
released that lock.

If you really want to use the KeyManager class (and not sequences, which are
better performing), then you should consider the following as well:

(1) DB2 uses its own class loader in the JVM tat executes the UDF. This
class loader ensures that objects created for one UDF call are completely
independent of objects created for another invocation. In fact, objects
from different invocations don't know anything about each other.
Therefore, the use of the singleton doesn't have any benefits. Just call
the "connection.prepareStatement" methods directly from "getSurrogateKey".
(But stick with the parameter markers for basic security reasons.)

(2) Take care of NULLs returned from the UDF.

(3) I would make the schema name for each table a separate parameter of the
UDF. That would be a cleaner interface.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Feb 14 '07 #2
Knut Stolze wrote:
(1) DB2 uses its own class loader in the JVM tat executes the UDF. This
class loader ensures that objects created for one UDF call are completely
independent of objects created for another invocation. In fact, objects
from different invocations don't know anything about each other.
Therefore, the use of the singleton doesn't have any benefits. Just call
the "connection.prepareStatement" methods directly from "getSurrogateKey".
(But stick with the parameter markers for basic security reasons.)

(2) Take care of NULLs returned from the UDF.

(3) I would make the schema name for each table a separate parameter of
the
UDF. That would be a cleaner interface.
Actually, these 3 points are only relevant if you want to implement a key
manager as a user-defined function running inside the DB2 database. The
article talks about a client-side/application-specific management.

With UDFs, things are a bit more complicated. You have to consider
(1) Invoking the UDF from different SQL sessions (connections)
(2) Calling the UDF in different SQL statements executed on the same
connection.
(3) Calling the UDF in the same SQL statement without intra-parallelism.
(4) Calling the UDF in the same SQL statement with intra-parallelism.

Option (2) and option (4) require some more thought because neither locking
nor the inherent sequential execution w/o parallelism have to be
considered. In such cases, it may be a good idea to put the UPDATE and the
SELECT into a single statement:

SELECT ...
FROM NEW TABLE ( UPDATE ... ) AS t

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Feb 14 '07 #3
On Feb 14, 1:49 am, Knut Stolze <sto...@de.ibm.comwrote:
Larry wrote:
On the following page:

http://www-128.ibm.com/developerwork...rticle/dm-0407...


IBM discusses a surrogate key generation function, along with a
listing in Java (Appendix A at the end of the web page). However it
seems the function is not thread-safe!
Suppose for a certain Table X, the SURROGATEKEYVALUE is currently set
to 100, and INCREMENT is set to 1. If 2 threads, A and B, enter
getSurrogateKey at nearly the same time It is possible for the
following sequence to occur:
1. Thread A executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 101.
2. Thread B executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 102.
3. Thread A executes:
ResultSet rs = getKeyStmt.executeQuery();
This will return 102.
4. Thread B executes:
ResultSet rs = getKeyStmt.executeQuery();
This will also return 102 !
Am I missing something here?

Yes, fortunately you do. ;-)

The "updateKeyStmt" is an UPDATE statement which will result in an X-lock to
be set on the KEY table for a specific row. Thus, thread B must wait at
step 2 until thread A has issued a COMMIT or ROLLBACK statement and
released that lock.
I don't see any COMMIT or ROLLBACK being issued in the code. And
furthermore, the same connection is being left open for the entire
lifetime of the application. So this code seems to assume that
AUTOCOMMIT is ON. So your statement that an X-lock is placed on the
row does not seem to be correct.

Feb 18 '07 #4
On Feb 14, 1:49 am, Knut Stolze <sto...@de.ibm.comwrote:
Larry wrote:
On the following page:

http://www-128.ibm.com/developerwork...rticle/dm-0407...


IBM discusses a surrogate key generation function, along with a
listing in Java (Appendix A at the end of the web page). However it
seems the function is not thread-safe!
Suppose for a certain Table X, the SURROGATEKEYVALUE is currently set
to 100, and INCREMENT is set to 1. If 2 threads, A and B, enter
getSurrogateKey at nearly the same time It is possible for the
following sequence to occur:
1. Thread A executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 101.
2. Thread B executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 102.
3. Thread A executes:
ResultSet rs = getKeyStmt.executeQuery();
This will return 102.
4. Thread B executes:
ResultSet rs = getKeyStmt.executeQuery();
This will also return 102 !
Am I missing something here?

Yes, fortunately you do. ;-)

The "updateKeyStmt" is an UPDATE statement which will result in an X-lock to
be set on the KEY table for a specific row. Thus, thread B must wait at
step 2 until thread A has issued a COMMIT or ROLLBACK statement and
released that lock.

(snip)
--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
I don't see any COMMIT or ROLLBACK being issued in the code. And
furthermore, the same connection is being left open for the entire
lifetime of the application. So this code seems to assume that
AUTOCOMMIT is ON. So your statement that an X-lock is placed on the
row does not seem to be correct.

Feb 18 '07 #5

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

Similar topics

2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: Johann Blake | last post by:
The following is a bug I have discovered using tab pages and threads and I am looking for a workaround. Create a new Windows Forms application and add a tab control with two tab pages. Add a...
7
by: Chris | last post by:
Hi, I am writing a webpage in C#. Visual Studio .NET. the following code does not provide me with intellisence on the context, session, and page.cache. When I put the large if...then...
4
by: Darrell Sparti, MCSD | last post by:
There have been many postings about this subject on this newsgroup. Unfortunately, they're incorrect. You can't just cast a value in C# and have it work for all ASCII characters. Nor can you use...
4
by: Ekkehard Preis | last post by:
Hi, I'm getting some strange error on one of my pages: The page is composed of some nested ascx controls, the error I am getting is 'undefined is null or not an object'. The error occurs in...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
7
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function Body_Onload() ' create the object Set obj = Server.CreateObject("UploadImage.cTest") ' use method of the object...
16
by: deepres | last post by:
Hi, I have the following problem. In my application I'm dynamically replacing some of web-page content. For example, I have something like that: <span id="ui_tr_22_1_treeJ_span" > <div...
7
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I am using this code to get groups for a user and getting a error (5) on the GetAuthorizationGroups() function . There are two domains. This function works on the local domain but does not work...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.