<!-- 配置C3P0连接池 -->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring3_day02"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property> </bean>

配置好连接池之后把它注入到JDBC模板.

21:55:24,695  INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [applicationContext.xml]
21:55:24,969 INFO GenericApplicationContext:510 - Refreshing org.springframework.context.support.GenericApplicationContext@43401ab6: startup date [Fri May 05 21:55:24 CST 2017]; root of context hierarchy
21:55:25,127 INFO DefaultListableBeanFactory:577 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@cf7e5b2: defining beans [dataSource,dataSource1,dataSource2,jdbcTemplate,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
21:55:25,170 INFO DriverManagerDataSource:153 - Loaded JDBC driver: com.mysql.jdbc.Driver
21:55:25,209 INFO MLog:80 - MLog clients using log4j logging.
21:55:25,394 INFO C3P0Registry:204 - Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
21:55:25,564 INFO AbstractPoolBackedDataSource:462 - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 2ygg1c9n1o6hot718sug49|4706c7b2, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 2ygg1c9n1o6hot718sug49|4706c7b2, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql:///spring3_day02, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
21:55:25,975 INFO GenericApplicationContext:1042 - Closing org.springframework.context.support.GenericApplicationContext@43401ab6: startup date [Fri May 05 21:55:24 CST 2017]; root of context hierarchy
21:55:25,975 INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@cf7e5b2: defining beans [dataSource,dataSource1,dataSource2,jdbcTemplate,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

初始化C3P0的连接池等等的一些信息.


<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入beans的头 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Spring默认的连接池 -->
<!-- 这个类由Spring来帮我们创建,它默认情况下只创建一次,因为是单例的. -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring3_day02"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property> </bean>
<!-- 配置DBCP连接池 -->
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring3_day02"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property> </bean> <!-- 配置C3P0连接池 -->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring3_day02"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property> </bean>
<!-- 定义jdbctemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource2"></property><!-- 把上面定义好的连接池注入进来了 -->
</bean>
</beans>
package cn.itcast.spring3.demo1;

import java.sql.DriverManager;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.jta.SpringJtaSynchronizationAdapter;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 { @Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;//注入Jdbc模板
@Test
public void demo2(){
jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
} @Test
public void demo1(){
// 创建连接池:
DriverManagerDataSource dataSource = new DriverManagerDataSource();//Spring自带的连接池
// 设置参数:
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring3_day02");
dataSource.setUsername("root");
dataSource.setPassword(""); //使用JDBC的模板:
//JdbcTemplate jdbcTemplate = new JdbcTemplate();
//jdbcTemplate.setDataSource(dataSource);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
}
}

day39-Spring 15-Spring的JDBC模板:C3P0连接池配置的更多相关文章

  1. Spring之c3p0连接池配置和使用

    1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...

  2. 【JDBC】C3P0连接池的使用

    C3P0连接池的c3p0-config.xml配置文件 <?xml version="1.0" encoding="UTF-8"?> <c3p ...

  3. C3p0连接池配置

    在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ②连接数据库(Connection co ...

  4. C3P0连接池配置(C3P0Utils.java)

    配置文件 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3p0-confi ...

  5. (30)java web的hibernate使用-c3p0连接池配置

    hibernate支持c3p0连接池 需要导入c3p0的jar包 <!-- 配置连接驱动管理类 --> <property name="hibernate.connecti ...

  6. Spring c3p0连接池配置

    数据库连接池 数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”.预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去.我们可以通过设定连接 ...

  7. spring hibernate4 c3p0连接池配置

    c3p0-0.9.1.2.jar,c3p0-oracle-thin-extras-0.9.1.2.jar,点此下载 <bean id="dataSource" class=& ...

  8. C3P0连接池配置方式

    c3p0的配置方式分为三种,分别是 1.setters一个个地设置各个配置项 2.类路径下提供一个c3p0.properties文件 3.类路径下提供一个c3p0-config.xml文件 1.set ...

  9. C3P0连接池配置和实现详解

    一.配置 <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> ...

随机推荐

  1. 19-10-19-I

    中午考试困够呛. T1 我想打矩阵快速幂,然后我咕了 T2 打T1了所以又咕了. T3 每一个黑点更新答案只有两种方式: 更新子树. 更新父链上的兄弟,叔伯,…… 于是: 把树拍在$DFS$序上. 更 ...

  2. redis教程(三)-----redis缓存雪崩、缓存穿透、缓存预热

    缓存雪崩 概念 缓存雪崩是由于原有缓存失效(过期),新缓存未到期间.所有请求都去查询数据库,而对数据库CPU和内存造成巨大压力,严重的会造成数据库宕机.从而形成一系列连锁反应,造成整个系统崩溃. 解决 ...

  3. mysql api 不支持source命令

    今天写了个代码 <?php ..... mysql_query("source /tmp/cr.sql",$link); ..... ?> 结果死活cr.sql的SQL ...

  4. tortoisegit如何删除远程分支

    图片来自:https://zhidao.baidu.com/question/134542616148384045.html

  5. javascript基础:dom

    Dom: * 概念:Document Object  Model    文档对象模型 * 将标记语言文档的各个组成部分,封装成对象,可以使用这些对象,对标记语言文档进行CRUD的动态操作 *    D ...

  6. Redis源码解析:16Resis主从复制之主节点的完全重同步流程

    主从复制过程中,主节点根据从节点发来的命令执行相应的操作.结合上一章中讲解的从节点在主从复制中的流程,本章以及下一篇文章讲解一下主节点在主从复制过程中的流程. 本章主要介绍完全重同步流程. 一:从节点 ...

  7. Crontab 入门

    参考网址: http://www.centoscn.com/CentOS/help/2014/0820/3524.html 简单命令 service crond restart //重启crontab ...

  8. http请求生命周期流程

    https://mp.weixin.qq.com/s/fpA2CThk2L-YBw6z0k4rtw HTTP 请求/相应 1.客户端连接到Web服务器 一个HTTP客户端,通常是浏览器,与Web服务器 ...

  9. windows服务器nginx日志分割

    编写一个bat文件 @echo off rem @echo off rem 取1天之前的日期 echo wscript.echo dateadd(,date) >%tmp%\tmp.vbs fo ...

  10. proteus 8.8 直装版提示Symbol $MKRORIGIN used but not found in libraries 安装后没有库

    用管理员运行程序,然后再通过菜单打开仿真文件是没问题. 解决方法:通常的安装目录是C:\Program Files (x86)\Labcenter Electronics\Proteus 8 Prof ...