• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pooled connection object vs non pooled connection object

 
ani jadhao
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ani jadhao
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2608
14
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ani jadhao
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing doubts .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic