Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Connection Pooling - c3p0 - Tomcat.

Reply
Thread Tools

Connection Pooling - c3p0 - Tomcat.

 
 
Chris Smith
Guest
Posts: n/a
 
      12-16-2004
Rico <(E-Mail Removed)> wrote:
> From what I understand, just a matter of having the c3p0.jar in the
> effective classpath; adding the following code and getting connections
> from the DataSource:
>
> import com.mchange.v2.c3p0.*;
> ...
> ComboPooledDataSource cpds = new ComboPooledDataSource();
> cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver
> cpds.setJdbcUrl( "jdbcostgresql://localhost/testdb" );
> cpds.setUser("dbuser");
> cpds.setPassword("dbpassword");


That would work, but it's not ideal.

> Coz there's this part:
> "You can easily configure Apache's Tomcat web application server to use
> c3p0 pooled DataSources. Below is a sample config to get you started.
> It's a fragment of Tomcat's conf/server.xml file, which should be
> modified to suit and placed inside a <Context> element."


That is ideal. The idea behind a data source is that your code doesn't
care where the connections are coming from. If you add the above code
to your project, then you have to recompile your code to connect a
database other than PostgreSQL on localhost; not the best idea in the
world. By placing the configuration in server.xml, you can change the
database configuration by editing a config file. You'll still aquire
the data source by the same JNDI name, and your code will be identical
when you connect to a database somewhere else.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
Rico
Guest
Posts: n/a
 
      12-16-2004
As the docs say, c3p0 was designed to be very simple to use, and it is.

From what I understand, just a matter of having the c3p0.jar in the
effective classpath; adding the following code and getting connections
from the DataSource:

import com.mchange.v2.c3p0.*;
....
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbcostgresql://localhost/testdb" );
cpds.setUser("dbuser");
cpds.setPassword("dbpassword");


So I'm thinking for our web-application I can just add the above code to
my classes in WEB-INF/classes. Is that enough?

Coz there's this part:
"You can easily configure Apache's Tomcat web application server to use
c3p0 pooled DataSources. Below is a sample config to get you started.
It's a fragment of Tomcat's conf/server.xml file, which should be
modified to suit and placed inside a <Context> element."

Do I need to do this? What would it gain us?

Thanks.

Rico.
 
Reply With Quote
 
 
 
 
Rico
Guest
Posts: n/a
 
      12-17-2004
On Thu, 16 Dec 2004 09:07:27 -0700, Chris Smith wrote:
> Rico <(E-Mail Removed)> wrote:
>> Coz there's this part:
>> "You can easily configure Apache's Tomcat web application server to use
>> c3p0 pooled DataSources. Below is a sample config to get you started.
>> It's a fragment of Tomcat's conf/server.xml file, which should be
>> modified to suit and placed inside a <Context> element."

>
> That is ideal. The idea behind a data source is that your code doesn't
> care where the connections are coming from. If you add the above code
> to your project, then you have to recompile your code to connect a
> database other than PostgreSQL on localhost; not the best idea in the
> world. By placing the configuration in server.xml, you can change the
> database configuration by editing a config file. You'll still aquire
> the data source by the same JNDI name, and your code will be identical
> when you connect to a database somewhere else.


Thanks for the input Chris. Until you outlined the purpose of all this
stuff, I was at a loss even as to what JNDI is and why I would want it.
I saw Naming Directory and that reminded me of LDAP which seemed not even
remotely related to what I wanted because I've never used it.

So, hardcoding the ComboPooledDataSource into the JavaBean saw intermittent
occurrences of some ResourceClosedException or something, it seems to have
stopped after making use of our good old container's services.

I'm wondering, creating a new DataSource each time we need a connection is
the right way, isn't it? If so, what could make it so cheap?
How come the DataSource is new and cheap, yet the Connection likely isn't?

Thanks.

Rico.

 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      12-18-2004
Rico wrote:
<snip>
> I'm wondering, creating a new DataSource each time we need a connection is
> the right way, isn't it? If so, what could make it so cheap?
> How come the DataSource is new and cheap, yet the Connection likely isn't?


The whole point is that you /don't/ create a new pooled DataSource
every time. All you do is obtain a reference to the DataSource and
invoke getConnection on the object. The DataSource manages a pool
of connections and simply returns one which is currently unused.
It's cheap because you don't create a new connection; you obtain
one which has been created previously. Same when you invoke
Connection#close; you're actually returning the connection to the
pool, making it available for reallocation at some time in the
future.
 
Reply With Quote
 
Rico
Guest
Posts: n/a
 
      12-18-2004
