JackAss
JackAss

Reputation: 338

ResultSet working

I am keen in knowing the working of ResultSet in depth. I have got many doubts regarding ResultSet. such as performance wise which one is better.

 while(rs.next())
{
  //  iterate as well as do call some other functions.
}
  or 
while(rs.next())
{
 // iterate and store the column values in a map
}
// do functions using the map.

since the table is accessed by many other concurrent programs.

Upvotes: 1

Views: 335

Answers (2)

Sankar
Sankar

Reputation: 172

The best thing to do is, The standard that generally follows is to store all the values into an object and then return that object. This is nothing but a Data Transfer Object (DTO Design pattern).

In the first case, you are doing like

  1. Establishing a connection to the database.
  2. Executing some SQL statement and then by getting resultset.
  3. And iterating of the ResultSet, In this step: you are going to call some other functions and we don't know how much time it will take to complete.

    The main disadvantage is that holding down the connection object is not good for long time, because it may be required by some other user, and of course it is a costliest one.

Upvotes: 1

Robert
Robert

Reputation: 2471

Its going to depend on a lot of factors.

  • How processing intensive is your 'other functions'. If the processing is minimal then it won't really matter but it there is a lot of CPU use involved then delaying the processing until the resultset is closed should improve transactional performance.
  • How big is the result set? If its fairly small then reading it into a Map is fine, but if its large and your processing is going to process it into some smaller form, then you may be better off doing the processing inside the loop.

Take a look at the CPU and Memory profiling in the free VisualVM, or use some basic timings to get a feel for what's going on.

Upvotes: 1

Related Questions