我是 Spring(yfying)我的应用程序,它使用 Hibernate + C3P0 作为连接池。出于特定原因,我正在使用托管休眠上下文。我正在使用实用程序类“HibernateUtil”进行会话处理。对于第一次迁移到 Spring,我正在创建一个 ApplicationContext 并在 HibernateUtil 中检索一个 SessionFactory bean,以替换用于构建 SessionFactory 的代码。当我完全从 Spring 中的旧 hibernate.cfg.xml 创建会话工厂 bean 时,一切都按预期工作:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:config/hibernate.cfg.xml"></property>
</bean>
休眠.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 并将 hibernate 属性移动到 Spring bean 配置而不是使用 hibernate.cfg.xml 都无济于事。我还不能完全切换到 Spring Transaction 管理。