原文链接

数据源

通过jdbc连接数据库,多建立几条连接放在数据源里面。可以设置数据源的最大连接数,同时活跃的连接数,最少空闲的连接数,能够同时接收处理的连接数等等。

dbcp数据源

需要的jar包:

commons-dbcp.jar;
commons-pool.jar
mysql-connector-java.jar

前两个jar包到阿帕奇官网下载。第三个jar包到mysql官网下载。

他是对jdbc的封装。底层还是要通过jdbc连接数据库的。实现了java.sql.DataSource接口的类有BasicDataSourceFactory和BasicDataSource。

这两个类是启动database的。

其中BasicDataSourceFactory底层实际上是先创建了BasicDataSource的对象,然后给BasicDataSource对象

进行分钟相关属性的赋值。比如username,password,url,driverClassName等等。BasicDataSource则是直接通过set方法对这些属性直接赋值。

通过getConnection方法获取数据库连接(Connection接口)。之后的操作就和jdbc一样了。

C3P0数据源

需要的jar包

c3p0-0.9.5.2.jar
mchange-commons-java-0.2.11.jar
mysql-connector-java-5.1.40-bin.jar

这些资源网上很多一找一大片。

(一)这个类(ComboPooledDataSource)是启动datadase数据源的

   ComboPooledDataSource source = new ComboPooledDataSource();
   source.setDriverClass("com.mysql.jdbc.Driver");
source.setUser("root");
source.setPassword("1213265442");
source.setJdbcUrl("jdbc:mysql://localhost:3306/user");

通过set方法即可设置数据池的相关参数(最大连接数,同时活跃的连接数,最少空闲的连接数,能够同时接收处理的连接数等等)

(二)ComboPooledDataSource source = new ComboPooledDataSource("intergalactoApp");

编写c3p0-config.xml文件放在src目录下面。内容如下(注意加黑的部分)

 <c3p0-config>
<!-- This app is massive! -->
<named-config name="intergalactoApp">
<property name="acquireIncrement">50</property> <!--连接池在无空闲连接可用时一次性创建的新数据库连接数 -->
<property name="initialPoolSize">100</property> <!--连接池初始化时创建的连接数-->
<property name="minPoolSize">50</property> <!--连接池最少保持的可用连接数->
<property name="maxPoolSize">1000</property> <!--连接池最多保持的可用连接数-> <property name="user">root</property>
<property name="password">1213265442</property>
<property name="driverClassName">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/user</property>
</named-config>
</c3p0-config>

通过source.getConnection()的方法就可以得到Connection接口对象。剩下的就是jdbc的操作了。

通过hibernate和datasource连接

我使用的是hibernate4.3当前稳定的最高版本。查看project/etc/hibernate.proterties文件,这个文件是hibernate所有的的配置了。

查看可知

hibernate支持C3P0ConnectionProvider,ProxoolConnectionProvider,DatasourceConnectionProvider,DriverManagerConnectionProvider这四种jdbc连接池

并不包括dbcp,但是支持c3p0。下面我们来给hibernate配置C3P0.

需要的jar包

c3p0-0.9.2.2.jar
mchange-commons-java-0.2.11.jar
mysql-connector-java-5.1.40-bin.jar
hibernate-c3p0-4.3.5.Final

还有一些hibernate的必须包,在required文件夹下面。

hibernate.cfg.xml配置文件中加入

<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>    

这就代表了不使用hibernate自己的连接池,而是使用我们自己的C3P0连接池    
使用hibernate和c3p0做数据库的数据源前面都要加上“hibernate.c3p0”

<property name="hibernate.c3p0.idle_test_period">3000</property>

有关于jdcb的连接还是按照hibernate的jdbc连接。而有关c3p0数据源的属性配置比如启动多少个connection连接,属性前面都要加上“hibernate.c3p0”就像这样

其他的就跟正常的hibernate配置一样了。其实我们就是把hibernate自带的数据池换成了我们自己的。其他都一样。

1、 Install Your JDBC Driver

Use of the JDBC Data Sources JNDI Resource Factory requires that you make an appropriate JDBC driver available to both Tomcat internal classes and to your web application. This is most easily accomplished by installing the driver’s JAR file(s) into the $CATALINA_HOME/lib directory, which makes the driver available both to the resource factory and to your application. 

2、Declare Your Resource Requirements

