aspose file tools*
The moose likes Tomcat and the fly likes Auto DB Connection switching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Auto DB Connection switching" Watch "Auto DB Connection switching" New topic
Author

Auto DB Connection switching

Felix Li
Ranch Hand

Joined: Jul 09, 2008
Posts: 38
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.


FL<br /> <br />SCJP,SCWCD
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18659
    
    8

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

Joined: Oct 23, 2005
Posts: 3710
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Felix Li
Ranch Hand

Joined: Jul 09, 2008
Posts: 38
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

Joined: Jul 09, 2008
Posts: 38
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
Bartender

Joined: Oct 14, 2005
Posts: 18659
    
    8

How do you set up JNDI resources in Tomcat? Okay, let's move this to a more suitable forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Auto DB Connection switching