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:
it's unsafe to put database password in context.xml which can be viewed by all;
how do I maintain two different datasources for production and devel mode?
How do you solve this problem?
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.
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.
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.
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>
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");
}
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With