我是Spring(yfying)我的應用程序,它使用Hibernate + C3P0作爲連接池。由於特定的原因,我使用託管的hibernate上下文。我正在使用實用程序類「HibernateUtil」進行會話處理。對於第一次遷移到Spring,我創建了一個ApplicationContext並在HibernateUtil中檢索了一個SessionFactory bean,替換了用於構建SessionFactory的代碼。當我從我的舊的hibernate.cfg.xml在春天的一切完全創建一個會話工廠bean按預期工作:將Hibernate管理的C3P0池遷移到Spring管理
<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 -->
.......
如果我外部化連接池(我也從hibernate.cfg.xml中刪除所有的連接設置),我的交易無法正常工作。
<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>
我已經試過指定hibernate.transaction.factory_class動人休眠屬性的Spring bean配置,而不是使用hibernate.cfg.xml的一切都是徒勞的。我還不能完全切換到Spring Transaction管理。
傳統代碼,全部代碼中的所有服務的單例模式,自定義安全性的自定義Web框架。感謝您指出確切的區別 – user979051 2013-05-16 01:58:54