| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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? |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| "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 |
|
#4
| |||
| |||
| 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. ![]() |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.