Lets say I have a large number of threads inserting into a mysql database using Java.sql. After i perform an insert i would like to know the primary key of the record i just inserted. I could use getRow() on the ResultSet returned by the insert query. However, is this thread safe? Or should I fire off another select statement to find the primary key of the record I just inserted?
Lets say I have a large number of threads inserting into a mysql database
Share
ResultSet does not advertise on the javadoc as being thread safe, so assume it is NOT. If you have 10 threads inserting, and somehow they get the SAME result set.. something weird will happen (or at least not guarantee to be right).
However, if you have 10 threads each doing their OWN inserting, and each one has it’s own ResultSet .. you are fine. Remember, thread safety often has to do with sharing of the same OBJECT, not just the same class. ArrayList is NOT thread safe, but if you have 10 different threads with 10 different ArrayList’s, you are fine. Same here with ResultSet.