spring中以dataSource方式使用proxool连接池

方式一:
 在Spring的"applicationContext.xml"中的dataSource bean定义——
 

< bean  id ="dataSource"
    class
="org.logicalcobwebs.proxool.ProxoolDataSource" >
    
< property  name ="driver" >
        
< value > com.mysql.jdbc.Driver </ value >
    
</ property >
    
< property  name ="driverUrl" >
        
< value > jdbc:mysql://localhost:3306/dbname?user=yourname &amp; password=yourpass </ value >
    
</ property >
    
< property  name ="user"  value ="yourname"   />
    
< property  name ="password"  value ="yourpass"   />
    
< property  name ="alias"  value ="Pool_dbname"   />
    
< property  name ="houseKeepingSleepTime"  value ="90000"   />
    
< property  name ="prototypeCount"  value ="0"   />
    
< property  name ="maximumConnectionCount"  value ="50"   />
    
< property  name ="minimumConnectionCount"  value ="2"   />
    
< property  name ="simultaneousBuildThrottle"  value ="50"   />
    
< property  name ="maximumConnectionLifetime"  value ="14400000"   />
    
< property  name ="houseKeepingTestSql"  value ="select CURRENT_DATE"   />
</ bean >

第一种方式需要把用户名和密码写在连接串里面,
ProxoolDataSource类提供的user,password属性似乎没有什么用。
无论提供什么,它都会以空用户名、密码去连接数据库。
这可能是Proxool RC0.93的一个bug。
不过配置中的user,password两个属性还必须设置,否则hibernate会报空指针错误。

方式二:
 在Spring的"applicationContext.xml"中的dataSource bean定义——

< bean  id ="dataSource"
    class
="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    
< property  name ="driverClassName" >
        
< value > org.logicalcobwebs.proxool.ProxoolDriver </ value >
    
</ property >
    
< property  name ="url" >
        
< value > proxool.Pool_dbname </ value >
    
</ property >
</ bean >

 

第二种方式需要预先在"web.xml"先配置好Proxool连接池,配置如下:

 

< servlet >
    
< servlet-name > proxoolServletConfigurator </ servlet-name >
    
< servlet-class >
        org.logicalcobwebs.proxool.configuration.ServletConfigurator
    
</ servlet-class >
    
< init-param >
        
< param-name > xmlFile </ param-name >
        
< param-value > WEB-INF/proxool.xml </ param-value >
    
</ init-param >
    
< load-on-startup > 1 </ load-on-startup >
</ servlet >

< servlet >
    
< servlet-name > context </ servlet-name >
    
< servlet-class >
        org.springframework.web.context.ContextLoaderServlet
    
</ servlet-class >
    
< load-on-startup > 2 </ load-on-startup >
</ servlet >

注意,第二种方式下Spring的上下文加载如果想使用listener方式(Struts2要求),
则与连接池有关的Bean全得延迟初始化。因为listener比servlet优先初始化,
如果相关Bean不是lazy-init的话,则启动服务器时会出现Bean找不到连接定义的异常。

 <listener>
  <listener-class>
      org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>


其中proxool的配置文件可以采用xmlFile"proxool.xml"或者propertyFile"proxool.properties"

"proxool.xml"格式如下:

<? xml version="1.0" encoding="UTF-8" ?>
< proxool-config >
   
< proxool >
      
< alias > Pool_dbname </ alias >
      
< driver-url > jdbc:mysql://localhost:3306/dbname </ driver-url >
      
< driver-class > com.mysql.jdbc.Driver </ driver-class >
      
< driver-properties >
      
< property  name ="user"  value ="yourname" />
      
< property  name ="password"  value ="yourpass" />
      
</ driver-properties >
      
< house-keeping-sleep-time > 60000 </ house-keeping-sleep-time >
      
< maximum-connection-count > 20 </ maximum-connection-count >  
      
< minimum-connection-count > 2 </ minimum-connection-count >
      
< prototype-count > 0 </ prototype-count >
      
< simultaneous-build-throttle > 20 </ simultaneous-build-throttle >
      
< house-keeping-test-sql > select CURRENT_DATE </ house-keeping-test-sql >
      
< statistics > 15s,10m,1d </ statistics >
      
< statistics-log-level > INFO </ statistics-log-level >
   
</ proxool >
   
< proxool >
    
<!-- 可以配置多个库 -->
   
</ proxool >
</ proxool-config >


"proxool.properties"格式如下:

jdbc- 0 .proxool.alias = Pool_dbname
jdbc-
0 .proxool.driver-url = jdbc:mysql://localhost: 3306 /dbname
jdbc-
0 .proxool.driver-class = com.mysql.jdbc.Driver
jdbc-
0 .user = yourname
jdbc-
0 .password = yourpass
jdbc-
0 .proxool.house-keeping-sleep-time = 60000
jdbc-
0 .proxool.house-keeping-test-sql = select CURRENT_DATE
jdbc-
0 .proxool.maximum-connection-count = 10
jdbc-
0 .proxool.minimum-connection-count = 3
jdbc-
0 .proxool.maximum-connection-lifetime = 18000000
jdbc-
0 .proxool.prototype-count = 3
jdbc-
0 .proxool.simultaneous-build-throttle = 10
jdbc-
0 .proxool.recently-started-threshold = 60000
jdbc-
0 .proxool.overload-without-refusal-lifetime = 50000
jdbc-
0 .proxool.maximum-active-time = 60000
jdbc-
0 .proxool.verbose = true
jdbc-
0 .proxool.trace = true
jdbc-
0 .proxool.fatal-sql-exception = Fatal error

jdbc-
2 .proxool.alias = Pool_dbname2
……
<!--可以配置多个库-->


-----------------------------------------------------------

个人比较倾向于第二种配置方式,
一来可以避免在Spring的"applicationContext.xml"中写一大堆参数,
尤其是避免了driverUrl中带用户名密码(这会显示在proxool包中带的
org.logicalcobwebs.proxool.admin.servlet.AdminServlet输出的页面中)

二来proxool连接池可以在tomcat启动时就初始化好,可以提高第一次访问web时的连接速度。

你可能感兴趣的:(spring,mysql,bean,jdbc,servlet)