views:

1064

answers:

5

Hi,

I had an issue in building the resultset using java. Here it goes...

I am storing a collection object which is organized as row wise taken from a resultset object and putting the collection object(which is stored as vector/array list) in cache and trying to retrieve the same collection object. Here i need to build back the resultset again using the collection object. Now my doubt is building the resultset in this way possible or not? Please let me know asap.

Thanks in advance,

Bhaskar

A: 

From what I could get, your code may be like this:

List collection = new ArrayList();
collection.add(" A collection in some order");

List cache = new ArrayList();
cache.add(collection); ...

Now when you retrieve I think you'll get your collection in order, since you have used List.

If this is not what you were expecting, do comment.

Techmaddy
Yes, I am able to get the collection back from the cache. After this I need to build the resultset again. Is that possible?
Bhaskara Krishna Mohan Potam
I think you have in the collection that you are getting back. COde snippet may help.
Techmaddy
Basically, I need to build a resultset object with all the functions that a resultset can do and the data should be that retrieved from the collection object. But is creating a resultset object possible (taking for granted that the db connection still exists)?
Bhaskara Krishna Mohan Potam
+2  A: 

The best idea if you are using a collection in place of a cache is to use a CachedRowSet instead of a ResultSet. CachedRowSet is a Subinterface of ResultSet, but the data is already cached. This is far simpler than to write all the data into an ArrayList.
CachedRowSets can also be queried themselves.

CachedRowSet rs;
.......................
.......................
Integer id;
String name;

while (rs.next())
{      
     if (rs.getInt("id") == 13)
     {
          id   = rs.getInt("id");
          name = rs.getString("name")); 
     }     
}

So you just call the CachedRowSet whenever you need the info. It's almost as good as sliced bread. :)

EDIT:
There are no set methods for ResultSet, while there are Update methods. The problem with using the Update method's for the purpose of rebuilding a ResultSet is that it requires selecting a Row to update. Once the ResultSet has freed itself, all rows are set to null. A null reference cannot be called. A List of Lists mimics a ResultSet itself, or more correctly, an array of arrays mimic a ResultSet.

While Vectors are thread safe, there is a huge overhead attached to them. Use the ArrayList instead. As each nested List is created and placed into the outer nest List, insert it in this manner.

nest.add(Collections.unmodifiableList(nested));

After all of the nested Lists are inserted, return the nest List as an umodifiableList as well. This will give you a thread-safe collection without the overhead of the vectors.

WolfmanDragon
Yes Wolfman, what you said is a good option. But here there is a problem.. when we use cachedRowSetImpl, we are trying to put single object in cache. This will create problem when the size of the query result is huge.
Bhaskara Krishna Mohan Potam
Instead if we use a array list/vector object, we can put this on multiple nodes using clustering right. So is there any api or method using which I can build a resultset after getting the data from cache?
Bhaskara Krishna Mohan Potam
I don't think I understand why you would want to rebuild a ResultSet. If you rebuild the ResultSet you get right back to having this huge object on one node.
WolfmanDragon
The thing about the "huge overhead" of Vector is nonsense; in fact, if you actually need the basic synchronization it provides, it is faster than using Collections.unmodifiableList().
Michael Borgwardt
It is all according to haw many threads are calling it at one time. If many threads start calling it at once, watch your system crawl to a stop with a vector. Using Vectors limit the use of the code. There are uses for it still, but in all it is outdated.
WolfmanDragon
Collections.unmodifiableList() are not synchronized, just thread safe. That is why it is faster for multiple threads. The trade off is that it is unmodifiable. As long as it is read only, as in the case of this question, there is no reason to use vectors.
WolfmanDragon
Oops - I was thinking of Collections.synchronizedList() - though I still doubt very much the performance hit is as big as you make it sound - very few apps actually have a large number of concurrently working threads accessing shared data.
Michael Borgwardt
A: 

I will advise you to use CachedRowSet. Refer http://www.onjava.com/pub/a/onjava/2004/06/23/cachedrowset.html this article to know more about CachedRowSet. Once you create this CachedRowSet, you can disconnect from the database, make some changes to the cached data and letter can even open the DB connection and commit the changes back to the Database.

Shamik
A: 

Another option you should consider is just to refactor your code to accept a Collection instead of a ResultSet.

I'm assuming you pass that ResultSet to a method that iterates over it. You might as well change the method to iterate over an ArrayList...

itsadok
A: 
aldrinleal