配置数据源的三种方式和sql心跳的配置

三种方式配置数据源连接池:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans 
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context 
 9         http://www.springframework.org/schema/context/spring-context.xsd
10         http://www.springframework.org/schema/tx 
11         http://www.springframework.org/schema/tx/spring-tx.xsd
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop.xsd">
14 
15     <!-- 配置数据源 01.spring的默认数据源 -->
16     <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
17         <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property 
18         name="url" value="jdbc:mysql:///news"/> <property name="username" value="wym"/> 
19         <property name="password" value="wym"/> </bean> -->
20 
21     <!-- 配置数据源 02.dbcp数据源 -->
22     <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
23         <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property 
24         name="url" value="jdbc:mysql:///news"/> <property name="username" value="wym"/> 
25         <property name="password" value="wym"/> </bean> -->
26 
27 
28     <!-- 配置数据源 03.c3p0数据源 -->
29     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
30         <property name="driverClass" value="${driverClass}" />
31         <property name="jdbcUrl" value="${jdbcUrl}" />
32         <property name="user" value="${user}" />
33         <property name="password" value="${password}" />
34     </bean>
35 
36     <!-- 01. 使用配置文件 加载 数据库需要的4要素 经常使用 -->
37     <context:property-placeholder location="classpath:jdbc.properties" />
38 
39     <!-- 02.使用配置文件 加载 数据库需要的4要素 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
40         <property name="location" value="classpath:jdbc.properties"></property> </bean> -->
41 
42 
43 <!--配置sessionFactory -->
44     <bean id="sessionFactory"
45         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
46         <!-- 读取hibernate配置文件<property name="configLocation" value=classpath:hibernate.cfg.xml"/> -->
47 
48         <!-- 配置数据源 -->
49         <property name="dataSource" ref="dataSource"></property>
50         <!-- 配置映射文件 -->
51         <property name="mappingDirectoryLocations" value="cn/teacher/bean" />
52         <property name="hibernateProperties">
53             <props>
54                 <prop key="hibernate.hbm2ddl.auto">update</prop>
55                 <prop key="hibernate.show_sql">true</prop>
56                 <prop key="hibernate.format_sql">true</prop>
57                 <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLInnoDBDialect</prop>
58             </props>
59         </property>
60     </bean>
61     <!--配置dao -->
62     <bean id="teacherDao" class="cn.teacher.dao.TeacherDaoImpl">
63         <property name="sessionFactory" ref="sessionFactory" />
64     </bean>
65     <!-- 配置service -->
66     <bean id="teacherService" class="cn.teacher.service.TeacherServiceImpl">
67         <property name="dao" ref="teacherDao"></property>
68     </bean>
69     <!-- 配置action层 -->
70     <!-- ===================事务================== -->
71     <!-- 配置事务管理器 -->
72     <bean id="transactionManager"
73         class=" org.springframework.jdbc.datasource.DataSourceTransactionManager">
74         <property name="dataSource" ref="dataSource" />
75     </bean>
76 
77     <tx:advice id="txAdvice" transaction-manager="transactionManager"><!-- 设置事务通知 -->
78         <tx:attributes>
79             <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" />
80             <tx:method name="del*" isolation="DEFAULT" propagation="REQUIRED" />
81             <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
82             <tx:method name="find*" read-only="true" isolation="DEFAULT"
83                 propagation="REQUIRED" />
84         </tx:attributes>
85     </tx:advice>
86     <aop:config><!-- 指定切入点 -->
87         <aop:pointcut expression="execution(* *..service.*.*(..))"
88             id="myPoint" />
89         <aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint" />
90     </aop:config>
91 </beans>
 

配置sql心跳:

 <!-- sql心跳    保证连接池中的连接是真是有效的-->
       <!--开启Evict的定时校验,循环校验  -->
       <property name="testWhileIdle" value="true"></property>
       <!-- 定义Evict的时间间隔,单位:毫秒 -->
       <property name="timeBetweenEvictionRunsMills" value="60000"></property>
       <!-- 在进行borrowObject处理时,会对拿到的 连接进行校验-false-->
       <property name="testOnBorrow" value="false"></property>
       <!-- 在进行ruturnObject处理时,会对返回的连接进行校验-false -->
       <property name="testOnReturn" value="false"></property>
       <!-- 校验使用的sql语句,validatetionQuery,复杂的校验sql会影响性能 -->
       <property name="validationQuery" value="select 1"></property>
       <!-- 配置每次校验连接的数量,一般等于maxActive -->
       <property name="numTestsPerEvictionRun" value="maxActive"></property>

properties文件:

 1 jdbc.driver=com.mysql.jdbc.Driver
 2 jdbc.url=jdbc:mysql://localhost:3306/sldb?useUnicode=true&characterEncoding=utf-8
 3 jdbc.username=hhr
 4 jdbc.password=hhr
 5 
 6 #最小空闲数
 7 minIdle=45
 8 #允许最大空闲数  不能配置太小
 9 maxIdle=50
10 #初始化时 连接个数   默认是0
11 initialSize=5   
12 #同时连接的最大活动数   默认是8
13 maxActive=100
14 #最大等待时间
15 maxWait=100
16 #超过这个时间就会将没用的连接回收
17 removeAbandonedTimeout=180
18 #是否开启无用连接的回收机制
19 #【当前空闲连接数<2 && 当前活动数>最大活动数-3】这种情况下会回收
20 removeAbandoned=true
jdbc.properties
原文地址:https://www.cnblogs.com/HHR-SUN/p/6850713.html