Simplified situation:
/a
and one webapp on contextpath /b
. 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)
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.