spring(16)------spring的数据源配置

来源:互联网 发布:ctp交易接口源码 编辑:程序博客网 时间:2024/06/07 05:10

在spring中,通过XML的形式实现数据源的注入有三种形式。

一,使用spring自带的DriverManagerDataSource

使用DriverManagerDataSource配置数据源与直接使用jdbc在效率上没有多大的区别,使用DriverManagerDataSource配置数据源

的代码实例如下,这里重点研究spring的数据源配置,就采用spring编程式事务处理来来研究数据源的配置。

所需要的jar包和spring编程式配置:http://blog.csdn.net/yhl_jxy/article/details/51167351

(1)HelloDAO类:

package com.lanhuigu.spring.dao;import javax.sql.DataSource;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.DefaultTransactionDefinition;import org.springframework.transaction.support.TransactionCallback;import org.springframework.transaction.support.TransactionCallbackWithoutResult;import org.springframework.transaction.support.TransactionTemplate;public class HelloDAO {private DataSource dataSource;private PlatformTransactionManager transactionManager;//通过set方式注入public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}public void setTransactionManager(PlatformTransactionManager transactionManager) {this.transactionManager = transactionManager;}//=============编程式事务处理的3种编写形式====================//第一种编写public int create(String msg) {System.out.println(msg+"开始!");//使用TransactionTemplate进行事务处理编程TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);//调用TransactionTemplate的execute方法Object result = transactionTemplate.execute(new TransactionCallback() {//覆盖TransactionCallback的doInTransaction()方法@Overridepublic Object doInTransaction(TransactionStatus status) {// TODO Auto-generated method stub//在该方法内对数据库进行增加操作System.out.println("添加对象进行");JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);//第一次执行不打开,第二次执行打开//jdbcTemplate.update("insert into t_user_main values(2,'test',24)");jdbcTemplate.update("insert into t_user_main values(1,'test',24)");System.out.println("添加对象结束");return null;}});return 0;}//第二种编写/*public int create(String msg) {System.out.println(msg+"开始!");DefaultTransactionDefinition dtd = new DefaultTransactionDefinition();TransactionStatus status = transactionManager.getTransaction(dtd);try {//使用JdbcTemplate与数据库进行交互System.out.println("添加对象进行");JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);jdbcTemplate.update("insert into t_user_main values(1,'test',24)");} catch (DataAccessException ex) {// TODO: handle exceptiontransactionManager.rollback(status);throw ex;} finally {transactionManager.commit(status);}return 0;}*///第三种编写/*public int create(String msg) {System.out.println(msg+"开始!");TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);transactionTemplate.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus status) {// TODO Auto-generated method stubJdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);jdbcTemplate.update("insert into t_user_main values(1,'test',24)");}});return 0;}*//*//============声明式事务编程============public int create(String msg) {System.out.println(msg+"开始");JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);//新增数据jdbcTemplate.update("insert into t_user_main values(1,'test',24)");System.out.println(msg+"结束");return 0;}*/}
(2)使用spring自带的DataSourceTransactionManager配置数据源:

<?xml version="1.0" encoding="UTF-8"?><!--  - Application context definition for JPetStore's business layer.  - Contains bean references to the transaction manager and to the DAOs in  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").  --><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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- spring编程式事务配置 --><!-- 1.配置数据源(dataSource) --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!-- oracle数据库驱动 --><property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property><!-- 数据库连接 --><property name="url"><value>jdbc:oracle:thin:@localhost:1521/XE</value></property><!-- 数据库用户名 --><property name="username"><value>system</value></property><!-- 数据库密码 --><property name="password"><value>123456</value></property></bean><!-- 2.设定transactionManager --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 这里配置的dataSource是DataSourceTransactionManager类中的属性,在类中有着属性的定义:private DataSource dataSource; 跟下面配置的HelloDAO中的dataSource不是用在一个地方,是一个东西用在两个地方--><property name="dataSource"><ref bean="dataSource"/></property></bean><!-- 3.配置事务管理目标类 --><bean id="helloDAO" class="com.lanhuigu.spring.dao.HelloDAO"><!-- dataSource属性 --><property name="dataSource"><ref bean="dataSource"/></property><!-- transactionManager属性 --><property name="transactionManager"><ref bean="transactionManager"/></property> </bean></beans>
(3)测试程序:

package com.lanhuigu.spring.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lanhuigu.spring.dao.HelloDAO;import com.lanhuigu.spring.impl.IUser;public class TestJDBCTemplate {@Testpublic void testJDBCTemplate(){//1.创建spring容器ApplicationContext actx =new ClassPathXmlApplicationContext("/applicationContext.xml");//2.编程式测试HelloDAO helloDAO = (HelloDAO) actx.getBean("helloDAO");helloDAO.create("添加对象");}}
(4)总结:

之所以说使用这种方式跟jdbc直接操作数据库效率上没有多大区别,是因为DriverManagerDataSource建立连接是只要有连接就新建一个connection,

根本没有连接池的作用,这样做的话,数据库一直开连接,数据库崩掉是早晚的事。

二,使用DBCP配置连接池

需要增加jar包如下:

在文件夹spring-framework-2.5.6\lib\jakarta-commons下的commons-collections.jar,commons-dbcp.jar,commons-pool.jar。

修改上面使用上面DriverManagerDataSource配置数据源的配置文件,修改为DBCP配置数据源的方式,修改后的xml如下,其他不变:

<?xml version="1.0" encoding="UTF-8"?><!--  - Application context definition for JPetStore's business layer.  - Contains bean references to the transaction manager and to the DAOs in  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").  --><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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- spring编程式事务配置 --><!-- 1.配置数据源(dataSource) --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"                destroy-method="close"><!-- oracle数据库驱动 --><property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property><!-- 数据库连接 --><property name="url"><value>jdbc:oracle:thin:@localhost:1521/XE</value></property><!-- 数据库用户名 --><property name="username"><value>system</value></property><!-- 数据库密码 --><property name="password"><value>123456</value></property><!-- 最大连接数据库连接数,设置为0时,表示没有限制 --><property name="maxActive"><value>255</value></property><!-- 最大等待连接中的数量,设置为0时,表示没有限制 --><property name="maxIdle"><value>2</value></property><!-- 最大等待秒数,单位为毫秒, 超过时间会报出错误信息 --><property name="maxWait"><value>50000</value></property></bean><!-- 2.设定transactionManager --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 这里配置的dataSource是DataSourceTransactionManager类中的属性,在类中有着属性的定义:private DataSource dataSource; 跟下面配置的HelloDAO中的dataSource不是用在一个地方,是一个东西用在两个地方--><property name="dataSource"><ref bean="dataSource"/></property></bean><!-- 3.配置事务管理目标类 --><bean id="helloDAO" class="com.lanhuigu.spring.dao.HelloDAO"><!-- dataSource属性 --><property name="dataSource"><ref bean="dataSource"/></property><!-- transactionManager属性 --><property name="transactionManager"><ref bean="transactionManager"/></property> </bean></beans>
注意:当你运行程序时,报错的话,是HelloDAO中

jdbcTemplate.update("insert into t_user_main values(1,'test',24)");插入数据id重复导致的,

换个id或删掉数据库表的数据,然后再运行。

关于DBCP的总结:

1. 首先得了解配置文件中的属性:

BasicDataSource提供close()方法关闭数据源,一定记得设置destroy-method=”close”,以便spring容器关闭时,

数据源能够得到正常的关闭。还有很多常用的property属性如下:

defaultAutoCommit:设置从数据源中返回的连接是否采用自动提交的机制,默认为true;

defaultReadOnly:设置数据源是否仅能执行只读操作,默认为false;

maxActive:最大连接数据库连接数,设置为0时,表示没有限制;

mmaxIdle:最大等待连接中的数量,设置为0时,表示没有限制;

maxWait:最大等待秒数,单位为毫秒,超时报错;

validationQuery:用于验证连接是否成功的查询SQL语句,SQL语句必须至少要返回一行数据;

removeAbandoned:是否自我中断,默认是false;

removeAbandonedTimout:几秒后数据库连接会自动断开,在removeAbandoned为true,提供该值;

logAbandoned:是否记录中断事件,默认为false;

2.使用连接池
使用这种方式的好处在于数据库连接时建立了连接池。连接池的好处就是当你建立连接时,从连接池中找,找到就使用已有连接,

找不到新建连接,类推到最大连接数时,如果没有空闲可用连接,就等待,用完就释放到连接池,做到资源使用最大化,不会

无限制的建立连接,把数据库搞崩了。

三,使用C3P0连接数据源

需要加入c3p0的jar包,这里使用c3p0-0.9.1.2.jar,修改spring数据源配置文件。

<?xml version="1.0" encoding="UTF-8"?><!--  - Application context definition for JPetStore's business layer.  - Contains bean references to the transaction manager and to the DAOs in  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").  --><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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- spring编程式事务配置 --><!-- 1.配置数据源(dataSource) --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><!-- oracle数据库驱动 --><property name="driverClass"><value>oracle.jdbc.driver.OracleDriver</value></property><!-- 数据库连接 --><property name="jdbcUrl"><value>jdbc:oracle:thin:@localhost:1521/XE</value></property><!-- 数据库用户名 --><property name="user"><value>system</value></property><!-- 数据库密码 --><property name="password"><value>123456</value></property></bean><!-- 2.设定transactionManager --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 这里配置的dataSource是DataSourceTransactionManager类中的属性,在类中有着属性的定义:private DataSource dataSource; 跟下面配置的HelloDAO中的dataSource不是用在一个地方,是一个东西用在两个地方--><property name="dataSource"><ref bean="dataSource"/></property></bean><!-- 3.配置事务管理目标类 --><bean id="helloDAO" class="com.lanhuigu.spring.dao.HelloDAO"><!-- dataSource属性 --><property name="dataSource"><ref bean="dataSource"/></property><!-- transactionManager属性 --><property name="transactionManager"><ref bean="transactionManager"/></property> </bean></beans>
使用总结:

ComboPooledDataSource和BasicDataSource一样都提供一个当spring容器关闭时用于数据源正确关闭的close()方法。

C3P0拥有比DBCP更丰富的配置属性,通过这些属性,可以对数据源进行各种有效的控制: 
acquireIncrement:当连接池中的连接用完时,C3P0一次性创建新连接的数目; 
acquireRetryAttempts:定义在从数据库获取新连接失败后重复尝试获取的次数,默认为30;

acquireRetryDelay:两次连接中间隔时间,单位毫秒,默认为1000; 
autoCommitOnClose:连接关闭时默认将所有未提交的操作回滚。默认为false; 
automaticTestTable: C3P0将建一张名为Test的空表,并使用其自带的查询语句进行测试。

如果定义了这个参数,那么属性preferredTestQuery将被忽略。

你不能在这张Test表上进行任何操作,它将中为C3P0测试所用,默认为null; 
 breakAfterAcquireFailure:获取连接失败将会引起所有等待获取连接的线程抛出异常。但是数据源仍有效保留,

并在下次调   用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。默认为 false; 
checkoutTimeout:当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,

如设为0则无限期等待。单位毫秒,默认为0; 
connectionTesterClassName: 通过实现ConnectionTester或QueryConnectionTester的类来测试连接,类名需设置为全限定名。默认为 com.mchange.v2.C3P0.impl.DefaultConnectionTester; 
idleConnectionTestPeriod:隔多少秒检查所有连接池中的空闲连接,默认为0表示不检查; 
initialPoolSize:初始化时创建的连接数,应在minPoolSize与maxPoolSize之间取值。默认为3; 
maxIdleTime:最大空闲时间,超过空闲时间的连接将被丢弃。为0或负数则永不丢弃。默认为0; 
maxPoolSize:连接池中保留的最大连接数。默认为15; 
maxStatements:JDBC的标准参数,用以控制数据源内加载的PreparedStatement数量。

但由于预缓存的Statement属 于单个Connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素,

如果maxStatements与 maxStatementsPerConnection均为0,则缓存被关闭。默认为0; 
maxStatementsPerConnection:连接池内单个连接所拥有的最大缓存Statement数。默认为0; 
numHelperThreads:C3P0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能,

通过多线程实现多个操作同时被执行。默认为3; 
preferredTestQuery:定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个参数能显著提高测试速度。

测试的表必须在初始数据源的时候就存在。默认为null; 
propertyCycle: 用户修改系统配置参数执行前最多等待的秒数。默认为300; 
testConnectionOnCheckout:因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都 将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 
等方法来提升连接测试的性能。默认为false; 
testConnectionOnCheckin:如果设为true那么在取得连接的同时将校验连接的有效性。默认为false。
四,JNDI

这种方式需要在Tomcat中server.xml中配置数据源,也就是把上面的数据配置放在server.xml中,同时需要在spring配置文件中进行jndi配置,非常麻烦,

一般不用。

0 0
原创粉丝点击