Next, modify the web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will look up preconfigured data source. By convention, all such names should resolve to the jdbc subcontext (relative to the standard java:comp/env naming context that is the root of all provided resource factories. A typical web.xmlentry might look like this:

 <resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the <Context>
configuration for the web application.
</description>
<res-ref-name>
jdbc/EmployeeDB
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>

3、 Code Your Application’s Use Of This Resource

     Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
use this connection to access the database ...
conn.close();

4、To configure Tomcat’s resource factory, add an element like this to the <Context>element for the web application.

 <Context >
<Resource name="jdbc/EmployeeDB"
auth="Container"
type="javax.sql.DataSource"
username="dbusername"
password="dbpassword"
driverClassName="org.hsql.jdbcDriver"
url="jdbc:HypersonicSQL:database"
maxTotal="8"
maxIdle="4"/>
</Context>

JAVA高级编程数据源datasource的更多相关文章

  1. java高级编程笔记(四)

    java的Object类: 1.Object 类位于 java.lang 包中,编译时会自动导入:Java 的所有类都继承了 Object,子类可以使用 Object 的所有方法. 2.Object ...

  2. JAVA高级编程(数据源datasource)

    数据源:通过jdbc连接数据库,多建立几条连接放在数据源里面.可以设置数据源的最大连接数,同时活跃的连接数,最少空闲的连接数,能够同时接收处理的连接数等等. dbcp数据源 需要的jar包: comm ...

  3. java高级编程-使用反射强制给private字段赋值

    转自:http://blog.csdn.net/yaerfeng/article/details/7103397 今天项目中遇到了一个问题,要调用一个类,并获取这个类的属性进行赋值然后将这个类传递到方 ...

  4. 【java高级编程】JDK和CGLIB动态代理区别

    转载:https://blog.csdn.net/yhl_jxy/article/details/80635012 前言 JDK动态代理实现原理(jdk8):https://blog.csdn.net ...

  5. 【java高级编程】jdk自带事件模型编程接口

    事件类 java.util.EventObject java.beans.PropertyChangeEvent 事件监听接口 java.util.EventListener java.beans.P ...

  6. java高级编程技巧

    1. boolean a= b==null;这句话很亮. public class Test { public static void main(String[] args) { String b=& ...

  7. Java学习进阶—高级编程

    当你已经熟练的掌握了面向对象中的各种概念后,是否会对这些知识是如何使用的产生浓厚的兴趣?本课程主要针对于已经掌握了JAVA核心开发技术的读者准备,讲解了JAVA多线程.常用类库.IO编程.网络编程.类 ...

  8. 对java高级程序员有益的十本书

    英文原文:http://www.programcreek.com/2013/08/top-books-for-advanced-level-java-developers/ java语言是当今最受欢迎 ...

  9. 2018.6.19 Java核心API与高级编程实践复习总结

    Java 核心编程API与高级编程实践 第一章 异常 1.1 异常概述 在程序运行中,经常会出现一些意外情况,这些意外会导致程序出错或者崩溃而影响程序的正常执行,在java语言中,将这些程序意外称为异 ...

随机推荐

  1. html标签的快捷

    https://www.jianshu.com/p/8f330e3571ee 一: <ul> <li><a href=""></a> ...

  2. IDE - IDEA - tab - 方法相关的移动

    1. 概述 标题可能会改 一个 tab 里方法相关的操作 2. 前提 以默认的模式编辑 tab 对我来说, 就关掉 vim 插件 3. 操作 1. 查看文件结构 概述 唤出当前文件的 结构 唤出后可以 ...

  3. P & R 8

    Floorplan: 要做好floorplan需要掌握哪些知识跟技能? 通常,遇到floorplan问题,大致的debug步骤跟方法有哪些? 如何衡量floorplan的QA? T:Block lev ...

  4. Unity 鼠标旋转物体360展示

    PC端 using UnityEngine; using System.Collections; public class DragRound : MonoBehaviour { public Tra ...

  5. idea选中文件时左侧菜单自动定位到文件所在位置

    一:设置成自动定位,如图: 二:点击项目工程目录上的阻击按钮,自动定位

  6. 安卓开发:打印Log

    在iOS开发中使用NSLog进行打印调试,在安卓中使用的是Log.v(tag, msg);等进行打印调试. 参考:[https://blog.csdn.net/salary/article/detai ...

  7. ajax传map,后端接收并解析

    前端let map = new Map(); map.set(1, 1); map.set(2, 2); map.set(3, 3); //map转obj let obj= Object.create ...

  8. 解决<%@taglib prefix="s" uri="/struts-tags"%>显示找不到

    问题: jsp中使用<%@taglib prefix="s" uri="/struts-tags"%>显示找不到 解决方法: 在web.xml中插入 ...

  9. 介绍Mobility Group

    Mobility或Roaming是无线客户端能够安全地从一个AP无缝关联到另一个AP的能力,并且延迟尽可能的短. 当无线客户端和AP关联并通过AP进行身份验证时,注册AP的WLC会将客户端条目放在自己 ...

  10. Jmeter_正则表达式提取器_提取单组数据

    1.用处:提取登录信息/获取session或者token数值 2.举例:获取登录结果的获取:msg":"登录成功" 这个数据 3.HTTP->后置处理器->正 ...