• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Create Reusable database connection pooling code in Tomcat to be called by multiple Application

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

 I am new to Tomcat/Java, so please bear with my silly questions.

We have multiple independent servlet applications deployed on tomcat that calls DB for performing various operations. So I have put java code in place for loading the connections during start up inside each of the application
Referred to this - http://tomcat.apache.org/tomcat-4.1-doc/printer/jndi-datasource-examples-howto.html#MySQL%20DBCP%20Example

Now the requirement is to make the connection pooling piece as a Common DataSource Code and load all the required connections at one go. Then from each independent application call this code to get the existing connection from the pool.

Is this the right way to do it and how can we achieve it ?


Thanks,
Anju
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Anju!

Database connection pools are a standard feature in J2EE application servers, including Tomcat.

You cannot share code between web applications. But the connection pool is easily obtainable from a simple JNDI lookup, so the amount of code that has to be duplicated between applications is small.

What actually makes the pool itself shareable between multiple web applications is where you define it.

For a pool used by only one application, you'd usually define the pool in that application's context xml file. To share a pool between multiple applications, you would have to configure the Tomcal server.xml file. I believe you can define shared. Connection pools at the Host and Engine levels.

You might also want to look at this: http://tomcat.apache.org/tomcat-7.0-doc/config/globalresources.html
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not silly question at all, in fact quite challenging if you want to do a good job with it.

Your question is somewhat misleading at first because you said "multiple" applications rather than "within single application".

But let go with your "multiple" thing.

Say you define all those DB pools, your different servlets or classes will get the concern connection. This will look something like:
ClassA = connA
ClassB = connB
etc

This look a good start but think about maintaining it. What happens if your app need to support a new connection? Add a new servlet/class? Honestly I start to hate code changes will "configuration" code can do the job.

So what if a single class can get the corresponding DB connection with a parameter and voila. This will surely reduce the maintenance burden. All need changing is the app server config file, hence no code changes.

How to approach this? I thought of that a while back but didn't actually implement it but eventually have to sooner or later.

I would start off like:
Have a default getConnection method that points to say your default connection (eg production)
Have another getConnection(connName) method passing in the desired connection name (eg dev DB1 or uat DB2 etc)
Have all those connection names in some config file (preferably outside of your app)

Now the real challenge. How to tell your app which connName to use? Sure you can have a form to prompt the user for the DB conn but that's some what unnecessary and error prone when deployment comes.

I shall let you ponder that. Hope I gave you an idea.
 
A Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Tim and K.Tsang for your reply.

I believe I did not communicate my query well. This is what I have implemented so far

1. I have 3 servelt applications deployed independently in their own container.
2. Each servlet code connects to multiple DB for executing logic.
3. So in each application I have defined a DbConnectionServlet which loads all the required DB connection  (setupDataSources) for that servlet on load (loadOnStartup=1) . The init function of DB servlet looks up a config file to pull all the required JNDI for that application.
4. For actual execution I pass a parameter which helps decide which datasource connection is needed.

But the question is can this DbConnectionServlet be deployed as common code and then be referred to by other 3 servlet applications, if yes how ?. As I said I am not sure if this is right option or even if its possible.

Thanks in advance for all the help!
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Nair wrote:But the question is can this DbConnectionServlet be deployed as common code and then be referred to by other 3 servlet applications, if yes how ?. As I said I am not sure if this is right option or even if its possible.



Tim Holloway wrote:You cannot share code between web applications.



However I don't think you've implemented the database connection code in the right way. For one thing the idea of using a servlet which loads on startup has been obsolete for a good number of years now. You should use a ServletContextListener, whose contextInitialized() method gets called when your application starts -- I can find examples on the web dating back to 2009 and it wasn't new then.

And your startup code shouldn't be storing database connections -- if that's what you meant when you said "loads all the required DB connection". You could do the work to set up a DataSource, sure, but each servlet should get a connection from the DataSource when it wants to access the database and then close it before forwarding to a JSP or whatever it's doing to send the response.
 
A Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul. I will surely check the ServletContextListener option.

And you are right, on start up we just do the DB set up configuration. Whenever the actual service call happens only then do we create the actual connection and close it after the work is done.

So looks like I cannot keep the DB set up code common between web applications.
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic