-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: How to obtain programmatic reference to c3p0 datasource
PostPosted: Wed Jan 10, 2007 5:21 pm 
Newbie

Joined: Wed Jan 10, 2007 5:15 pm
Posts: 7
Hi,

I use java and configure c3p0 via the default config files: hibernate.cfg.xml and c3p0.properties

During runtime, how do I get a reference to the connection pooling object of c3p0 so that I can read the already congfiured variables and other statistics from that object


Top
 Profile  
 
 Post subject: i forgot to mention that I use spring
PostPosted: Wed Jan 10, 2007 5:40 pm 
Newbie

Joined: Wed Jan 10, 2007 5:15 pm
Posts: 7
so, maybe this will work?
I will try and let you know

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost/xxx"/>
<property name="user" value="xxx"/>
<property name="password" value="xxx"/>
</bean>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 12:54 am 
C3P0 Developer
C3P0 Developer

Joined: Tue Jan 06, 2004 8:58 pm
Posts: 145
Hi.

Recent prereleases of c3p0-0.9.1 include a JMX management interface, which provides a very convenient way of monitoring pool stats without programmatic access.

If you do want programmatic access, you can use static methods of the class C3P0Registry to find any active C3P0 PooledDataSources. Please see c3p0's docs for more information.

smiles,
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 8:19 pm 
Newbie

Joined: Wed Jan 10, 2007 5:15 pm
Posts: 7
Thanks!

I couldnt find any examples in the docs on the JMX stuff to do what I want. Also the programmatic reference doesnt seem to have a reference to the "poolconfig" object?
(i.e. i cant seem to be able to read the "timeout" constants "maxconnectionage" etc from the programmatic reference to the pooling object)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 9:57 pm 
C3P0 Developer
C3P0 Developer

Joined: Tue Jan 06, 2004 8:58 pm
Posts: 145
wouvlfe,

The JMX mbean is the easiest approach. I'm not sure what sort of documentation you want, other than that it is there by default. If you run a JMX agent (like jconsole) on an app using 0.9.1, you'll find (under com.mchange.v2.c3p0) mbeans for each of your c3p0 PooledDataSource, and all config properties will be accessible from those mbeans.

The PoolConfig object only exists at initialization time; there's no way to get to it. c3p0 is architectured after the recommendations of the JDBC-spec, even where its author (me!) would have chosen to do things differently. One peculiarity of the JDBC-spec architecture (intended to make ConnectionPoolDataSources portable over pooling implementations, which is rarely useful in practice) is that most pool configuration data is stored as properties of the ConnectionPoolDataSource, rather than of the client-visible DataSource implementation. I probably should let the client DataSource serve as a "facade" through which you can access the underying config properties, but as of now, that's not there, except via the JMX mbean. You can get at this stuff. How you do it is sketched out below. It looks more complicated than it is. Unless you are doing something very unusual, either NORMAL CASE #1 or NORMAL CASE #2 will apply, and you'll have an object from which you can access config
properties.

Code:
import javax.sql.*;
import com.mchange.v2.c3p0.*;
import com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource;

DataSource ds = ...; //your c3p0 PooledDataSource
if ( ds instanceof ComboPooledDataSource )
  {
      // NORMAL CASE #1

      ComboPooledDataSource cpds = (ComboPooledDataSource) ds;
      ...

      // cpds has direct accessors for all config properties, check out
      // the api docs for ComboPooledDataSource
  }
else if (ds instanceof AbstractPoolBackedDataSource)
{
     AbstractPoolBackedDataSource apbds = (AbstractPoolBackedDataSource) ds;
     ConnectionPoolDataSource cpds = apbds.getConnectionPoolDataSource();
    if (cpds instanceof WrapperConnectionPoolDataSource)
    {
        // NORMAL CASE #2

        WrapperConnectionPoolDataSource wcpds = (WrapperConnectionPoolDataSource) cpds;
        ...
      // cpds has direct accessors for most config properties, check out
      // the api docs for WrapperConnectionPoolDataSource
    }
    else
    {
       // unusal -- you build a c3p0 PooledDataSource using your
       // own ConnectionPoolDataSource implementation. You'd
       // know it if you did this. c3p0-specific config properties are
       // almost certainly the documented defaults. (If this is your
       // situation -- very unlikely -- then ask, and I'll explain in
       // more detail.
    }
  }
else
  {
      // not a c3p0-0.9.1-defined implementation
  }


Good luck!
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 12, 2007 4:50 pm 
Newbie

Joined: Wed Jan 10, 2007 5:15 pm
Posts: 7
Awesome, thanks for showing how to do that. That was exactly what I was looking for.

Now I am trying to understand c3p0 a little better than simply tweaking and using a set of configs that doesnt cause problems.

It seems like there are two different pooling methods provided, PoolBackedDataSource vs. a ComboPooledDataSource?
(mine turned out to be normal case #2 but dont know why its not #1)
What is the difference between the two?
How do I specify which one I want in hibernate.cfg.xml or c3p0.properties?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 3:57 am 
C3P0 Developer
C3P0 Developer

Joined: Tue Jan 06, 2004 8:58 pm
Posts: 145
wouvlfe,

Whether the implementation of a c3p0 PooledDataSource ends up being a PoolBackedDataSource or ComboPooledDataSource depends on how it is constructed. If you construct a ComboPooledDataSource directly, obviously, you get a ComboPooledDataSource. If you use c3p0's DataSources factory, or if you "roll your own" PoolBackedDataSource wrapping a non-c3p0 ConnectionPoolDataSource, then you get a PoolBackedDataSource. There's no way to configure this in hibernate, except by writing your own ConnectionProvider (hibernate's ConnectionProvider builds its c3p0 DataSource as it sees fit). So, the code above is written agnostically, to work in either case. (c3p0-0.9.1 merges the implementations, so that all extend AbstractPoolBackedDataSource, so for the most recent versions of c3p0 and future versions, you can just omit the first case.

BTW, I think in Spring, the config you show or something like it will cause the implementation of ComboPooledDataSource as desired. (ComboPooledDataSource is what you should use with JavaBean-centric tools and APIs.)


smiles,
Steve


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.