servlet - many contexts - how to share db connection?
I have .war file A and .war file B and .war file C; B and C need to use database connection so I decided to use .war A to share database connection. But the problem is that .war files are limited with their own contexts. So my question is how to get database开发者_运维技巧 connection to share it among my Tomcat web apps? And how to limit the connection access just for a few apps?
You probably don't actually want to share a single connection across apps. You probably want to share the thing that creates connections, which should be a connection pool. Put an appropriate implementation of DataSource into Tomcat's JNDI registry. Then all apps can retrieve a connection from the same source. To restrict access to some apps, just force them to use the getConnection() method that requires a username and password, and only the apps that have credentials can use it.
Declare in your Tomcat's server.xml
a dataSource resource and reference that in your context.xml
. This covers your case.
精彩评论