• Post Reply Bookmark Topic Watch Topic
  • New Topic

removing data access code from servlets

 
Darren Briaris
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is quite a basic question and may well be in the wrong forum but forgive me for that. I am going to struggle to explain my question but I will give it a go.

I have a servlet and jsp application and I am making sure that the data access code is seperated into its own classes. My question is what is the correct way to access the data classes from the servlets?

Do I have each servlet create an instance of the data access class and then call the various methods on that instance (the data access class will get its connection to the db from a connection pool singleton class)?

Or do I have the data access class as a singleton class and access the methods within that as static methods from within the servlets?

If I have code in the data access class that I want to run syncrhonised to ensure it is only ever run once at anyone time, having mulitple instances of the data access class will mean that this synchronised code wont work properly as each instance can run at the same time?

I hope some of that makes sense!

I have real trouble getting my head around the im
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darren Briaris:
Or do I have the data access class as a singleton class and access the methods within that as static methods from within the servlets?


As a general design principle, you should never create a singleton class unless you can write a 7-page dissertation justifying its existence.
 
Darren Briaris
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


As a general design principle, you should never create a singleton class unless you can write a 7-page dissertation justifying its existence.


Fair enough! I'll drift off that idea!! So back to my plan of having an instance of each data access class created by each servlet - I think my worries about the sync'd code are unfounded because only one servlet will run the sync'd code and that servlet will use the same instance of the data access class thus the sync'd code will only be running in one instance??

I sometimes have a real problem getting my head around the 'theory' of objects / instances etc...
 
Paul Clapham
Sheriff
Posts: 21876
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A web application with servlets and JSPs? Then if at all possible, configure a connection pool in your application server (Tomcat, Weblogic, whatever it is). They all support that as far as I know. Then just use JNDI to get a connection from the pool. You shouldn't be writing that sort of logic yourself unless there's a good reason that you can't use the server's connection pool.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
... unless there's a good reason that you can't use the server's connection pool.


Yeah, that's another one of those 7-pagers.
 
Darren Briaris
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
A web application with servlets and JSPs? Then if at all possible, configure a connection pool in your application server (Tomcat, Weblogic, whatever it is). They all support that as far as I know. Then just use JNDI to get a connection from the pool. You shouldn't be writing that sort of logic yourself unless there's a good reason that you can't use the server's connection pool.


Thanks for the advice. I seem to be doing a lot of things the wrong way! I will look into using the Websphere app server connection pooling.

Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!