Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat 6 webapp with datasource deployment

What is the best way to deploy an app with datasource both in development and in production environments?

It is recommended to use META-INF/context.xml to specify Tomcat context but I don't understand how should I specify datasource in context.xml:

  1. it's unsafe to put database password in context.xml which can be viewed by all;

  2. how do I maintain two different datasources for production and devel mode?

How do you solve this problem?

like image 630
Andrey Minogin Avatar asked Jan 10 '11 16:01

Andrey Minogin


People also ask

How do I use DataSource with Tomcat?

Tomcat DataSource JNDI Configuration Example - server.Add below code in the tomcat server. xml file. The code should be added in the GlobalNamingResources element. Also make sure that database driver is present in the tomcat lib directory, so in this case mysql jdbc jar have to be present in the tomcat lib.

What is Tomcat webapps?

The webapps directory is where deployed applications reside in Tomcat. The webapps directory is the default deployment location, but this can be configured with the appBase attribute on the <Host> element.

What should be deploy path for Tomcat?

Another thing we can do is to deploy it by simply dropping it into the $CATALINA_HOME\webapps directory of any Tomcat instance. If the instance is running, the deployment will start instantly as Tomcat unpacks the archive and configures its context path.


2 Answers

I don't understand how should I specify datasource in context.xml:

So:

<?xml version="1.0" encoding="UTF-8"?>

<Context>
    <Resource
        type="javax.sql.DataSource"
        name="jdbc/dbname" 
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/dbname"
        username="java" 
        password="d$7hF_r!9Y"
        maxActive="100" maxIdle="30" maxWait="10000" 
    />
</Context>

And in web.xml:

<resource-env-ref>
    <resource-env-ref-name>jdbc/dbname<resource-env-ref-name>
    <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>

See also:

  • Tomcat JNDI resources HOWTO

1: it's unsafe to put database password in context.xml which can be viewed by all;

It can't be viewed by web users. It's only viewable by serveradmins who needs to know about them anyway.


2: how do I maintain two different datasources for production and devel mode?

Define two separate <Resource> each with a different name and toggle the dev/prod mode by some param in web.xml or properties file so that you can dynamically grab the one or the other datasource. E.g.

<context-param>
    <param-name>dev</param-name>
    <param-value>true</param-value>
</context-param>

with

boolean dev = Boolean.valueOf(getServletContext().getInitParameter("dev"));

if (dev) {
    dataSource = getDataSource("jdbc/devdb");
} else {
    dataSource = getDataSource("jdbc/proddb");
}
like image 140
BalusC Avatar answered Oct 18 '22 11:10

BalusC


Sorry, Looking for some code to copy and paste I found this Q&A. BalusC's is the right answer and should be marked as the answer but I don't quite agree with point 2

2: how do I maintain two different datasources for production and devel mode?

You don't need to define different context parameters or different datasources. Let's consider:

  1. The database - has the different databases/schemas and you need specific users and passwords for each database.
  2. The application server/web container - will have datasource to bind the databases.
  3. The application - refers to the datasource.

In this scenario, your application server (or whatever you are using) binds the database using the datasource and your application only knows about the datasource. So your local tomcat or whatever you are using binds via the datasource to your local dev database. When you build, pack and deploy in your application for dev, test, production or whatever environment your might have, your application server (or whatever you are using) will have the datasource defined pointing to the right database for that enviroment. You only need to keep the same JNDI name across all app servers.

like image 40
Desorder Avatar answered Oct 18 '22 10:10

Desorder