我了解到要在hibernate中配置c3p0池,我们可以在hibernate.cfg.xml中编写如下配置:
<property name="hibernate.c3p0.min_size">2</property> <property name="hibernate.c3p0.max_size">5</property> <property name="hibernate.c3p0.timeout">600</property> <property name="hibernate.c3p0.max_statements">0</property> <property name="hibernate.c3p0.idle_test_period">300</property> <property name="hibernate.c3p0.acquire_increment">1</property>
但是我使用Spring配置了Hibernate。当我尝试执行以下操作时,它将无法正常工作:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/news_loader" /> <property name="username" value="blah" /> <property name="password" value="blah" /> <property name="hibernate.c3p0.min_size" value="2" /> <property name="hibernate.c3p0.max_size" value="5" /> <property name="hibernate.c3p0.timeout" value="600" /> <property name="hibernate.c3p0.max_statements" value="0" /> <property name="hibernate.c3p0.idle_test_period" value="300"/> <property name="hibernate.c3p0.acquire_increment" value="1" /> </bean>
我已经阅读了有关使用独立c3p0池的信息,该池可以使用Spring进行配置,但是有什么方法可以使用Spring在Hibernate中配置内置c3p0池呢?
启发我,因为我是初学者。
这是有关如何在数据源中配置c3p0的示例配置(来自我们的应用程序):
<bean id="dataSourceGlobal" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${driver}" /> <property name="jdbcUrl" value="${server}" /> <property name="user" value="${user}" /> <property name="password" value="${passw}" /> <!-- these are C3P0 properties --> <property name="acquireIncrement" value="${acquireIncrement}" /> <property name="minPoolSize" value="${minPoolSize}" /> <property name="maxPoolSize" value="${maxPoolSize}" /> <property name="maxIdleTime" value="${maxIdleTime}" /> </bean>
我们使用外部属性文件来配置一些参数,但是也可以直接在Spring中配置它们。
如果您希望hibernate处理池,则需要配置会话属性:
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <!--suppress InjectionValueTypeInspection --> <property name="mappingResources" ref="hibernateMappingList" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop> <prop key="transaction.factory_class"> net.sf.hibernate.transaction.JDBCTransactionFactory </prop> <prop key="hibernate.transaction.factory_class"> net.sf.hibernate.transaction.JDBCTransactionFactory </prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.cglib.use_reflection_optimizer">false</prop> <prop key="hibernate.jdbc.batch_size">0</prop> <prop name="hibernate.c3p0.min_size" value="2" /> <prop name="hibernate.c3p0.max_size" value="5" /> <prop name="hibernate.c3p0.timeout" value="600" /> <prop name="hibernate.c3p0.max_statements" value="0" /> <prop name="hibernate.c3p0.idle_test_period" value="300"/> <prop name="hibernate.c3p0.acquire_increment" value="1" /> </props> </property> </bean>
您必须使用以下方法之一:在数据源中池或在hibernate会话中池。切勿同时使用两者,因为这会浪费资源。