Обсуждение: jdbc: getBinaryStream blocks on second call
Hello, I'm using the Postgresql-JDBC interface inside of a Jave Server Page and request a large object. When the page is first called, it works fine. When its called twice for the same database row, the ResultSet.getBinaryStream method blocks infinetely. This is on postgresql 7.0.3 (from RPM, on Linux, glibc). The code is something like: dbc.setAutoCommit(false);Statement st = dbc.createStatement();ResultSet rs = st.executeQuery("SELECT contents FROM file WHEREid=" + id);dbc.commit();if(rs.next()) { Reader r = new InputStreamReader(rs.getBinaryStream(1)); char buf[] =new char[2048]; for(int read = r.read(buf); read != -1; read = r.read(buf)) out.write(buf, 0, read); out.flush(); r.close(); is.close();} (yes, I known that the getBytes method is more convenient but the JspWriter class a JSP page provides can't write byte[], only char[]) -- Ingo Luetkebohle / ingo@blank.pages.de / 95428014 / |PraxisXML Open Source contact; Computational Linguistics |student; Fargonauten.DE sysadmin; Gimp Registry (not-)maintainer;
Within JDBC 1.0, it is required to access the columns in a result set row from left to right and only once. Since the Postgres JDBC driver is not totally 2.0, it's not mandatory, but it is a real good idea. I'd suggest storing the byte array somewhere so you don't have to read it twice. If you have to read it twice, the only way to gaurantee that it works, is to close the Statement and do it again. Greg Speegle Baylor University Ingo Luetkebohle wrote: > Hello, > > I'm using the Postgresql-JDBC interface inside of a Jave Server Page > and request a large object. When the page is first called, it works > fine. When its called twice for the same database row, the > ResultSet.getBinaryStream method blocks infinetely. This is on > postgresql 7.0.3 (from RPM, on Linux, glibc). > > The code is something like: > > dbc.setAutoCommit(false); > Statement st = dbc.createStatement(); > ResultSet rs = st.executeQuery("SELECT contents FROM file WHERE id=" + id); > dbc.commit(); > if(rs.next()) { > Reader r = new InputStreamReader(rs.getBinaryStream(1)); > char buf[] = new char[2048]; > for(int read = r.read(buf); read != -1; read = r.read(buf)) > out.write(buf, 0, read); > > out.flush(); > r.close(); > is.close(); > } > > (yes, I known that the getBytes method is more convenient but the > JspWriter class a JSP page provides can't write byte[], only char[]) >
On Sun, Dec 03, 2000 at 01:10:21PM -0600, Greg Speegle wrote: > If you have to read it twice, the only way to gaurantee that it > works, is to close the Statement and do it again. Thanks for the tip, it led me in the right direction. Closing the Statement didn't suffice, but closing the whole DB connection worked. Not beautiful, but ok for the moment. -- Ingo Luetkebohle / ingo@blank.pages.de / 95428014 / |PraxisXML Open Source contact; Computational Linguistics |student; Fargonauten.DE sysadmin; Gimp Registry (not-)maintainer;
What is "is" here, ie: the is.close() line? -- Peter Mount Enterprise Support Officer, Maidstone Borough Council Email: petermount@maidstone.gov.uk WWW: http://www.maidstone.gov.uk All views expressed within this email are not the views of Maidstone Borough Council > -----Original Message----- > From: Ingo Luetkebohle [mailto:ingo@blank.pages.de] > Sent: Sunday, December 03, 2000 4:13 PM > To: pgsql-interfaces@postgresql.org > Subject: [INTERFACES] jdbc: getBinaryStream blocks on second call > > > Hello, > > I'm using the Postgresql-JDBC interface inside of a Jave Server Page > and request a large object. When the page is first called, it works > fine. When its called twice for the same database row, the > ResultSet.getBinaryStream method blocks infinetely. This is on > postgresql 7.0.3 (from RPM, on Linux, glibc). > > The code is something like: > > dbc.setAutoCommit(false); > Statement st = dbc.createStatement(); > ResultSet rs = st.executeQuery("SELECT contents FROM > file WHERE id=" + id); > dbc.commit(); > if(rs.next()) { > Reader r = new InputStreamReader(rs.getBinaryStream(1)); > char buf[] = new char[2048]; > for(int read = r.read(buf); read != -1; read = > r.read(buf)) > out.write(buf, 0, read); > > out.flush(); > r.close(); > is.close(); > } > > (yes, I known that the getBytes method is more convenient but the > JspWriter class a JSP page provides can't write byte[], only char[]) > > -- > Ingo Luetkebohle / ingo@blank.pages.de / 95428014 > / > |PraxisXML Open Source contact; Computational Linguistics > |student; Fargonauten.DE sysadmin; Gimp Registry (not-)maintainer; >
Hmm, I'm a tad worried now, as that's not how the driver should be behaving. You shouldn't need to close the Connection just to read a Stream... Peter -- Peter Mount Enterprise Support Officer, Maidstone Borough Council Email: petermount@maidstone.gov.uk WWW: http://www.maidstone.gov.uk All views expressed within this email are not the views of Maidstone Borough Council > -----Original Message----- > From: Ingo Luetkebohle [mailto:ingo@blank.pages.de] > Sent: Sunday, December 03, 2000 9:33 PM > To: Greg Speegle > Cc: pgsql-interfaces@postgresql.org > Subject: Re: [INTERFACES] jdbc: getBinaryStream blocks on second call > > > On Sun, Dec 03, 2000 at 01:10:21PM -0600, Greg Speegle wrote: > > If you have to read it twice, the only way to gaurantee that it > > works, is to close the Statement and do it again. > > Thanks for the tip, it led me in the right direction. Closing the > Statement didn't suffice, but closing the whole DB connection > worked. Not beautiful, but ok for the moment. > > -- > Ingo Luetkebohle / ingo@blank.pages.de / 95428014 > / > |PraxisXML Open Source contact; Computational Linguistics > |student; Fargonauten.DE sysadmin; Gimp Registry (not-)maintainer; >
Yes the spec does say that (and also not to read a column more than once per row). However, internally because we store the resultset in a two dimensional vector, this isn't a problem. I'd like to know more about this problem, just incase it's a strange bug caused by how it's being used in this instance... Peter -- Peter Mount Enterprise Support Officer, Maidstone Borough Council Email: petermount@maidstone.gov.uk WWW: http://www.maidstone.gov.uk All views expressed within this email are not the views of Maidstone Borough Council > -----Original Message----- > From: Greg Speegle [mailto:Greg@10happythings.com] > Sent: Sunday, December 03, 2000 7:10 PM > To: Ingo Luetkebohle > Cc: pgsql-interfaces@postgresql.org > Subject: Re: [INTERFACES] jdbc: getBinaryStream blocks on second call > > > > Within JDBC 1.0, it is required to access the columns in a result set > row from left to right > and only once. Since the Postgres JDBC driver is not totally > 2.0, it's > not mandatory, but it > is a real good idea. I'd suggest storing the byte array > somewhere so you > don't have to > read it twice. If you have to read it twice, the only way to > gaurantee > that it works, is to > close the Statement and do it again. > > Greg Speegle > Baylor University > > Ingo Luetkebohle wrote: > > > Hello, > > > > I'm using the Postgresql-JDBC interface inside of a Jave Server Page > > and request a large object. When the page is first called, it works > > fine. When its called twice for the same database row, the > > ResultSet.getBinaryStream method blocks infinetely. This is on > > postgresql 7.0.3 (from RPM, on Linux, glibc). > > > > The code is something like: > > > > dbc.setAutoCommit(false); > > Statement st = dbc.createStatement(); > > ResultSet rs = st.executeQuery("SELECT contents FROM > file WHERE id=" + id); > > dbc.commit(); > > if(rs.next()) { > > Reader r = new InputStreamReader(rs.getBinaryStream(1)); > > char buf[] = new char[2048]; > > for(int read = r.read(buf); read != -1; read = > r.read(buf)) > > out.write(buf, 0, read); > > > > out.flush(); > > r.close(); > > is.close(); > > } > > > > (yes, I known that the getBytes method is more convenient but the > > JspWriter class a JSP page provides can't write byte[], only char[]) > > >
On Tue, Dec 05, 2000 at 02:40:55PM -0000, Peter Mount wrote: > What is "is" here, ie: the is.close() line? InputStream object, left over from some edits. I originally assigned the return value of getBinaryStream to it. I'll see if I can reproduce the problem outside of a Java Server Page so that it can be debugged easier. If you have further hints on how I could provide you with more information to track this down, they'd be much appreciated! -- Ingo Luetkebohle / ingo@blank.pages.de / 95428014 / |PraxisXML Open Source contact; Computational Linguistics |student; Fargonauten.DE sysadmin; Gimp Registry (not-)maintainer;