>> Use it carefully - in case of multiple inserts

>> you could get wrong @@IDENTITY value.

 

Good catch! I can’t risk getting the wrong identity when multiple inserts are happening.

 

In that case, I will be using your version:

 

<selectKey resultClass="int" type="post" property="Id">

  SELECT cast(IDENT_CURRENT('dbo.Person') as int) AS Id

</selectKey>

 

 

Instead of this:

 

<selectKey property="Id" resultClass="int">

  SELECT cast(@@IDENTITY as int) as value

</selectKey>

 

 

Do you recommend using SQL Server’s auto-increment feature?

 

 

 


Gary P. Russo
Wolters Kluwer - Corporate Legal Services
111 Eighth Avenue
New York, NY 10011

Blog:     http://spaces.msn.com/members/pocketxp/

MSN IM: [EMAIL PROTECTED]

email:   [EMAIL PROTECTED]
url:       http://www.ctadvantage.com

 

 

-----Original Message-----
From: oleksa borodie [mailto:[EMAIL PROTECTED]
Sent: Tuesday, October 25, 2005 11:00 AM
To: [email protected]
Subject: Re: auto-key generation example

 

> However, I decided to use Pablo's code because there is no hard coded

> table name.

 

I don't like it - but it is placed near main insert statement where

table name is hardcoded too.

 

> <selectKey property="IdUsuario" resultClass="int">

>  SELECT cast(@@IDENTITY as int) as value

> </selectKey>

 

Use it with carefull - in case of multipile inserts you could get

wrong @@IDENTITY value.

 

E.g.

 1. application1 inserts record to the Table1 (using iBATIS)

 2. application2 inserts record to the Table2 (using anything to

access database)

 3. selectKey performed for the app1 insert ATTENTION!

 4. selectKeyPerformed for the app2 insert

 

If thing goes as in the example above you'll get wrong ID for

inserted object in the app1.

 

Good luck.

Reply via email to