• Post Reply Bookmark Topic Watch Topic
  • New Topic

Allocating The DB connection in Web App  RSS feed

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im really confused about how to allocate DB connection to each user.

One approach could be Giving it a DB connection in every servlet which is what i made for very simple logic testing purpose.

a Sample code of it



This has a problem when there are many users. And i wonder if it would use the same DB connection for every user.


Please guide me to a better approach.
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using the same connection for event request served by that Servlet instance.

A few points:
  • Don't use the JDBC-ODBC bridge unless you have no choice (which is only the case if you are using Access)
  • A Connection Pool is the normal mechanism to use for Servlets.
  • Close the connection in a finally block immediately after you have finished using it.

  •  
    Gaurav Wadhwani
    Ranch Hand
    Posts: 68
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    1.Right now im using Access so no choice
    2. Can you suggest me very easy tutorials for Connection Pool. Im a beginner
    3. I do that

    thank you for the reply
     
    Paul Sturrock
    Bartender
    Posts: 10336
    Eclipse IDE Hibernate Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have a read of this. There are pooling examples.

    Its worth noting that Access is not really designed to support multiple users so is not a good choice for a Servlet application (and certainly should not be used in production). If its a tutor or similar that is specifying Access you could perhaps have a word with them? My experience is the "ease" of using Access is quickly outweighed by the headache that is the JDBC-ODBC bridge!
     
    Rancher
    Posts: 42972
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    +1 on what Paul said. Access is not really designed to be used in multi-user concurrent mode, although you may get it work acceptably. But the JDBC/ODBC bridge is most definitely not made for concurrent access (which is what a servlet environment does) - it is buggy and not thread-safe. If you HAVE to use Access in a multi-threaded environment, get a functional driver for it (like HXTT, which is commercial, but that's the price you need to pay to use Access in a web app).
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!