You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Frank <fa...@verizon.net> on 2005/08/25 15:00:12 UTC
Best way to return a resultset?
Hello,
I have this working now that returns the resultset using an ArrayList.
In Tapestry, is this the best way or is there something other than an
ArrayList to use?
Thanks for the help.
Frank
public List doQuery() {
List list = new ArrayList();
try {
Context context = new InitialContext();
DataSource ds = (DataSource)
context.lookup("java:comp/env/jdbc/bankDataSource");
Connection conn = ds.getConnection();
PreparedStatement st = conn.prepareStatement("select * from accounts");
ResultSet rs = st.executeQuery();
if (rs.next()) {
list.add(createNewItem(rs.getString("accname")));
}
conn.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Best way to return a resultset?
Posted by Jesse Kuhnert <jk...@gmail.com>.
Or go check out hivetranse.sourceforge.net
On 8/25/05, Aleksej <al...@ivs.lt> wrote:
> JNDI Lookup is quite expensive and really there is no sense to search
> data source
> for each time. It is enough to find it once and save somewhere for
> later reuse.
> Singleton object is good for such task. For example:
> -----
> public class DataSourceWrapper{
> private static DataSource ds;
> public static getDataSource(){
> if( DataSourceWrapper.ds == null ){
> // well, it is not created yet, so create it here
> }
> return DataSourceWrapper.ds; // at this point data source will be
> allready created and only once
> }
> }
> -----
> In hivemind you could create service that wraps data source and inject
> it directly in your page... Read more
> about it in hivemind docs. ( not a simple task :) )
>
> > I am using Tapestry 4 and 3, I have the 4 version installed in tomcat.
> > When you say that "making lookup each time", is it because the page is
> > refreshed?
> > How would I code this using hivemind?
> >
> > Regards,
> >
> > Frank
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Best way to return a resultset?
Posted by Aleksej <al...@ivs.lt>.
JNDI Lookup is quite expensive and really there is no sense to search
data source
for each time. It is enough to find it once and save somewhere for
later reuse.
Singleton object is good for such task. For example:
-----
public class DataSourceWrapper{
private static DataSource ds;
public static getDataSource(){
if( DataSourceWrapper.ds == null ){
// well, it is not created yet, so create it here
}
return DataSourceWrapper.ds; // at this point data source will be
allready created and only once
}
}
-----
In hivemind you could create service that wraps data source and inject
it directly in your page... Read more
about it in hivemind docs. ( not a simple task :) )
> I am using Tapestry 4 and 3, I have the 4 version installed in tomcat.
> When you say that "making lookup each time", is it because the page is
> refreshed?
> How would I code this using hivemind?
>
> Regards,
>
> Frank
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Best way to return a resultset?
Posted by Frank <fa...@verizon.net>.
I am using Tapestry 4 and 3, I have the 4 version installed in tomcat.
When you say that "making lookup each time", is it because the page
is refreshed?
How would I code this using hivemind?
Regards,
Frank
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Best way to return a resultset?
Posted by Aleksej <al...@ivs.lt>.
First of all, you are making lookup each time and it is not very good.
Second thing, is that your connection will be closed only if no exception
was thrown. For this, try something like:
---------
Connection conn= null;
try{
conn= getConnection(); // your way to get it
//TODO do your job
}catch( Exception ex ){
log.error( ex );
}finally{
if( conn != null ) try{ conn.close(); }catch( SQLException ex ){}
}
---------
Also, what version of Tapestry you are using? For T4 there is much more ways
to work with database ( thru Hivemind mostly ).
Frank wrote:
> Hello,
>
> I have this working now that returns the resultset using an ArrayList.
> In Tapestry, is this the best way or is there something other than an
> ArrayList to use?
>
> Thanks for the help.
>
> Frank
> public List doQuery() {
> List list = new ArrayList();
> try {
> Context context = new InitialContext();
> DataSource ds = (DataSource)
> context.lookup("java:comp/env/jdbc/bankDataSource");
> Connection conn = ds.getConnection();
> PreparedStatement st = conn.prepareStatement("select *
> from accounts");
> ResultSet rs = st.executeQuery();
> if (rs.next()) {
> list.add(createNewItem(rs.getString("accname")));
> }
> conn.close();
> } catch (Exception e) {
> throw new RuntimeException(e);
> }
>
> return list;
> }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org