0
votes

I'm Spring(yfying) my application that uses Hibernate + C3P0 for the connection pool. I'm using a managed hibernate context for specific reasons. I'm using a utility class "HibernateUtil" for Session handling. For the first migration to Spring I am creating an ApplicationContext and retrieving a SessionFactory bean in HibernateUtil replacing the code that used to build the SessionFactory. When I create a session factory bean completely from my old hibernate.cfg.xml in Spring everything works as expected:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="configLocation" value="classpath:config/hibernate.cfg.xml"></property>

  </bean>

hibernate.cfg.xml

<hibernate-configuration>

<session-factory>

<property name="connection.username">user</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/mydb?zeroDateTimeBehavior=convertToNull</property>
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="connection.password">pass</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.max_fetch_depth">3</property>
    <property name="hibernate.current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
    <property name="hibernate.transaction.auto_close_session">false</property>


    <property name="hibernate.cache.region.factory_class">
         net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.show_sql">true</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>

    <!-- configuration pool via c3p0--> 

    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="connection.isolation">2</property>

    <property name="hibernate.c3p0.acquire_increment">3</property> 
    <property name="hibernate.c3p0.idle_test_period">120</property> <!-- seconds --> 
    <property name="hibernate.c3p0.max_size">100</property> 
    <property name="hibernate.c3p0.max_statements">50</property> 
    <property name="hibernate.c3p0.min_size">3</property> 
    <property name="hibernate.c3p0.timeout">1800</property> 

    <!-- mapping files -->
   .......

If I externalize the connection pool (I also remove all the connection settings from hibernate.cfg.xml), my transactions do not work properly.

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
     <property name="driverClass" value="${db.driver}"/>
     <property name="jdbcUrl" value="${db.url}"/>
     <property name="user" value="${db.user}"/>
     <property name="password" value="${db.pass}"/>

  </bean>

      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="configLocation" value="classpath:config/hibernate.cfg.xml"></property>

      </bean>

I've tried specifying hibernate.transaction.factory_class and moving hibernate properties to Spring bean configuration instead of using hibernate.cfg.xml all to no avail. I cannot completely switch to Spring Transaction management just yet.

1

1 Answers

1
votes

Imho you should try to move everything to Spring. In my experience when it is to mixed up there are a lot of problems. Is there a particular reason why you cannot yet set up a spring managed transaction management using:

<!-- Transaction Management -->
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

From your description it is quite dificult to find out where exactly the problem is occuring is it with the transactions or with the sessionFactory? Another point you might be causing your sessionFactory problems when using the dataSource outside of the hibernateProperties is that the ConnectionProvider implementation Hibernate is using is changing. If you specify a dataSource Hibernate will use DataSourceConnectionProvider(http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/connection/DatasourceConnectionProvider.html) whereas with setting the data source in the hibernate config Hibernate will use the DriverManagerConnectionProvider (http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/connection/DriverManagerConnectionProvider.html) The differences between these two might be the cause of your problems.