Close resultset but not the connection

This is a discussion on Close resultset but not the connection within the JDBC JAVA forums in Framework and Interface Programming category; I have a method that has a resultset as return value, so i can not close the connection: public ResultSet getSomething() { StringBuffer sql = new StringBuffer(); Statement stmt = null; try { con = SQLTestFactory.createConnection(); stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSIT IVE, ResultSet.CONCUR_READ_ONLY); sql.append("SELECT Someone from Something "); rs = stmt.executeQuery(sql.toString()); return rs; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } } After i collect the rs i close it, but how do i close the connection?...

Go Back   Application Development Forum > Framework and Interface Programming > JDBC JAVA

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-30-2008, 02:28 PM
polilop
Guest
 
Default Close resultset but not the connection

I have a method that has a resultset as return value, so i can not close the
connection:

public ResultSet getSomething()
{

StringBuffer sql = new StringBuffer();
Statement stmt = null;

try {
con = SQLTestFactory.createConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSIT IVE,
ResultSet.CONCUR_READ_ONLY);
sql.append("SELECT Someone from Something ");
rs = stmt.executeQuery(sql.toString());
return rs;

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return rs;
}
}


After i collect the rs i close it, but how do i close the connection?




Reply With Quote
  #2  
Old 09-30-2008, 03:07 PM
joeNOSPAM@BEA.com
Guest
 
Default Re: Close resultset but not the connection

On Sep 30, 11:28*am, "polilop" <fmatosicREM...@inet.hr> wrote:
> I have a method that has a resultset as return value, so i can not close the
> connection:
>
> public ResultSet getSomething()
> *{
>
> * StringBuffer sql = new StringBuffer();
> * Statement stmt = null;
>
> * try {
> * *con = SQLTestFactory.createConnection();
> * *stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSIT IVE,
> * * *ResultSet.CONCUR_READ_ONLY);
> * *sql.append("SELECT Someone from Something ");
> * *rs = stmt.executeQuery(sql.toString());
> * *return rs;
>
> * } catch (SQLException e) {
> * *// TODO Auto-generated catch block
> * *e.printStackTrace();
> * }
>
> * return rs;
> *}
>
> }
>
> After i collect the rs i close it, but how do i close the connection?


The code isn't so good, that you obtain a connection and don't
retain/manage it. That being said, you can do this in the code
that is closing the result set, instead of closing the result set:

rs.getStatement().getConnection().close();

By spec, closing the connection will close the statement which
will close the result set.

Joe Weinstein
Reply With Quote
  #3  
Old 09-30-2008, 07:14 PM
Lew
Guest
 
Default Re: Close resultset but not the connection

"polilop" wrote:
>> After i [sic] collect the rs i [sic] close it, but how do i [sic] close the connection?


joeNOSPAM@BEA.com wrote:
> The code isn't so good, that you obtain a connection and don't
> retain/manage it. That being said, you can do this in the code
> that is closing the result set, instead of closing the result set:
>
> rs.getStatement().getConnection().close();
>
> By spec, closing the connection will close the statement which
> will close the result set.


Alternatively, use a disconnected RowSet. Then you can close the connection
in the normal place, in the context of the code that opened it, and not worry.

--
Lew
Reply With Quote
  #4  
Old 10-01-2008, 11:05 AM
joeNOSPAM@BEA.com
Guest
 
Default Re: Close resultset but not the connection

On Sep 30, 4:14*pm, Lew <no...@lewscanon.com> wrote:
> "polilop" wrote:
> >> After i [sic] collect the rs i [sic] close it, but how do i [sic] close the connection?

> joeNOS...@BEA.com wrote:
> > The code isn't so good, that you obtain a connection and don't
> > retain/manage it. That being said, you can do this in the code
> > that is closing the result set, instead of closing the result set:

>
> > rs.getStatement().getConnection().close();

>
> > By spec, closing the connection will close the statement which
> > will close the result set.

>
> Alternatively, use a disconnected RowSet. *Then you can close the connection
> in the normal place, in the context of the code that opened it, and not worry.
>
> --
> Lew


What Lew said.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:46 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.