On Fri, 17 Dec 2004 21:33:05 -0500, Sudsy wrote:

> The whole point is that you /don't/ create a new pooled DataSource
> every time. All you do is obtain a reference to the DataSource and
> invoke getConnection on the object.


Precisely. I was thinking that something from the library needs to remain
'alive'. I would understand that what Chris described as the 'ideal' way
of making use of the container's context to lookup the DataSource wouldn't
create a new pooled DataSource every time.

But it seems to me that in the 1st, non-ideal, case of "new
ComboPooledDataSource( )" hardcoded in the
WEB-INF/classes/misc/Util.class, we're creating a new pooled DataSource
every time. It even goes out of scope upon returning from the method call,
doesn't it? Which might explain the ResourceClosedException that was
thrown every now and then in this non-ideal case...

I've found an article that says that even the lookup is expensive and
recommends performing the lookup in HttpServlet's init() and keeping the
reference to DataSource around somehow. Any thoughts on the expensiveness
mentioned?

http://www.javaworld.com/javaworld/j...rformance.html

> The DataSource manages a pool of connections and simply returns one
> which is currently unused. It's cheap because you don't create a new
> connection


This much is clear. What happens in the case of using "new
ComboPooledDataSource( )" explicitly? Isn't that a new DataSource created?

Thanks.
Rico.
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      12-18-2004
Rico wrote:
<snip>
> I've found an article that says that even the lookup is expensive and
> recommends performing the lookup in HttpServlet's init() and keeping the
> reference to DataSource around somehow. Any thoughts on the expensiveness
> mentioned?


We're getting some cross-over with another thread discussing connection
pools. In fact I just responded to that thread with a quote from the
java.sql.PooledConnection javadocs.
Your information is quite correct: JNDI lookups are expensive and you
should make every effort to perform them as rarely as practicable.
In the case of Struts applications, I store the Home references in
application scope. You just have to implement a recovery mechanism
so that naming system outages don't permanently (until restart) dis-
able your application.

> This much is clear. What happens in the case of using "new
> ComboPooledDataSource( )" explicitly? Isn't that a new DataSource created?


You're creating a new object instance but the behaviour "under the
covers" might be to return a Facade. You could, in fact, be accessing
a Singleton when you invoke methods on the object. It's impossible to
tell without seeing the source code.
While I don't have access to my source at this moment, I seem to recall
performing the initial lookup on the ConnectionPoolDataSource and just
keeping it around.
 
Reply With Quote
 
simplyjava simplyjava is offline
Junior Member
Join Date: May 2008
Posts: 1
 
      05-31-2008
Hi,
While going through the forum , i came across this post. Can someone help me in configuring datasource using c3p0 in tomcatv5.5.25.
Quote:
<Resource name="jdbc/mydatasource" auth="Container"
type="javax.sql.DataSource" driverClassName="com.mchange.v2.c3p0.ComboPooledDa taSource"
url="jdbcracle:thin:@url:servicename"
username="user" password="password"/>


---------------------------------------------------------------------------
public static Connection getConnection() {
Connection conn = null;
if (datasource == null) {
datasource = locateDataSource();

}
if (datasource != null) {
try {
conn = datasource.getConnection();
} catch (SQLException e) {
LOGGER.error("Cannot get connection:" + e.getMessage()+e);
}
}

return conn;
}
/**
* Method to locate data source.
* @return
*/
private static DataSource locateDataSource() {
Context initialContext = null;
String DATASOURCE_CONTEXT = "java:comp/env/jdbc/mydatasource";
try {
initialContext = new InitialContext();
datasource = (ComboPooledDataSource)initialContext.lookup(DATAS OURCE_CONTEXT);

} catch (NamingException e) {
LOGGER.error("Cannot get connection:" + e.getMessage() +e);
}
return datasource;

}
-----------------------------------------------------------------------
i take connection like this for my data access layer
connection = DBUtil.getConnection();
i am getting class cast exception. Would some one will help me in configuring c3p0 for my application.i have c3p0.jar in my tomcat common/lib.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection Pooling, Dispose/Close/Using =?Utf-8?B?UGllcnNvbiBD?= ASP .Net 9 11-26-2008 02:58 PM
Datasource Connection Pool c3p0 Saburo Java 0 06-27-2008 10:15 AM
Re: SqlConnection and connection pooling William \(Bill\) Vaughn ASP .Net 0 11-14-2003 07:27 PM
connection pooling error Chris Szabo ASP .Net 6 08-19-2003 07:19 PM
connection pooling Trevor Hartman ASP .Net 2 07-28-2003 07:58 PM



Advertisments