Java class returns 45 database connections and same code in JSP returns 15 connections. Here is what I am trying to do.
I have created a DataSource in WebLogic. The Maximum connection capacity is set to 15. I have created three managed servers clustered and I have targeted the DataSource to all three Managed servers in the cluster. I have a java class (client) which requests database connection in a loop and it stops/errors-out when the total number of connections reaches 45 (15 connections each provided by the three Managed Servers in the cluster). When I put the same code in the JSP, I only get 15 connections max. I am not sure what is wrong.
Here is the code - please excuse me for the quality of code. This code is not mine. I am trying to investigate why the DataSource connection count stops at 15 in the JSP.
Obviously both of those aren't closing connections between attempts so it looks like you are intentionally trying to exhaust resources. The problem is that it isn't a realistic test. When real users access your JSP, they will presumably load balanced to all three servers rather than competing for the same 15 connections. Because of this, there is no reason for the server to allow sharing connections across servers. I'm guessing the client has something that does.
Setting the connection variable to "null" (line 38) is not the same as closing it. Doing so is really waiting for the GC to kick in. But since you are using connection pool (right?), not closing connection means that connection WILL NOT go back to the pool.
Also if there is an exception you are closing it (line 49). Instead, the closing part should be in finally block.
What are you trying to prove/solve here? You have bad code that is subject to the vagaries of GC and the connection pool. Many times the connection pool will detect orphaned connections, and close them. Or the database might detect an idle connection, close it, and the pool will discard it. It entirely depends on how sophisticated the connection pool is. By not explicitly closing the connection, you are subjecting the system to too many unknown factors that you have no control over.
Say, if you did find out why JSP is able to open more connections, what will you do with that knowledge? How is it going to help you? It might be an interesting exercise, but how does it help you?
I have a stupid hunch. I bet the memory settings in your 2 test beds are completely differrent. And I bet that GC runs more often when you run this test in a Java app. And I bet the connection pool has something that returns the underlying connection back to the pool in the finalize method. If all of the above are true, then GC might be returning the orphaned connection back to the pool more often in the Java app. WHich is why you can open more connections.
Do an experiment:- Increase the memory given to the Java app, and run the test again.