This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes LAST_INSERT_ID()  with threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "LAST_INSERT_ID()  with threads" Watch "LAST_INSERT_ID()  with threads" New topic
Author

LAST_INSERT_ID() with threads

John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
I need to get the id from a previous insert to use as a foreign ke in another table
I am using the connection pool below in a web application.
Can i use LAST_INSERT_ID() in this configuration whithout the risk of getting the wrong value?

If not what is another way of doing this?

Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19754
    
  10

At the very least, you will want to send the LAST_INSERT_ID query to the same connection which did the INSERT statement. None of the code you posted seems to show an example of what you are doing.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
Below is what i am currently doing but i am using SELECT @@IDENTITY AS IDENTITY
This is giving me a problem so i am looking for an laternative

thanks
john

Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19754
    
  10

In that code I don't see where you do an insert before you do the SELECT @@IDENTITY query. Maybe that's your problem.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
Paul Clapham wrote:In that code I don't see where you do an insert before you do the SELECT @@IDENTITY query. Maybe that's your problem.


OMG you are the man. That was one of my other problems.
I must have overwrote the execute line when I pasted:


It helps to have a second pair of eyes.
This may relate to the second problem also.

Aside from that do you see any other flaws in the logic?
thanks
john
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LAST_INSERT_ID() with threads
 
It's not a secret anymore!