aspose file tools*
The moose likes Tomcat and the fly likes Tomcat, MySQL,JSP/Servlets -> Connection Pooling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat, MySQL,JSP/Servlets -> Connection Pooling" Watch "Tomcat, MySQL,JSP/Servlets -> Connection Pooling" New topic
Author

Tomcat, MySQL,JSP/Servlets -> Connection Pooling

Aditya Dhekney
Greenhorn

Joined: Jan 06, 2004
Posts: 7
Hi all,
I'm a bit confused about how connection pooling works with tomcat right now. I have what i think is connection pooling working right now, but have a few concerns.
The following is some code that i picked up from one of the sites with which i retreive a connection:
public class Pool {
/**
* Method Pool create a new connection pool
*/
public Pool() {
System.err.println("DBUtil instance created.");
}
/**
* Method getConnection.
* @return Connection New connection from the pool
*/
public static Connection getConnection() {
Connection conn = null;
try {
Context ctx = (Context) new InitialContext().
lookup("java:comp/env");
conn = ((DataSource) ctx.lookup("jdbc/db2")).
getConnection();
}
catch (Exception e) {
e.printStackTrace(System.err);
throw new ApplicationError(e);
}
return conn;
}
}

Everytime i need a connection, i use:
Connection conn=Pool.getConnection();
..and conn.close(); to return it to the pool.
Now, here is what i don't understand. Am i not creating a new Initial Context every time i call this? I know that creating an InitialContext is VERY expensive..and does this not create N number of connections in the pool everytime? I don't think what i'm doing is right, but if it is, i need some reassurance.
I thought that i should only be creating one InitialContext when the application is started. Now, for a jsp/servlet application, when/where should i be doing this? Can someone please explain to me what happens when i call the getConnection method above? Am i actually creating a new context everytime, or does tomcat have some sort of way of handling this?
Thanks in advance,
Aditya
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Since this so Tomcat-specific, I'm moving this along to the Tomcat forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Yes, you're creating a single connection object every time you call getConnection() so this is not a very good Pool. Not to mention the InitialContext.

Is there any reason you're not using DBCP that comes with Tomcat?

http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html
Aditya Dhekney
Greenhorn

Joined: Jan 06, 2004
Posts: 7
Originally posted by Mike Curwen:
Yes, you're creating a single connection object every time you call getConnection() so this is not a very good Pool. Not to mention the InitialContext.

Is there any reason you're not using DBCP that comes with Tomcat?

http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html

Hi,
I thought i was using the DBCP that comes with tomcat...i used that example to set everything up in tomcat actually. The code might be a bit misleading cause i'm not using "DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/jdbc/TestDB");"
in that example...but in the actual code i am (can't access the code from where i am right now...).
In any case, will this (pooling) work if i create a new ConnectionPool Servlet, with a public DataSource object, and the rest of the code (getting InitialContext, Initializing/associating the DataSource with the db.) within the init() method. Then, i am thinking of having another getConnection() method within the servlet that just does a ds.getConnection()and returns this where it is needed. I am not sure if i can return objects in a servlet though...if i can't, i am thinking of saving the connection in a javabean...but dont' know if that is a good idea.
Could i implement it in just a regular class (ie. not servlet) and have it inside it's init() method? Will this work the same way? I know that for a servlet, the init() method is only called once (when it is intially invoked)...but don't know if regular classes 1) have such a thing, and 2) if it would only be called once.
Could someone please help me clear up my confusion? I currently have an app that works great for a few hours, and then suddenly comes down to a screeching halt...and that requires me to restart tomcat!
Thanks in advance,
Aditya
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Ok, I've more carefully read your post, and checked it against the docs.

The docs do something very similar with a plain java class called DBTest (not a servlet). This class is then used within a JSP page, it's init() method is called (not a servlet init method), and then some values are retrieved.

So the answer to your paragraph of questions starting: "Could i implement it in just a regular class...?" is "Yes, you can abstract away the retrieval of a connection into a separate class, and use it from within any servlet". The other questions about 'do objects have a method that is called once and only once, and at 'init' time?' Yes, regular classes have this, it's called the constructor

As to this:
i am thinking of having another getConnection() method within the servlet that just does a ds.getConnection()and returns this where it is needed. I am not sure if i can return objects in a servlet though...if i can't, i am thinking of saving the connection in a javabean...but dont' know if that is a good idea.

You can return objects in servlets, but not FROM them. So if by your question, you mean "can a private method inside a servlet class return objects to the doGet or doPost method?", the answer is Yes. If you mean "can I make this connection broker object a servlet, and have it return an object, rather than HTML response?".. then just do NOT go down this path. As for javabeans as a workaround... I'd also recommend steering AWAY from this. Finally, if you have a method that does exactly one thing, which is to call another method.. then why would you need that method? Why not call the 'inner method' directly?

How about something simple like this. In the init() method of any servlet you write, do something like this (warning: this is code 'as typed' and has not been compiled or tested):

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tomcat, MySQL,JSP/Servlets -> Connection Pooling