Spring3.x中配置DBCP2.0数据库连接池

 maven工程pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tliu</groupId>
    <artifactId>case</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>case Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.2.9.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>0.2.25</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.3.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.kenai.nbpwr</groupId>
            <artifactId>javax-inject</artifactId>
            <version>1.0-201002241208</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>
    <build>       
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7.2</version>
                <configuration>
                    <forkMode>once</forkMode>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <configuration>
                    <show>private</show>
                    <nohelp>true</nohelp>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

数据库的属性配置文件ds.properties

oracle.driver = oracle.jdbc.driver.OracleDriver
oracle.url = jdbc:oracle:thin:@127.0.0.1:1521:ORCL
oracle.username = PREM
oracle.password = 123456
dbcp2.maxIdle = 10
dbcp2.minIdle = 5
dbcp2.initialSize = 20
dbcp2.timeBetweenEvictionRunsMillis = 60000
dbcp2.poolPreparedStatements = true
dbcp2.maxOpenPreparedStatements = 50
dbcp2.removeAbandonedTimeout = 180
dbcp2.testWhileIdle = true
dbcp2.testOnBorrow = false
dbcp2.testOnReturn = false
dbcp2.validationQuery = SELECT 1 FROM DUAL

 spring的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:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"    
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:jee="http://www.springframework.org/schema/jee"    
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd        
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">  

    <!--读取外部的属性文件配置参数 -->
    <context:property-placeholder location="classpath:ds.properties" />  
 
    <bean id="BasicDataSource" class="org.apache.commons.dbcp2.BasicDataSource">       
        <!-- 数据库基本信息配置 -->        
        <property name="url" value="${oracle.url}"/>      
        <property name="driverClassName" value="${oracle.driver}"/>        
        <property name="username" value="${oracle.username}"/>        
        <property name="password" value="${oracle.password}"/>  
              
        <property name="maxIdle" value="${dbcp2.maxIdle}"/>        
        <property name="minIdle" value="${dbcp2.minIdle}"/>        
        <property name="initialSize" value="${dbcp2.initialSize}"/>        
        <property name="timeBetweenEvictionRunsMillis" 
                                        value="${dbcp2.timeBetweenEvictionRunsMillis}"/> 
       
        <property name="poolPreparedStatements" value="${dbcp2.poolPreparedStatements}"/>        
        <property name="maxOpenPreparedStatements" value="${dbcp2.maxOpenPreparedStatements}"/>        
        <property name="removeAbandonedTimeout" value="${dbcp2.removeAbandonedTimeout}"/>

        <!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->        
        <property name="testOnBorrow" value="${dbcp2.testOnBorrow}"/>

        <!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能  -->        
        <property name="testOnReturn" value="${dbcp2.testOnReturn}"/>

        <!-- 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于
             timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。  -->               
        <property name="testWhileIdle" value="${dbcp2.testWhileIdle}"/>

        <!-- 用来检测连接是否有效的sql,要求是一个查询语句,如果validationQuery为
             null,testOnBorrow、testOnReturn、testWhileIdle都不起其作用。 -->            
        <property name="validationQuery" value="${dbcp2.validationQuery}"/>    
    </bean>      
</beans>

Spring3.x中配置DBCP2.0数据库连接池

时间: 2024-12-15 04:55:37

Spring3.x中配置DBCP2.0数据库连接池的相关文章

Tomcat6.0数据库连接池配置

http://blog.163.com/magicc_love/blog/static/185853662201111101130969/ oracle驱动包Tomcat 6.0配置oracle数据库连接池 安装Tomcat后,在我的电脑-属性—>高级-->环境变量系统变量中添加以下环境变量(假定你的tomcat安装在c:\tomcat); CATALINA_HOME: c:\tomcat CATALINA_BASE: c:\tomcat TOMCAT_HOME: c:\tomcat 然后修改

第77节:Java中的事务和数据库连接池和DBUtiles

