aspose file tools*
The moose likes JDBC and the fly likes  pooled connection object vs non pooled connection object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark " pooled connection object vs non pooled connection object" Watch " pooled connection object vs non pooled connection object" New topic
Author

pooled connection object vs non pooled connection object

ani jadhao
Ranch Hand

Joined: Dec 23, 2008
Posts: 62
Hello All,

I have found my self in a bit confusion while reading database connection pooling topic.
I have read about advantages of connection pooling, and how it Enhances performance etc.
But a question raises suddenly in my mind , that what if I put non pooled connection object reference in servlet context and access that context when ever required.
Means the connection will be made to database lets say while context initialization (only once) and same connection will be used by multiple requests. No need to create database connections/pool any more.
Will this be a valid approach in terms of designing the application?

Please help me in clearing my confusion.

SCJP 1.4 : 91%
SCWCD 5: 96%
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31074
    
232

Anj,
I see two problems with that solution. Both are big enough to render it useless.

1) When the connection expires, there is no way to refresh it.
2) It isn't thread safe. If you have more than one user or request at a time, they can both access the same connection.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
ani jadhao
Ranch Hand

Joined: Dec 23, 2008
Posts: 62
I believe for the first problem , we could place a check in code that would test connection aliveliness before use and act as per the condition of it .Wouldn't singleton connection object work for this problem?
For second problem , I am confused , what will happen if same connection reference gets accessed by multiple threads? Are there any chances of any thing getting hampered ?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2516
    
  10

Are there any chances of any thing getting hampered ?
Yes. Not all jdbc drivers are thread safe. And you might hijack someone's transaction, thereby breaking isolation of transactions.
What if screen A uses the connection to create a transaction, and updates a row,
...
Screen B uses the same transaction, updates some other record and commits (while A still has to do some more work)
...
And A detects an error somewhere, and you want to roll back the transaction in A?

================

For load balancing, availability and high availability operations, your application server might require that you put objects in Session in stead of application scope, and it might require that all objects in session scope are serializable. Is your Connection object serializable?

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
ani jadhao
Ranch Hand

Joined: Dec 23, 2008
Posts: 62
Thanks for clearing doubts .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pooled connection object vs non pooled connection object