codesmith :
Simplified situation:
- I have 1 Tomcat container and 1 WAR which uses a database.
- The database configuration sits in a properties file (in the war).
- I deployed the WAR 2 times, one webapp on contextpath
/a
and one webapp on contextpath/b
. - Both webapps now point to the same database (same cfg).
What I want is that each webapp points to a different database. So, the webapp on /a
points to database A and the the webapp on /b
points to database B.
How would you solve this? (without splitting the war itself)
Mehmet Sunkur :
You can do it by Tomcat's context.xml configuration without splitting your code.
You can define two context for example /a
and /b
and two different global data sources "sharedDataSourceA" and "sharedDataSourceB". You can bind different global data sources to these contexts with same name like "appDataSource".
<GlobalNamingResources>
...
<Resource name="sharedDataSourceA"
global="sharedDataSourceA"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
alternateUsernameAllowed="true"
username="bar"
password="barpass"
...
<Resource name="sharedDataSourceB"
global="sharedDataSourceB"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
alternateUsernameAllowed="true"
username="bar"
password="barpass"
...
...
</GlobalNamingResources>
<Context path="/a"...>
...
<ResourceLink
name="appDataSource"
global="sharedDataSourceA"
type="javax.sql.DataSource"
factory="org.apache.naming.factory.DataSourceLinkFactory"
username="foo"
password="foopass"
...
</Context>
<Context path="/b"...>
...
<ResourceLink
name="appDataSource"
global="sharedDataSourceA"
type="javax.sql.DataSource"
...
</Context>
Then in your code you can bind datasource to "appDataSource" by jndi lookup. Deploy the same war to /a
and /b
. They will work on different databases.
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=475708&siteId=1