• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Auto DB Connection switching

 
Felix Li
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to deploy a same piece of code on different Tomcat environment with different serverport. Each environment should connect to different database connection. What is a common approach to make the code itself be able to sense the difference in serverport connection and connect itself to the appropiate db?

also, would any framework (ex. struts, hibernate) help to make it convenient at all?

Thanks in advance.
 
Paul Clapham
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A common approach is to not do that, but to configure the appropriate database connection information in each instance of Tomcat. Then your code just uses JNDI to get a connection and it's automatically connected to the right place.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4007
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if the database server and the application server are always on the same box, using the address of "localhost" for the database saves a lot of time configuring each box.
 
Felix Li
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the inputs. The environment are associated with different boxes. I think the JNDI solution sounds great. I am now looking into some info about configuring Tomcat with db and using JNDI lookup in the code. It would be great if anyone has some useful resources/links that can send me.

Thanks everyone.
 
Felix Li
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have recently tried to make use of JNDI to connect to database and it works awesome, thanks. But I have one slight problem, if I put JNDI resources under webapp/META-INF/context.xml, then it will work; but if I put it under TOMCATROOT/conf/context.xml, then it won't work. I really want it to be in the server/JVM scope so that I don't have to recreate the context resource for every single web app I have. How can I do that?

Thanks again in advance.
 
Paul Clapham
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you set up JNDI resources in Tomcat? Okay, let's move this to a more suitable forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic