配置數據源的三種方式和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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM