Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7535

Re: Unmodifiable ResultSet wrapper?

From Ian Pilcher <arequipeno@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Unmodifiable ResultSet wrapper?
References <XYR6q.1475$Ld7.24@newsfe21.iad> <87879b14-d65c-4f0b-9d7c-0efb203dc922@glegroupsg2000goo.googlegroups.com>
Message-ID <5M58q.4277$sk5.2462@newsfe03.iad> (permalink)
Organization TeraNews.com
Date 2011-09-02 09:30 -0500

Show all headers | View raw


On 08/29/2011 04:08 PM, Lew wrote:
> Ian Pilcher wrote:
>> Is there any standard way to wrap a ResultSet into an unmodifiable
>> wrapper?  I need to pass a ResultSet to a callback as I iterate through
>> it, and the callback has no business altering its state (calling next(),
>> etc.)

For the sake of posterity, I ended up using a dynamic proxy.  The
invocation handler matches the name of the method being called against
a whitelist (a HashSet of method names) and throws an
UnsupportedOperationException if the method name isn't in the whitelist.

The only fly in the ointment is the need to handle getMetaData()
specially and return a ResultSetMetaData proxy which whitelists every
method except unwrap().

> There is no standard way to pass an unmodifiable ResultSet back, perhaps because it's a really bad idea. ResultSets carry all sorts of database connection and Statement state with them, so exposing that through a callback is a layer violation and a flat-out request for disaster.  Don't do it.

... which is why I need the unmodifiable "view" that gives the callback
read-only access to the current row.  (I probably should have stated it
that way in my original post.)

> Also, the _raison d'etre_ for ResultSet is to perform 'next()' and other such calls.  Asking for a ResultSet on which you don't wish to do the fundamental operations is a red flag that you're heading the wrong way down Fubar Path.

The method that's iterating through the ResultSet obviously needs to
call next(), etc.  As I said in my original post, the callback method
"has no business" changing the state of the ResultSet.

> The standard approach is to unwrap the data from the ResultSet into an object within your domain model and pass that back.  The object with the callback almost certainly doesn't want the relational model but a domain model anyway.

In fact, the class with the callback is a subclass of the class with
the "iterator" method, and it will "want" the relational model if I
write it that way.

The "iterator" method is also very general, so there isn't any way to
construct a particularly useful object for it to pass back.  I could use
a Map<String,Object> or an Object[], but that's about it.

> You should consider JPA.  And perhaps take a course in object-oriented design.

Perhaps, but I'll venture that the latter is quite a conclusion to
reach from the information provided.

-- 
========================================================================
Ian Pilcher                                         arequipeno@gmail.com
"If you're going to shift my paradigm ... at least buy me dinner first."
========================================================================

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Unmodifiable ResultSet wrapper? Ian Pilcher <arequipeno@gmail.com> - 2011-08-29 14:42 -0500
  Re: Unmodifiable ResultSet wrapper? Lew <lewbloch@gmail.com> - 2011-08-29 14:08 -0700
    Re: Unmodifiable ResultSet wrapper? Ian Pilcher <arequipeno@gmail.com> - 2011-09-02 09:30 -0500
      Re: Unmodifiable ResultSet wrapper? Lew <lewbloch@gmail.com> - 2011-09-03 00:59 -0700

csiph-web