(1) 在hibernate.properties中添加:
Xml代码
1 hibernate.connection.provider_class =org.hibernate.connection.C3P0ConnectionProvider
(2) 新建自己写的properites,取名:jdbc.properties,关于c3p0的代码如下:
Xml代码
2 ######C3P0 MySQL config #######
3 c3p0.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8
4 c3p0.user=root
5 c3p0.password=root
7 c3p0.driverClass=com.mysql.jdbc.Driver
8 c3p0.acquireIncrement=1
9 c3p0.maxIdleTime=60
10 c3p0.maxPoolSize=200
11 c3p0.minPoolSize=50
12 c3p0.initialPoolSize=300
其中,属性文件通过如下方式读入:
(3) 在spring中对DataAccess的配置如下:
Xml代码
13 <bean id="dataSource"
14 class="com.mchange.v2.c3p0.ComboPooledDataSource"
15 destroy-method="close">
16 <property name="driverClass" value="${c3p0.driverClass}"></property>
17 <property name="jdbcUrl" value="${c3p0.url}"></property>
18 <property name="user" value="${c3p0.user}"></property>
19 <property name="password" value="${c3p0.password}"></property>
20 <property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>
21 <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
22 <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
23 <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
24 <property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
25
26 <property name="acquireRetryDelay" value="1000"></property>
27 <property name="acquireRetryAttempts" value="60"></property>
28 <property name="breakAfterAcquireFailure" value="false"></property>
29 </bean>
30 <!--Hibernate SessionFatory-->
31 <bean id="sessionFactory"
32 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
33 <property name="dataSource" ref="dataSource" />
34 <property name="lobHandler" ref="lobHandler" />
35 <property name="configLocations">
36 <list>
37 <value>classpath*:/hibernate/hibernate.cfg.xml</value>
38 </list>
39 </property>
40 <property name="configurationClass"
41 value="org.hibernate.cfg.AnnotationConfiguration" />
42 <!-- 如果采用hbm 方式
43 <property name="mappingDirectoryLocations">
44 <list>
45 <value>
46 classpath*:/org/ustb/mis/model/hbm/
47 </value>
48 </list>
49 </property>
50 -->
51 <property name="hibernateProperties">
52 <props>
53 <prop key="hibernate.dialect">
54 ${hibernate.dialect}
55 </prop>
56 <prop key="hibernate.show_sql">
57 ${hibernate.show_sql}
58 </prop>
59 <prop key="hibernate.cache.use_query_cache">
60 ${hibernate.cache.use_query_cache}
61 </prop>
62 <prop key="hibernate.cache.provider_class">
63 ${hibernate.cache.provider_class}
64 </prop>
65 <!-- 配置C3P0ConnectionProvider-->
66 <prop key="hibernate.connection.provider_class">
67 ${hibernate.connection.provider_class}
68 </prop>
69 <prop key="hibernate.current_session_context_class">
70 ${hibernate.current_session_context_class}
71 </prop>
72 </props>
73 </property>
74 </bean>
Xml代码
75 <!-- 属性文件读入 -->
76 <bean id="propertyConfigurer"
77 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
78 <property name="locations">
79 <list>
80 <value>classpath*:hibernate/jdbc.properties</value>
81 <value>classpath*:hibernate/hibernate.properties</value>
82 </list>
83 </property>
84 </bean>
Xml代码
1 hibernate.connection.provider_class =org.hibernate.connection.C3P0ConnectionProvider
(2) 新建自己写的properites,取名:jdbc.properties,关于c3p0的代码如下:
Xml代码
2 ######C3P0 MySQL config #######
3 c3p0.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8
4 c3p0.user=root
5 c3p0.password=root
7 c3p0.driverClass=com.mysql.jdbc.Driver
8 c3p0.acquireIncrement=1
9 c3p0.maxIdleTime=60
10 c3p0.maxPoolSize=200
11 c3p0.minPoolSize=50
12 c3p0.initialPoolSize=300
其中,属性文件通过如下方式读入:
(3) 在spring中对DataAccess的配置如下:
Xml代码
13 <bean id="dataSource"
14 class="com.mchange.v2.c3p0.ComboPooledDataSource"
15 destroy-method="close">
16 <property name="driverClass" value="${c3p0.driverClass}"></property>
17 <property name="jdbcUrl" value="${c3p0.url}"></property>
18 <property name="user" value="${c3p0.user}"></property>
19 <property name="password" value="${c3p0.password}"></property>
20 <property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>
21 <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
22 <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
23 <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
24 <property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
25
26 <property name="acquireRetryDelay" value="1000"></property>
27 <property name="acquireRetryAttempts" value="60"></property>
28 <property name="breakAfterAcquireFailure" value="false"></property>
29 </bean>
30 <!--Hibernate SessionFatory-->
31 <bean id="sessionFactory"
32 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
33 <property name="dataSource" ref="dataSource" />
34 <property name="lobHandler" ref="lobHandler" />
35 <property name="configLocations">
36 <list>
37 <value>classpath*:/hibernate/hibernate.cfg.xml</value>
38 </list>
39 </property>
40 <property name="configurationClass"
41 value="org.hibernate.cfg.AnnotationConfiguration" />
42 <!-- 如果采用hbm 方式
43 <property name="mappingDirectoryLocations">
44 <list>
45 <value>
46 classpath*:/org/ustb/mis/model/hbm/
47 </value>
48 </list>
49 </property>
50 -->
51 <property name="hibernateProperties">
52 <props>
53 <prop key="hibernate.dialect">
54 ${hibernate.dialect}
55 </prop>
56 <prop key="hibernate.show_sql">
57 ${hibernate.show_sql}
58 </prop>
59 <prop key="hibernate.cache.use_query_cache">
60 ${hibernate.cache.use_query_cache}
61 </prop>
62 <prop key="hibernate.cache.provider_class">
63 ${hibernate.cache.provider_class}
64 </prop>
65 <!-- 配置C3P0ConnectionProvider-->
66 <prop key="hibernate.connection.provider_class">
67 ${hibernate.connection.provider_class}
68 </prop>
69 <prop key="hibernate.current_session_context_class">
70 ${hibernate.current_session_context_class}
71 </prop>
72 </props>
73 </property>
74 </bean>
Xml代码
75 <!-- 属性文件读入 -->
76 <bean id="propertyConfigurer"
77 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
78 <property name="locations">
79 <list>
80 <value>classpath*:hibernate/jdbc.properties</value>
81 <value>classpath*:hibernate/hibernate.properties</value>
82 </list>
83 </property>
84 </bean>