0

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user592704
  • 3,674
  • 11
  • 70
  • 107
  • I think something similar was mentioned there: http://stackoverflow.com/questions/6807818/share-java-classes-between-war-files-in-tomcat – gentimouton Aug 01 '11 at 19:52
  • It is quite interesting but I want to share inited db connection (which is connected to avoid each time new connection from B and C but get connection and do a query execution) but the thread says of just a lib class sharing, isn't it? :X – user592704 Aug 01 '11 at 19:58

2 Answers2

3

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.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • But what if I need to have more then one db login connection? Can I use some two resources for example? – user592704 Aug 01 '11 at 20:19
  • Use as many as you want, but I already suggested using the DataSource.getConnection() method that would let each app specify its own login credentials. That will let you do the same thing. – Ryan Stewart Aug 01 '11 at 21:13
  • Actually, I'd like to keep db connection in session but ONE contexts limits don't allow doing this :( Is it possible to share session data between .war(s) then? – user592704 Aug 02 '11 at 15:58
  • I'd strongly discourage holding a connection open like that. You'll likely run into problems with firewalls and timeouts, and there's no real reason to do so. Why do you think you want to? As to sharing session data between contexts, I believe that would come under the heading of security violations, so no, there's probably no good way to do that. – Ryan Stewart Aug 02 '11 at 21:46
  • Because I am going to use localhost :) – user592704 Aug 03 '11 at 20:10
  • Still, use a connection pooling DataSource implementation. – Ryan Stewart Aug 04 '11 at 18:46
  • OK, I will :) But should I keep it in session or how to keep it alive within web-container is running? – user592704 Aug 07 '11 at 00:36
1

Declare in your Tomcat's server.xml a dataSource resource and reference that in your context.xml. This covers your case.

Michael-O
  • 18,123
  • 6
  • 55
  • 121
  • So the solves all things I asked in my question or there should be something more? – user592704 Aug 01 '11 at 20:23
  • This should do it. The container will manage **one** shared connetion pool for all of your apps. – Michael-O Aug 01 '11 at 20:49
  • JDBC is not a pool. It is just the technology for DB connection. You should read Java & Database 101. – Michael-O Aug 03 '11 at 08:37
  • I know it is :) Actually I want to try DataSource. The thing is I don't want to keep all in a web-container settings... All because I want have ability to change connection options dynamically. But all ways I could find do describe some static connection way :( Is it really the only way to do or I could miss something? – user592704 Aug 03 '11 at 20:15
  • In this case, JNDI is designed to be a read-only store. Changing stuff globally may result in faulty behavior. If you mant to manipulate the dataSource at runtime, make sure that no one uses it and create your own instance of a DBCP pool and manage it. – Michael-O Aug 04 '11 at 06:16