前言 看哭你,字数:8803,承蒙关照,谢谢朋友点赞! 事务 Transaction事务,什么是事务,事务是包含一组操作,这组操作里面包含许多个单一的逻辑,只要有一个逻辑没有执行成功就算失败,导致回滚就是指所有的数据都会回到最初的状态. 有事务,是为了保证逻辑一定要成功,如银行转账. 回顾一下 什么是jsp,jsp的三大指令. page: 定义当前页面的信息 include: 包含其他页面 taglib: 引入标签库 三大动作标签: <jsp:forward page="">

Tomcat中配置数据源和连接池

(1)为什么需要配置数据源和连接池? 我们知道在每次java程序俩接数据库的时候我们都需要请求连接数据库然后打开读取数据然后关闭, 这样使得每一个用户访问的时候都需要服务器做出相应,这样的话服务器端承受巨大的压力,如此效率就会下降, 为了解决这个问题我们可以让数据库提前打开连接等待用户连接当有用户连接的时候,就把数据库已存在的连接 给用户即可 而我们就把这条连接叫做"连接池"  当连接池中的连接足够的话 就分给用户,如果不够用户则在"|队列池"中等待: (2)实现过

Spring3 Security 中配置会话管理

账户登录时,要求一个账户同时只能一人登录,配置中的步骤有三个: 1.在web.xml中配置HttpSessionEventPublisher <listener> <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> </listener> 2.在security.xml中配置session management

在Eclipse中配置Tomcat7.0

为了在Eclipse中进行struts2的测试,才发现自己机器上的Eclipse没有集成Tomcat,在网上找了半天,不是这个插件没有下载地址,就是那个有好多注意事项或者版本问题.结果,自己到tomcatforEclipse的插件的网上直接下载了一个,按照提供的说明一下子就配成了.网上的那么多配置文章,唉,不说啥了.看看我的配置步骤吧. 1,Eclipse的安装.这个是从Eclipse的官方网站下载的.不用教吧,下载后exe文件执行安装. 2,下载一个tomcat,从apache的官方网站上下载

在eclipse中配置spark 0.9.1源码的开发环境

一.准备工作: 1.下载release版spark 0.9.1,本次用的版本是spark-0.9.1-bin-cdh4. 2.下载scala IDE 3.0.2,这个版本ide的支持scala 2.10版. 二.配置工作 1.在ide中新建scala项目,注意jre要用jdk1.7版的.如下图: 2.找到spark-0.9.1-bin-cdh4\core\src\main,目录下java.resources.scala三个目录,如下图 找到spark-0.9.1-bin-cdh4\repl\sr

【MySQL】JavaWeb项目中配置数据库的连接池

在META-INF目录下新建context.xml <?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="10" maxIdle="4&

详解CentOS 7 中配置RAID 0 、RAID 1、RAID 5(理论+实践)

RAID介绍 RAID全称是独立磁盘冗余阵列(Redundant Array of Independent Disks),基本思想是把多个磁盘组合起来,组合一个磁盘阵列组,使得性能大幅提高.最初是为了组合小的廉价磁盘来代替大的昂贵磁盘,同时希望磁盘失效时不会使对数据的访问受损失而开发出一定水平的数据保护技术. RAID可以充分发挥出多块硬盘的优势,可以提升硬盘速度,增大容量,提供容错功能够确保数据安全性,易于管理的优点,在任何一块硬盘出现问题的情况下都可以继续工作,不会 受到损坏硬盘的影响. R

Ubuntu18.04中配置wxWidget3.0.4开发环境

准备工作 在 https://www.wxwidgets.org/downloads/ 下载最新的稳定版 wxWidgets-3.0.4.tar.bz2 安装依赖 sudo apt install libgtk-3-dev build-essential checkinstall 编译安装 $ make # ... 略 ... $ sudo checkinstall checkinstall会需要准备好package的描述等, 详细的输入输出. 使用checkinstall会同时生成deb包,