c3p0配置
1.web.xml配置:
<!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml Spring 容器启动监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:**/spring/*bean*.xml </param-value> </context-param>
2.dataSource.xml配置:
<bean id="parentDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 指定连接数据库连接池的最大连接数 --> <property name="maxPoolSize"> <value>${jdbc.maxPoolSize}</value> </property> <!-- 指定连接数据库连接池的最小连接数 --> <property name="minPoolSize"> <value>${jdbc.minPoolSize}</value> </property> <!-- 指定连接数据库连接池的初始化连接数 --> <property name="initialPoolSize"> <value>${jdbc.initialPoolSize}</value> </property> <!-- 指定连接数据库连接池的连接的最大空闲时间 --> <property name="maxIdleTime"> <value>${jdbc.maxIdleTime}</value> </property> <property name="maxStatements"> <value>0</value> </property> </bean> <!-- 定义数据源Bean,使用C3P0数据源实现 惠明之家--> <bean id="defaultSource" parent="parentDataSource" > <!-- 指定连接数据库的驱动 --> <property name="driverClass"> <value>${jdbc.driver}</value> </property> <!-- 指定连接数据库的URL --> <property name="jdbcUrl"> <value>${jdbc.url}></value> </property> <!-- 指定连接数据库的用户名 --> <property name="user"> <value>${jdbc.user}</value> </property> <!-- 指定连接数据库的密码 --> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <!-- 定义数据源Bean,使用C3P0数据源实现 预售--> <bean id="theSecondSource" parent="parentDataSource"> <!-- 指定连接数据库的驱动 --> <property name="driverClass"> <value>${jdbc.driver}</value> </property> <!-- 指定连接数据库的URL --> <property name="jdbcUrl"> <value>${jdbc.urlthesecond}></value> </property> <!-- 指定连接数据库的用户名 --> <property name="user"> <value>${jdbc.user}</value> </property> <!-- 指定连接数据库的密码 --> <property name="password"> <value>${jdbc.passwordthesecond}</value> </property> </bean> <bean id="dataSource" class="org.util.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="default" value-ref="defaultSource"/> <entry key="theSecond" value-ref="theSecondSource"/> </map> </property> <property name="defaultTargetDataSource" ref="defaultSource"/> </bean>
proxool配置,在spring目录下增加proxoo.xml文件,并修改 web.xml及dataSource.xml文件信息,增加spring-web.jar包,具体如下:
1.proxool.xml文件配置如下,可以配置多个数据源:
<proxool-config> <proxool> <alias>default_pool</alias> <driver-url>jdbc:jtds:sqlserver://192.168.11.89:1433/Backstage</driver-url> <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="sa"/> <property name="password" value="123"/> </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>10s,1m,1d</statistics> <statistics-log-level>INFO</statistics-log-level> </proxool> <proxool> <alias>Second_pool</alias> <driver-url>jdbc:jtds:sqlserver://192.168.11.88:1433/database</driver-url> <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="sa"/> <property name="password" value="123"/> </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>10s,1m,1d</statistics> <statistics-log-level>INFO</statistics-log-level> </proxool> </proxool-config>
具体参数说明如下:
fatal-sql-exception: 它是一个逗号分割的信息片段.当一个SQL异常发生时,他的异常信息将与这个信息片段进行比较.如果 在片段中存在,那么这个异常将被认为是个致命错误(Fatal SQL Exception ).这种情况下,数据库连接将要被放弃.无论 发生什么,这个异常将会被重掷以提供给消费者.用户最好自己配置一个不同的异常来抛出. fatal-sql-exception-wrapper-class:正如上面所说,你最好配置一个不同的异常来重掷.利用这个属性,用户可以包装 SQLException,使他变成另外一个异常.这个异常或者继承SQLException或者继承字RuntimeException.proxool自带了2个实现 :'org.logicalcobwebs.proxool.FatalSQLException' 和'org.logicalcobwebs.proxool.FatalRuntimeException'.后者更合适. house-keeping-sleep-time: house keeper 保留线程处于睡眠状态的最长时间,house keeper 的职责就是检查各个连接的状态,并判 断是否需要销毁或者创建. house-keeping-test-sql: 如果发现了空闲的数据库连接.house keeper 将会用这个语句来测试.这个语句最好非常快的被执行. 如果没有定义,测试过程将会被忽略。 injectable-connection-interface: 允许proxool实现被代理的connection对象的方法. injectable-statement-interface: 允许proxool实现被代理的Statement 对象方法. injectable-prepared-statement-interface: 允许proxool实现被代理的PreparedStatement 对象方法. injectable-callable-statement-interface: 允许proxool实现被代理的CallableStatement 对象方法. jmx: 如果属性为true,就会注册一个消息Bean到jms服务,消息Bean对象名: "Proxool:type=Pool, name=<alias>". 默认值为false. jmx-agent-id: 一个逗号分隔的JMX代理列表(如使用MBeanServerFactory.findMBeanServer(String agentId)注册的连接池。) 这个属性是仅当"jmx"属性设置为"true"才有效。所有注册jmx服务器使用这个属性是不确定的 jndi-name: 数据源的名称 maximum-active-time: 如果housekeeper 检测到某个线程的活动时间大于这个数值.它将会杀掉这个线程. 所以确认一下你的服务器的带宽.然后定一个合适的值.默认是5分钟. maximum-connection-count: 最大的数据库连接数. maximum-connection-lifetime: 一个线程的最大寿命. minimum-connection-count: 最小的数据库连接数 overload-without-refusal-lifetime: 这可以帮助我们确定连接池的状态。如果我们已经拒绝了一个连接在这个设定值(毫秒), 然后被认为是超载。默认为60秒。 prototype-count: 连接池中可用的连接数量.如果当前的连接池中的连接少于这个数值.新的连接将被建立(假设没有超过最大可用数). 例如.我们有3个活动连接2个可用连接,而我们的prototype-count是4,那么数据库连接池将试图建立另外2个连接. 这和 minimum-connection-count不同. minimum-connection-count把活动的连接也计算在内.prototype-count 是spare connections 的数量. recently-started-threshold: 这可以帮助我们确定连接池的状态,连接数少还是多或超载。只要至少有一个连接已开始在此值(毫秒)内, 或者有一些多余的可用连接,那么我们假设连接池是开启的。默认为60秒 simultaneous-build-throttle: 这是我们可一次建立的最大连接数。那就是新增的连接请求,但还没有可供使用的连接。由于连接可以使用多线程, 在有限的时间之间建立联系从而带来可用连接,但是我们需要通过一些方式确认一些线程并不是立即响应连接请求的,默认是10。 statistics: 连接池使用状况统计。 参数“10s,1m,1d” statistics-log-level: 日志统计跟踪类型。 参数“ERROR”或 “INFO” test-before-use: 如果为true,在每个连接被测试前都会服务这个连接,如果一个连接失败,那么将被丢弃,另一个连接将会被处理,如果所有 连接都失败,一个新的连接将会被建立。否则将会抛出一个SQLException异常。 test-after-use: 如果为true,在每个连接被测试后都会服务这个连接,使其回到连接池中,如果连接失败,那么将被废弃。 trace: 如果为true,那么每个被执行的SQL语句将会在执行期被log记录(DEBUG LEVEL).你也可以注册一个ConnectionListener (参看ProxoolFacade)得到这些信息. verbose: 详细信息设置。 参数 bool 值
2.web.xml配置,因为要先加载proxool.xml,所以需要将监听器改成servlet模式
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 改成 <servlet> <servlet-name>contextConfigLocation</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> web.xml配置如下: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml Spring 容器启动监听器 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> --> <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/classes/spring/proxool.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>contextConfigLocation</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.util.MyOpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value><!-- 强制进行转码 --> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:**/spring/*bean*.xml </param-value> </context-param> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!-- Spring MVC 的Servlet,它将加载WEB-INF/spring/spring-mvc.xml 的 配置文件,以启动Spring MVC模块--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/springMVC-mvc.xml</param-value> </init-param> <init-param> <param-name>namespace</param-name> <param-value>mvc</param-value> </init-param> <load-on-startup>4</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!-- 这里可以用 / 但不能用 /* ,拦截了所有请求会导致静态资源无法访问,所以要在spring3-servlet.xml中配置mvc:resources --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- webservice --> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> <load-on-startup>3</load-on-startup > </servlet > <servlet-mapping> <servlet-name>AxisServlet</servlet-name > <url-pattern>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>WEB-INF/classes/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <display-name /> <context-param> <description>Flag whether the report resources can only be accessed under the working folder. Defaults to false</description> <param-name>WORKING_FOLDER_ACCESS_ONLY</param-name> <param-value>false</param-value> </context-param> <context-param> <description>Temporary document files directory. Defaults to ${Context Root}/documents</description> <param-name>BIRT_VIEWER_DOCUMENT_FOLDER</param-name> <param-value>WEB-INF/report-engine/documents</param-value> </context-param> <context-param> <description>Flag whether to allow server-side printing. Possible values are ON and OFF. Defaults to ON</description> <param-name>BIRT_VIEWER_PRINT_SERVERSIDE</param-name> <param-value>ON</param-value> </context-param> <context-param> <description>Memory size in MB for creating a cube.</description> <param-name>BIRT_VIEWER_CUBE_MEMORY_SIZE</param-name> <param-value /> </context-param> <context-param> <description>Directory where to store all the birt report script libraries (JARs).. Defaults to ${Context Root}/scriptlib</description> <param-name>BIRT_VIEWER_SCRIPTLIB_DIR</param-name> <param-value>WEB-INF/report-engine/scriptlib</param-value> </context-param> <context-param> <description>Temporary images/charts directory. Defaults to ${Context Root}/report/images</description> <param-name>BIRT_VIEWER_IMAGE_DIR</param-name> <param-value>WEB-INF/report-engine/images</param-value> </context-param> <context-param> <description>BIRT viewer extended configuration file</description> <param-name>BIRT_VIEWER_CONFIG_FILE</param-name> <param-value>WEB-INF/report-engine/viewer.properties</param-value> </context-param> <context-param> <description>Preview report rows limit. An empty value means no limit.</description> <param-name>BIRT_VIEWER_MAX_ROWS</param-name> <param-value /> </context-param> <context-param> <description>Max cube fetch rows levels limit for report preview (Only used when previewing a report design file using the preview pattern). Defaults to return all levels</description> <param-name>BIRT_VIEWER_MAX_CUBE_ROWLEVELS</param-name> <param-value /> </context-param> <context-param> <description>Default locale setting</description> <param-name>BIRT_VIEWER_LOCALE</param-name> <param-value>en-US</param-value> </context-param> <context-param> <description>Max cube fetch columns levels limit for report preview (Only used when previewing a report design file using the preview pattern). Defaults to return all levels</description> <param-name>BIRT_VIEWER_MAX_CUBE_COLUMNLEVELS</param-name> <param-value /> </context-param> <context-param> <description>Report resources(design files or document files) directory for preview. Defaults to ${Context Root}</description> <param-name>BIRT_VIEWER_WORKING_FOLDER</param-name> <param-value /> </context-param> <context-param> <description>Report Engine logs directory. Default to ${Context Root}/logs</description> <param-name>BIRT_VIEWER_LOG_DIR</param-name> <param-value>WEB-INF/report-engine/logs</param-value> </context-param> <context-param> <description>Resource location directory (library files, images files or others). Defaults to ${Context Root}</description> <param-name>BIRT_RESOURCE_PATH</param-name> <param-value /> </context-param> <context-param> <description>Flag whether to force browser-optimized HTML output. Defaults to true</description> <param-name>HTML_ENABLE_AGENTSTYLE_ENGINE</param-name> <param-value>true</param-value> </context-param> <context-param> <description>Report engine log level. (ALL|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST|OFF)</description> <param-name>BIRT_VIEWER_LOG_LEVEL</param-name> <param-value>WARNING</param-value> </context-param> <listener> <listener-class>org.eclipse.birt.report.listener.ViewerHttpSessionListener</listener-class> </listener> <listener> <listener-class>org.eclipse.birt.report.listener.ViewerServletContextListener</listener-class> </listener> <servlet> <servlet-name>EngineServlet</servlet-name> <servlet-class>org.eclipse.birt.report.servlet.BirtEngineServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet> <servlet-name>ViewerServlet</servlet-name> <servlet-class>org.eclipse.birt.report.servlet.ViewerServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ViewerServlet</servlet-name> <url-pattern>/frameset</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>EngineServlet</servlet-name> <url-pattern>/document</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>EngineServlet</servlet-name> <url-pattern>/download</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>EngineServlet</servlet-name> <url-pattern>/parameter</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>EngineServlet</servlet-name> <url-pattern>/extract</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ViewerServlet</servlet-name> <url-pattern>/run</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>EngineServlet</servlet-name> <url-pattern>/preview</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>EngineServlet</servlet-name> <url-pattern>/output</url-pattern> </servlet-mapping> <jsp-config> <taglib> <taglib-uri>/birt.tld</taglib-uri> <taglib-location>/WEB-INF/report-engine/tlds/birt.tld</taglib-location> </taglib> </jsp-config> </web-app>
3.dataSource.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中 的sessionFactory不会被注入 --> <bean id="defaultSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.default_pool</value> </property> </bean> <bean id="theSecondSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.Second_pool</value> </property> </bean> <bean id="dataSource" class="org.util.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="default" value-ref="defaultSource"/> <entry key="theSecond" value-ref="theSecondSource"/> </map> </property> <property name="defaultTargetDataSource" ref="defaultSource"/> </bean> <!--定义了Hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 配置Hibernate的参数 --> <property name="hibernateProperties"> <props> <!-- 指定数据库的方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <!-- 表示允许自动提交 --> <prop key="hibernate.connection.autocommit">true</prop> <prop key="hibernate.show_sql">false</prop> <!-- JDBC执行批量更新语句的大小 清除缓存(定期清除缓存,减小压力 --> <prop key="hibernate.jdbc.batch_size">50</prop> <!-- <prop key="hibernate.cache.use_query_cache">true</prop>--> <!-- <prop key="hibernate.cache.use_second_level_cache">true</prop>--> <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>--> </props> </property> <property name="mappingLocations"> <list> <value>classpath*:org/vo/*.hbm.xml</value> <!-- <value>classpath:org/website/vo/persale/*.hbm.xml</value> --> </list> </property> </bean> <!-- 自动扫描组件,这里要把web下面的 controller去除,他们是在springMVC-mvc.xml中配置的, 如果不去除会影响事务管理的。--> <context:component-scan base-package="org.vo"> <context:exclude-filter type="regex" expression="org.controller.*"/> </context:component-scan> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager" /> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="execute*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="queryt*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Dao</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> <bean id="baseDao" class="org.dao.impl.BaseDaoImpl" init-method="init" lazy-init="true"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 读取配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>/WEB-INF/classes/jdbc.properties</value> </property> </bean> <bean id="basicService" class="org.service.BasicService"> <property name="baseDao"> <ref bean="baseDao"/> </property> </bean> </beans>