-->
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.  [ 3 posts ] 
Author Message
 Post subject: Spring and C3PO
PostPosted: Mon May 24, 2004 10:24 am 
Regular
Regular

Joined: Wed Apr 21, 2004 10:57 am
Posts: 62
Location: Hasselt, Belgium
I'm trying to implement C3PO in my spring supported application (Hibernate 2.1.3 - MySQL 4.1).

This was the original state of my DataSource entry in the applicationContext.xml.

My idea was to use com.mchange.v2.c3p0.ComboPooledDataSource instead of org.apache.commons.dbcp.BasicDataSource

But when I just changed this, it complained that the properties were nonWritable. After commenting them, it tried to use the c3po connection pooling but threw an error that my url couldn't be null. What was expected since I didn't specify it.

My question, where can I specify this url? Cause the only references I have on c3po connection pooling is in this applicationContect.xml and in my hibernate.cfg.xml. Or is there perhaps a better way to implement c3po in my spring application?

I guess I just forgot something, but after staring myself blind on documentation, forum posts, ... I decided to ask for help.

applicationContext.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
   <!--
      Datasource that works in any application server
      You could easily use J2EE data source instead if this were
      running inside of a J2EE container.
    -->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
      <property name="url"><value>jdbc:mysql:///ngb</value></property>
      <property name="username"><value>root</value></property>
      <property name="password"><value></value></property>
    </bean>

   <!-- Hibernate SessionFactory -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
      <property name="dataSource"><ref local="dataSource"/></property>

      <property name="configLocation">
            <value>hibernate.cfg.xml</value>
      </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
       <property name="sessionFactory"><ref bean="sessionFactory"/></property>
    </bean>

    ...

</beans>


Hibernate.cfg.xml
Code:
<hibernate-configuration>

    <session-factory>
        ...
        <property name="c3p0.initialPoolSize">1</property>
        <property name="c3p0.minPoolSize">5</property>
        <property name="c3p0.maxPoolSize">20</property>
        <property name="c3p0.timeout">45000</property>
        <property name="c3p0.max_statements">50</property>
        <property name="c3p0.validate">false</property>
        <property name="connection.provider_class">net.sf.hibernate.connection.C3P0ConnectionProvider</property>
        ...
    </session-factory>

</hibernate-configuration>


If you have need of any more code our info, please tell me.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 25, 2004 3:50 pm 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
You need to configure either a Spring-managed DataSource or a Hibernate ConnectionProvider - those are mutually exclusive. So in your case, I suggest to omit the connection-pool-related properties from hibernate.cfg.xml, and solely concentrate on the "dataSource" bean in the Spring context.

Side note: You can configure a complete Hibernate SessionFactory without hibernate.cfg.xml. Simply specify all relevant properties on LocalSessionFactoryBean; see its javadoc and the Petclinic sample app for details respectively a working example.

Using C3P0's ComboPooledDataSource is the right approach to integrate C3P0. Of course, ComboPooledDataSource doesn't have the same bean properties as DBCP's BasicDataSource, so you need to adapt those: "driverClass", "jdbcUrl", "user". See ComboPooledDataSource's javadoc for details: "setDriverClass", "setJdbcUrl", "setUser", etc.

Code:
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="driverClass"><value>com.mysql.jdbc.Driver</value></property>
      <property name="jdbcUrl"><value>jdbc:mysql:///ngb</value></property>
      <property name="user"><value>root</value></property>
      <property name="password"><value></value></property>
    </bean>


Juergen


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 7:19 am 
Regular
Regular

Joined: Wed Apr 21, 2004 10:57 am
Posts: 62
Location: Hasselt, Belgium
I guess my mistake with the jdbcUrl was with case-sensitivity. Cause I think I had about the same properties filled in (not to sure, since I deleted it all) but it kept complaining that the jdbcurl was non-writable in ComboPooledDataSource.

I had run in some problems with the app. So I left this connection pooling for a while. But it works perfect now, thx for the help.

Peter


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.