You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Lon Varscsak <lo...@gmail.com> on 2014/10/15 00:54:27 UTC

Access to JDBC Connection

Hey all,

I have some code that executes some raw SQL and historically (with EOF) I
just get the connection that would normally be used for that editing
context.  No problems.  So I'm trying to implement that similarly in
Cayenne, and I'm getting the DataNode's DataSource's connection, and it
works, but then any subsequent calls (at least within the same request) I
get a timeout because it's like the connection is being used (even though
I'm done executing my query) and unavailable to the pool (error:  Can't
obtain connection. Request timed out. Total used connections: 1)

I've been using this code with a JDBC Connection for a very long time, and
I think I'm releasing the resources I create (the Statement).  What could I
be missing here?  Is there a better way to "checkout" and then "release"
the connection?

Thanks,

Lon

Re: Access to JDBC Connection

Posted by Andrus Adamchik <an...@objectstyle.org>.
Yep. You must close it, and not worry about connection pooling in your code. That part is encapsulated, so "close" returns it ot the pool.

Andrus

On Oct 14, 2014, at 8:38 PM, Lon Varscsak <lo...@gmail.com> wrote:
> Okay, that's the issue (not calling close()).  I thought what I was getting
> back was a real JDBC Connection, but it looks like I'm getting back a
> ConnectionWrapper, which when closed, returns the connection to pool.  I
> can do something conditionally with this.
> 
> Thanks,
> 
> Lon
> 
> On Tue, Oct 14, 2014 at 5:12 PM, Lon Varscsak <lo...@gmail.com>
> wrote:
> 
>> That's basically what I'm doing...
>> 
>>     DataSource dataSource = serverRuntime().getDataSource(dataNodeName);
>> 
>> 
>> 
>>    try {
>> 
>>    return dataSource.getConnection();
>> 
>>    } catch (SQLException sqe) {
>> 
>>    throw new RuntimeException(sqe);
>> 
>>    }
>> 
>> No exception is being throw.  My query executes (a simple select) and
>> returns results.  I then immediately do a "normal" Cayenne fetch, but it
>> times out with the error I mentioned.  My code doesn't explicitly close the
>> connection, because historically I wouldn't want to disconnect from the
>> database.
>> 
>> -Lon
>> 
>> On Tue, Oct 14, 2014 at 4:46 PM, Andrus Adamchik <an...@objectstyle.org>
>> wrote:
>> 
>>> Hi Lon,
>>> 
>>> Some code samples showing how you get and release connection would help
>>> to understand your problem better. But... You can actually bypass dealing
>>> with the internals of Cayenne stack and work with DataSource as you would
>>> in a straight JDBC application:
>>> 
>>> ServerRuntime r = ...
>>> DataSource ds = r.getDataSource("mydatanode");
>>> 
>>> try(Connection c = ds.getConnection()) {
>>>   // do your thing here.. and the Java will close the connection
>>>   // on exit from the "try" block
>>> }
>>> 
>>> 
>>> Or you can use SQLTemplate for raw SQL and avoid dealing with connections
>>> all together, which is a preferred way:
>>> 
>>> 
>>> http://cayenne.apache.org/docs/3.1/cayenne-guide/queries.html#sqltemplate
>>> 
>>> Andrus
>>> 
>>> 
>>> On Oct 14, 2014, at 6:54 PM, Lon Varscsak <lo...@gmail.com> wrote:
>>> 
>>>> Hey all,
>>>> 
>>>> I have some code that executes some raw SQL and historically (with EOF)
>>> I
>>>> just get the connection that would normally be used for that editing
>>>> context.  No problems.  So I'm trying to implement that similarly in
>>>> Cayenne, and I'm getting the DataNode's DataSource's connection, and it
>>>> works, but then any subsequent calls (at least within the same request)
>>> I
>>>> get a timeout because it's like the connection is being used (even
>>> though
>>>> I'm done executing my query) and unavailable to the pool (error:  Can't
>>>> obtain connection. Request timed out. Total used connections: 1)
>>>> 
>>>> I've been using this code with a JDBC Connection for a very long time,
>>> and
>>>> I think I'm releasing the resources I create (the Statement).  What
>>> could I
>>>> be missing here?  Is there a better way to "checkout" and then "release"
>>>> the connection?
>>>> 
>>>> Thanks,
>>>> 
>>>> Lon
>>> 
>>> 
>> 


Re: Access to JDBC Connection

Posted by Lon Varscsak <lo...@gmail.com>.
Okay, that's the issue (not calling close()).  I thought what I was getting
back was a real JDBC Connection, but it looks like I'm getting back a
ConnectionWrapper, which when closed, returns the connection to pool.  I
can do something conditionally with this.

Thanks,

Lon

On Tue, Oct 14, 2014 at 5:12 PM, Lon Varscsak <lo...@gmail.com>
wrote:

