数据库连接池相关配置。 c3p0,tomcat jdbc,druid

c3p0

destroy-method="close">









 pom.xml

<dependency>
   <groupId>c3p0groupId>
   <artifactId>c3p0artifactId>
   <version>0.9.1.2version>
dependency>


tomcat jdbc


<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
   <property name="driverClassName" value="${driverClassName}" />
   <property name="url" value="${url}" />
   <property name="username" value="${username}" />
   <property name="password" value="${password}" />
   <property name="validationQuery" value="${validationQuery}" /> 
   <property name="testWhileIdle" value="true" />  
   <property name="testOnBorrow" value="true" />  
   <property name="testOnReturn" value="false" />  
   <property name="validationInterval" value="30000" />  
 
   <property name="timeBetweenEvictionRunsMillis" value="30000" />  
 
   <property name="maxActive" value="40" />  
   <property name="minIdle" value="2" /> 
   
   <property name="maxIdle" value="40" /> 
   <property name="maxWait" value="10000" />  
   <property name="initialSize" value="4" />  
   <property name="removeAbandonedTimeout" value="60" />  
   <property name="removeAbandoned" value="true" />  
   <property name="logAbandoned" value="true" />  
    
   <property name="minEvictableIdleTimeMillis" value="30000" />  
   <property name="jmxEnabled" value="true" />  
bean> 
 
  
pom.xml
 
  
<dependency>  
    <groupId>org.apache.tomcatgroupId>  
    <artifactId>tomcat-jdbcartifactId>  
    <version>7.0.29version>  
dependency>  
<dependency>  
    <groupId>org.apache.tomcatgroupId>  
    <artifactId>tomcat-juliartifactId>  
    <version>7.0.29version>  
dependency>  
参考:http://rayoo.iteye.com/blog/1980973

 
  
druid配置
参考:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
1.	导入jar包



    com.alibaba
    druid
    1.1.6



2.	替换原连接池配置,spring-hibernate.xml 配置文件中,将原dataSource配置替换为以下配置:



   
   
   
   
   
   
   
   


   
   


   
   


   
   


   
   


   


   
   
   


   
   
   


   
      
         
         
      
   


	
   



   
   

    	





如果只是替换连接池,以下步骤可省略


3.	配置切面开启spring关联监控,spring-hibernate.xml中配置切面(根据项目实际路径配置)



			  class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
	


			  scope="prototype">
		
			
				com.xxxx.项目名.service.*
				com.xxxx.项目名.dao.*
			
		
	


	
		
	


4.在web.xml中配置开启druid监控.druid监控页面用户名,密码



  DruidStatView
  com.alibaba.druid.support.http.StatViewServlet
  
    
    resetEnable
    true
  
  
    
    loginUsername
    用户名
  
  
    
    loginPassword
    密码
  





  DruidStatView
  /druid/*




  druidWebStatFilter
  com.alibaba.druid.support.http.WebStatFilter
  
  
    exclusions
    /public/*,*.js,*.css,/druid*,*.jsp,*.swf,*.html
  
  
    principalSessionName
    sessionInfo
  
  
    profileEnable
    true
  


  druidWebStatFilter
  /*





	
5.security.xml中配置例外,不经过权限校验,pattern为DruidStatView 配置的访问路径


	
	
	
6.  配置日志文件,记录慢sql


	# Druid
	log4j.appender.druid=org.apache.log4j.DailyRollingFileAppender
	log4j.appender.druid.layout=org.apache.log4j.PatternLayout
	log4j.appender.druid.layout.ConversionPattern= [%d{HH\:mm\:ss}] %c{1} - %m%n
	log4j.appender.druid.datePattern='.'yyyy-MM-dd
	log4j.appender.druid.Threshold = WARN
	log4j.appender.druid.append=true
	log4j.appender.druid.File=druid-sql.log


7.  配置完成后,启动项目,访问http://localhost:8080/项目名/druid 配置的访问路径



你可能感兴趣的:(java,sql)