• Post Reply Bookmark Topic Watch Topic
  • New Topic

difficulties implementing tomcat datasource  RSS feed

 
Tapio Niemela
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, could someone help me please. I have a sort of problem with Tomcat and connection pooling/datasource-type connection object. I have this servlet (called "program"), which gets Connection from the helper class(MyConnection) with static connect-method..


now, the config values for the datasource in the conf/server.xml are following:



I use MySQL 4.1-dbms and I can see open connections (processes), PID, user, Host, DB, command (like "sleep") and runningtime (in seconds, since last time it was put into "sleep") with one of tools it offers..now I'm bit confused with this datasource approach..

I have made also another way to handle DB connections. In this approach I implemented a context-listener and used the servletContext to save (and fetch) 10 different connections. With this approach there was always excatly 10 processes (connections) running in the MySQL. This approach makes totally sense to me: if I wanted a connection I just fetched it from the servletContext, used it and returned it, and when I went to see open connections in the MySQL-tool I could see that one connection was used and then put into "sleep". I could see this because it's running time was different to the others.

But this differs from the approach datasource offers. And it seems quite absurd. If I open multiple "program"-servlets (like with a html-page with multiple framesets all containing "program") I can see that there were 2 connections running (although I think there should have been 10 after configurations in server.xml), but if I open just one "program"-servlet I can see there were only 1 connection running (well actually, that's quite easy to understand ) . Anyway, this all is quite difficult to understand. Shouldn't there be 10 connections running when 10 "programs" are running? And after that shouldn't they be put into "sleep" in the MySQL so they can be easily (and fast) re-used? Thank you for advance..
 
Jinesh Parekh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Apache DBCP a Connection is allocated when required (not when the server boots up) and then when it is closed the Connection isn't actually closed but just saved in a pool for the next request that comes in for a Connection.

The reason you cannot corelate the number of connections with the number of programs running is that we do not know the underlying implementation of the connection pooling library (dbcp). Even if you configure 10 connections, we cannot be sure that they would be created when you boot up your server. It can use a LAZY approach and create the connections only when they are required. Now when u open up 10 program-servlets, its possible that they can share connections from the pool. So there could be less connections.

The maxActive property only ensures that there are not more than 10 connections. It does not say that there are exactly 10 connections.

I hope this helped.

Jinesh
 
Tapio Niemela
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, and thanks for your answer. I see what you are meaning (that new connections are opened only when needed)

I studied this concept little bit more and made an application that tries to update item which has a X-lock on it inside transaction and then tries to return connections back to the pool (eg it won't return the connection back to the pool before it has finished and it can't finish before the X-lock has been unlocked)..anyways what I found out was that with different browsers (eg IE and opera) you can get more than just 2 connections!!! I was using only IE when I was testing and this caused me mysterious problems

I think when I was using only one browser (IE) with multiple frames, the server thought that I was only one client (and gave me only 2 connections, I still wonder why 2 instead of one)..but when I used another type browser (opera) I could get more connections than just two..I still kind of wonder why I could get 2 connections, could someone explain this?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!