• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connection Pooling with c3p0 and postgressql

 
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After getting suggestions from Everyone to use libraries like c3p0 instead of writing own connection Pool, I have used c3p0 and its shown below:

I'm calling above code from service layer like this I have kept postgresql-9.4.1208 c3p0-0.9.5.2 mchange-commons-java-0.2.3.4 in classpath , but stll I got error as shown in pic. I 'm using java8 and postgressql9.4. Where I'm going wrong or suggest me some good links to use c3P0 effectively
2.JPG
[Thumbnail for 2.JPG]
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any error in that image.

If there's an exception could you copy and paste it here?
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I didn't get any error, cpds = new ComboPooledDataSource(); this statement is not executing, setupDataSource () is returning null. [Edit: line15 is cpds = new ComboPooledDataSource() ,I have defined it as instance variable)
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see how that code compiles.

'cpds' is declared inside the try/catch, so what is it returning here on line 25 (which is outside that block)?
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but that change I have mentioned in the earlier message saying that I have kept it as instance variable. Below are the actual code and output



2.JPG
[Thumbnail for 2.JPG]
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what happens if you have a simple test harness to check this?


What happens?
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I execute your TestHarness , i got error as below
3.JPG
[Thumbnail for 3.JPG]
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think jar is the problem, need to update my mchange-commons-java i guess
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what are you doing in your code that was testing this that meant you weren't seeing that exception?

That would worry me that exceptions like that might be being lost.
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are my observations :-
1) when I keep the return statement(returning true/false) in finally block, It is eating the exceptions and showing only true or false.
2) when I keep the same return statement out of finally, It is correctly showing the Exception.
(kept both scenarios in the pic)

3) I knew that I shouldn't throw an Exception from finally block(the one which i'm throwing is exceptionally for the exception that may occur while closing connection).
4) But I don't know that I shouldn't keep a return statement in finally block.

please correct me If my 3rd and 4th observations are wrong

and my finally block code:

3.JPG
[Thumbnail for 3.JPG]
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For (3), log it, don't throw it.

Also, you can avoid all that by using the try-with-resources form of try/catch:


For (4), you shouldn't either throw a new exception (if you can avoid it) or return from finally.
Either of those will override any previous exception:

Either of those lines in the finally block (yes, it won't compile as is, you need to pick one line) will result in the exception thrown in the try block to be lost entirely.
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we declare conn in try block(try-with-resources) like below:


then we cannot rollback in catch block during Exception.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can:

Essentially, your prepared statement should by in a try-with-resources as well.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic