Query returned by SQL Object is always closed

714 views
Skip to first unread message

Alex Spurling

unread,
Apr 23, 2013, 7:06:33 AM4/23/13
to jd...@googlegroups.com
Hi,

I'm trying to write an SQL Object that returns a Query object so that I can later perform a fold over the result set. Unfortunately, when I write an SQL Object that returns a Query the underlying connection is always closed which means I am unable to use any of the methods such as first, list, map, fold etc.

Here's what I have so far:

public interface FooDAO
{
    @SqlQuery("select foo from bar")
    Query<String> getValues();
}

public class FooController
{
    private final FooDAO fooDAO;

    public FooController(FooDAO fooDAO)
    {
        this.fooDAO = fooDAO;
    }
    private String getFirstFoo()
    {
        return fooDAO.getValues().first();
    }
}

The error I get is:

! org.skife.jdbi.v2.exceptions.UnableToCreateStatementException: java.sql.SQLException: Connection is closed. [statement:"select foo from bar", arguments:{ positional:{}, named:{}, finder:[]}]
! at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1280) ~[jdbi-2.43.jar:na]
! at org.skife.jdbi.v2.Query.fold(Query.java:172) ~[jdbi-2.43.jar:na]
! at org.skife.jdbi.v2.Query.first(Query.java:267) ~[jdbi-2.43.jar:na]
! at org.skife.jdbi.v2.Query.first(Query.java:259) ~[jdbi-2.43.jar:na]
! at com.myapp.FooController.getFirstFoo(FooController.java:13) ~[classes/:na]
...
! at java.lang.Thread.run(Thread.java:680) [na:1.6.0_37]
Caused by: ! java.sql.SQLException: Connection is closed.
! at org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.checkOpen(PoolingDataSource.java:185) ~[tomcat-dbcp-7.0.37.jar:7.0.37]
! at org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:312) ~[tomcat-dbcp-7.0.37.jar:7.0.37]
! at org.skife.jdbi.v2.DefaultStatementBuilder.create(DefaultStatementBuilder.java:48) ~[jdbi-2.43.jar:na]
! at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1276) ~[jdbi-2.43.jar:na]
!... 54 common frames omitted


Note here I'm just getting the first result from the Query which I could do by having getValues() return a List but this is just a contrived example to demonstrate the problem - in my actual project I need to use the Query fold method. Also note the FooDAO instance is created by calling onDemand(FooDAO.class).

Any ideas what I'm doing wrong?

Alex

Brian McCallister

unread,
Apr 23, 2013, 9:25:07 AM4/23/13
to jd...@googlegroups.com
Has this sql object been created via onDemand(), attach(), or open() ?

-Brian



Alex

--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alex Spurling

unread,
Apr 23, 2013, 9:39:53 AM4/23/13
to jd...@googlegroups.com
Hi Brian,

It's created via onDemand()

Alex

Brian McCallister

unread,
Apr 23, 2013, 12:26:05 PM4/23/13
to jd...@googlegroups.com
Okay, I know what the problem is, the solution is not obvious though. I think the expectation is that a handle will be obtained when it is needed and released when not, but this doesn't actually work well with Query as implemented today.

Fixing this to do "on demand" semantics within the Query itself is very non-trivial (not rocket surgery, just a lot of changes. My honest recommendation, in the mean time, is to return an ResultIterator rather then a Query and fold via external traversal. It is not as functionally-cool, but it works fine and is fewer lines of code :-)

Alex Spurling

unread,
Apr 23, 2013, 12:34:47 PM4/23/13
to jd...@googlegroups.com
Thanks for your prompt responses, Brian. It might be worth updating the documentation on this page http://jdbi.org/sql_object_api_queries/ with this information as there is an example there that returns a Query object which is how I ended up down this path.

Alex

Brian McCallister

unread,
Apr 23, 2013, 1:00:31 PM4/23/13
to jd...@googlegroups.com
Yeah, thanks!
Reply all
Reply to author
Forward
0 new messages