> That's basically what I'm doing...
>
>      DataSource dataSource = serverRuntime().getDataSource(dataNodeName);
>
>
>
>     try {
>
>     return dataSource.getConnection();
>
>     } catch (SQLException sqe) {
>
>     throw new RuntimeException(sqe);
>
>     }
>
> No exception is being throw.  My query executes (a simple select) and
> returns results.  I then immediately do a "normal" Cayenne fetch, but it
> times out with the error I mentioned.  My code doesn't explicitly close the
> connection, because historically I wouldn't want to disconnect from the
> database.
>
> -Lon
>
> On Tue, Oct 14, 2014 at 4:46 PM, Andrus Adamchik <an...@objectstyle.org>
> wrote:
>
>> Hi Lon,
>>
>> Some code samples showing how you get and release connection would help
>> to understand your problem better. But... You can actually bypass dealing
>> with the internals of Cayenne stack and work with DataSource as you would
>> in a straight JDBC application:
>>
>> ServerRuntime r = ...
>> DataSource ds = r.getDataSource("mydatanode");
>>
>> try(Connection c = ds.getConnection()) {
>>    // do your thing here.. and the Java will close the connection
>>    // on exit from the "try" block
>> }
>>
>>
>> Or you can use SQLTemplate for raw SQL and avoid dealing with connections
>> all together, which is a preferred way:
>>
>>
>> http://cayenne.apache.org/docs/3.1/cayenne-guide/queries.html#sqltemplate
>>
>> Andrus
>>
>>
>> On Oct 14, 2014, at 6:54 PM, Lon Varscsak <lo...@gmail.com> wrote:
>>
>> > Hey all,
>> >
>> > I have some code that executes some raw SQL and historically (with EOF)
>> I
>> > just get the connection that would normally be used for that editing
>> > context.  No problems.  So I'm trying to implement that similarly in
>> > Cayenne, and I'm getting the DataNode's DataSource's connection, and it
>> > works, but then any subsequent calls (at least within the same request)
>> I
>> > get a timeout because it's like the connection is being used (even
>> though
>> > I'm done executing my query) and unavailable to the pool (error:  Can't
>> > obtain connection. Request timed out. Total used connections: 1)
>> >
>> > I've been using this code with a JDBC Connection for a very long time,
>> and
>> > I think I'm releasing the resources I create (the Statement).  What
>> could I
>> > be missing here?  Is there a better way to "checkout" and then "release"
>> > the connection?
>> >
>> > Thanks,
>> >
>> > Lon
>>
>>
>

Re: Access to JDBC Connection

Posted by Lon Varscsak <lo...@gmail.com>.
That's basically what I'm doing...

     DataSource dataSource = serverRuntime().getDataSource(dataNodeName);



    try {

    return dataSource.getConnection();

    } catch (SQLException sqe) {

    throw new RuntimeException(sqe);

    }

No exception is being throw.  My query executes (a simple select) and
returns results.  I then immediately do a "normal" Cayenne fetch, but it
times out with the error I mentioned.  My code doesn't explicitly close the
connection, because historically I wouldn't want to disconnect from the
database.

-Lon

On Tue, Oct 14, 2014 at 4:46 PM, Andrus Adamchik <an...@objectstyle.org>
wrote:

> Hi Lon,
>
> Some code samples showing how you get and release connection would help to
> understand your problem better. But... You can actually bypass dealing with
> the internals of Cayenne stack and work with DataSource as you would in a
> straight JDBC application:
>
> ServerRuntime r = ...
> DataSource ds = r.getDataSource("mydatanode");
>
> try(Connection c = ds.getConnection()) {
>    // do your thing here.. and the Java will close the connection
>    // on exit from the "try" block
> }
>
>
> Or you can use SQLTemplate for raw SQL and avoid dealing with connections
> all together, which is a preferred way:
>
>
> http://cayenne.apache.org/docs/3.1/cayenne-guide/queries.html#sqltemplate
>
> Andrus
>
>
> On Oct 14, 2014, at 6:54 PM, Lon Varscsak <lo...@gmail.com> wrote:
>
> > Hey all,
> >
> > I have some code that executes some raw SQL and historically (with EOF) I
> > just get the connection that would normally be used for that editing
> > context.  No problems.  So I'm trying to implement that similarly in
> > Cayenne, and I'm getting the DataNode's DataSource's connection, and it
> > works, but then any subsequent calls (at least within the same request) I
> > get a timeout because it's like the connection is being used (even though
> > I'm done executing my query) and unavailable to the pool (error:  Can't
> > obtain connection. Request timed out. Total used connections: 1)
> >
> > I've been using this code with a JDBC Connection for a very long time,
> and
> > I think I'm releasing the resources I create (the Statement).  What
> could I
> > be missing here?  Is there a better way to "checkout" and then "release"
> > the connection?
> >
> > Thanks,
> >
> > Lon
>
>

Re: Access to JDBC Connection

Posted by Andrus Adamchik <an...@objectstyle.org>.
Hi Lon,

Some code samples showing how you get and release connection would help to understand your problem better. But... You can actually bypass dealing with the internals of Cayenne stack and work with DataSource as you would in a straight JDBC application:

ServerRuntime r = ...
DataSource ds = r.getDataSource("mydatanode");

try(Connection c = ds.getConnection()) {
   // do your thing here.. and the Java will close the connection 
   // on exit from the "try" block
}


Or you can use SQLTemplate for raw SQL and avoid dealing with connections all together, which is a preferred way:

  http://cayenne.apache.org/docs/3.1/cayenne-guide/queries.html#sqltemplate

Andrus


On Oct 14, 2014, at 6:54 PM, Lon Varscsak <lo...@gmail.com> wrote:

> Hey all,
> 
> I have some code that executes some raw SQL and historically (with EOF) I
> just get the connection that would normally be used for that editing
> context.  No problems.  So I'm trying to implement that similarly in
> Cayenne, and I'm getting the DataNode's DataSource's connection, and it
> works, but then any subsequent calls (at least within the same request) I
> get a timeout because it's like the connection is being used (even though
> I'm done executing my query) and unavailable to the pool (error:  Can't
> obtain connection. Request timed out. Total used connections: 1)
> 
> I've been using this code with a JDBC Connection for a very long time, and
> I think I'm releasing the resources I create (the Statement).  What could I
> be missing here?  Is there a better way to "checkout" and then "release"
> the connection?
> 
> Thanks,
> 
> Lon