• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to access database from Servlet

 
Aryeh Golob
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was wondering if anyone could advise me regarding the best was to access a database from Servlet's.

I was thinking about initializing DB servlet in contextInitialized method of ServletContextListener and setting it as an attribute in the ServletContext.

From this point, I would use a static class, say DbUtil to perform all interactions with the database.

Is this a good approach or is there a better way to do things?

Can I initialize database using web.xml file?

Any advice, suggestions is greatly appreciated ...

Thanks in advance!

-Aryeh
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a very poor way to approach the problem. In a properly structured web application, all database access should be performed in the Model layer, not the Controller.
 
Aryeh Golob
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bear,

Appreciate the reply ...

Where did you get the idea that I was using Control layer?

Placing application in ServletContext was for access reasons.

Can you be more specific regarding the proper way to structure DB access through Model layer.

A link would be fine as well ....

Thanks for the help ....

Be Well,
Aryeh
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not 100% sure what your DB servlet would be doing.
But your mention of servlet context attributes and static classes sound sub-optimal.

The current best practice is to obtain database connections from a JNDI Datasource. Most servers support this approach now. For example Tomcat

This lets you
- change the database connection details in a configuration somewhere
- gives you connection pooling and other nice features for free :-)

Whenever you want a database connection, look up the JNDI datasource, and call getConnection() on it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aryeh Golob wrote:Where did you get the idea that I was using Control layer?


Your own words: you said "servlet". If your Model knows anything about servlets, or your servlets know anything about the database, you're doing it wrong.
 
Aryeh Golob
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys!

Looks like the JNDI is the right way to go ....

I appreciate the help!

-Aryeh
 
Rudy Gireyev
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aryeh Golob wrote:
From this point, I would use a static class, say DbUtil to perform all interactions with the database.
Aryeh just to clarify, the DbUtil class need not be a Servlet, in fact should not be a Servlet, as it will not be interacting with the HTTP client, it will only be interacting with your servlets.
Stefan Evans wrote:But your mention of servlet context attributes and static classes sound sub-optimal.
Stefan why do you feel that using the static methods in the DB Access class is sub-optimal?

Rudy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic