Tomcat: 1 war, deployed 2x, 2 configurations

Simplified situation:

  • I have 1 Tomcat container and 1 WAR that uses a database.
  • The database configuration is in the properties file (in the war).
  • I deployed WAR 2 times, one webapp on contextpath /a and one webapp on context path /b .
  • Both web applications now point to the same database (same cfg).

I want each webapp to point to a different database. So webapp on /a points to database A, and webapp on /b points to database B.

How would you solve this? (without sharing the war itself)

+5
source share
3 answers

You can do this using the Tomcat configuration context.xml without code splitting.

You can define two contexts, for example /a and /b , and two different global data sources "sharedDataSourceA" and "sharedDataSourceB". You can associate different global data sources with these contexts with the same name as "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 link the data source to "appDataSource" using jndi lookup. Deploy the same war to /a and /b . They will work in different databases.

+9
source

You can get the current context programmatically and configure the data source according to the received value. For example, using javax.servlet.ServletContext.getContextPath ().

You can also load the properties file according to the context name.

+2
source

getContextPath () , and here is a method for using it with getInitParameter ()

As an example, if you had two contexts: " / dev " and " / prod " - as indentical - and you created your network. xml with entries like:

 <context-param> <param-name>database_ip_prod</param-name> <param-value>192.168.1.10</param-value> </context-param> <context-param> <param-name>database_ip_dev</param-name> <param-value>127.0.0.1</param-value> </context-param> 

And using this method:

 public String getContextInitParam( javax.servlet.ServletContext context, String key) { key += context.getContextPath().replace("/","_"); return context.getInitParameter(key); } 

A call like this from jsp or servlet:

getContextInitParam (request.getServletContext (), "database_ip" );

returns 192.168.1.10 in the / prod context and 127.0.0.1 in the / dev context.

0
source

Source: https://habr.com/ru/post/1273933/


All Articles