dataSource

本文介绍了Java中DataSource接口的概念及其实现方式,包括基本实现、连接池实现和分布式事务实现,并提供了MyDataSource、DBCP和C3P0的具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1   customizedDataSource

1.1 Introduction

DataSource

javax.sql
接口DataSource

所有超级接口:

CommonDataSource, Wrapper

public interface DataSource

extends CommonDataSource, Wrapper

该工厂用于提供到此 DataSource 对象所表示的物理数据源的连接。作为 DriverManager 工具的替代项,DataSource 对象是获取连接的首选方法。实现 DataSource 接口的对象通常在基于 JavaTM Naming andDirectory Interface (JNDI) API 的命名服务中注册。

DataSource 接口由驱动程序供应商实现。共有三种类型的实现:

1.基本实现- 生成标准的 Connection 对象

2.连接池实现- 生成自动参与连接池的 Connection 对象。此实现与中间层连接池管理器一起使用。

3.分布式事务实现- 生成一个 Connection 对象,该对象可用于分布式事务,大多数情况下总是参与连接池。此实现与中间层事务管理器一起使用,大多数情况下总是与连接池管理器一起使用。

DataSource 对象的属性在必要时可以修改。例如,如果将数据源移动到另一个服务器,则可更改与服务器相关的属性。其优点在于,由于可以更改数据源的属性,所以任何访问该数据源的代码都无需更改。

通过 DataSource 对象访问的驱动程序本身不会向 DriverManager 注册。通过查找操作获取 DataSource 对象,然后使用该对象创建 Connection 对象。使用基本的实现,通过 DataSource 对象获取的连接与通过 DriverManager 设施获取的连接相同。

从以下版本开始:

1.4

方法摘要

 Connection

getConnection()
          尝试建立与此 DataSource 对象所表示的数据源的连接。

 Connection

getConnection(String username, String password)
          尝试建立与此 DataSource 对象所表示的数据源的连接。

1.2 example

1.2.1   DataSource

public class MyDataSource implements DataSource{

   LinkedList<Connection> connlist=newLinkedList<Connection>();

   public MyDataSource(){

      Connectionconn;

      try {

         for(int i=0;i<3;i++){

            conn = JdbcUtils.getConnection();

            connlist.add(conn);

         }

      } catch (SQLException e) {

         e.printStackTrace();

      }

   }

   public ConnectiongetConnection() throws SQLException {

      return connlist.removeLast();

   }

public void release(Connection conn){

      connlist.add(conn);

   }

   public PrintWritergetLogWriter() throws SQLException {

      return null;

   }

   public void setLogWriter(PrintWriter out) throws SQLException {

   }

   public void setLoginTimeout(int seconds) throws SQLException {

   }

   public int getLoginTimeout() throws SQLException {

      return 0;

   }

   public LoggergetParentLogger() throwsSQLFeatureNotSupportedException {

      return null;

   }

   public <T> Tunwrap(Class<T> iface) throws SQLException {

      return null;

   }

   public booleanisWrapperFor(Class<?> iface) throws SQLException {

      return false;

   }

   public ConnectiongetConnection(String username, String password) throws SQLException {

      return null;

   }

}

1.2.2   test

public void testMyDataSource(){

      MyDataSourcemds=new MyDataSource();

      Connectionconn=null;

      PreparedStatementpstate=null;

      try{

         conn=mds.getConnection();

         String sql="updateuser set username=? where id=?";

         pstate=conn.prepareStatement(sql);

         pstate.setString(1, "mydatasource");

         pstate.setInt(2, 1);

         pstate.executeUpdate();

        

      }catch(Exception e){

         e.printStackTrace();

      }finally{

         mds.release(conn);

         JdbcUtils.release(conn, pstate);

      }

   }

2 DBCP

DBCP 是 Apach软件基金组织下的开源数据库连接池的实现,使用DBCP 数据源,应用程序需要在系统中增加两个jar文件

Commons-dbcp.jar : 连接池的实现

Commons-pool.jar : 连接池实现的依赖库

Tomcat 连接池正是采用这种连接池实现的。    该数据库连接池既可以和应用服务器整合,也可由应用程序独立使用。

2.1     手动配置

public void setDbcp(){

      BasicDataSourcebds=newBasicDataSource();

      bds.setDriverClassName("com.mysql.jdbc.Driver");

      bds.setUrl("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8");

      bds.setUsername("root");

      bds.setPassword("");

      Connectionconn=null;

      PreparedStatementpstate=null;

      try{

         conn=bds.getConnection();

         String sql="updateuser set username=? where id=?";

         pstate=conn.prepareStatement(sql);

         pstate.setString(1, "dbcp");

         pstate.setInt(2, 1);

         pstate.executeUpdate();

      }catch(Exception e){

         e.printStackTrace();

      }finally{

         JdbcUtils.release(conn, pstate);

      }

   }

2.2     配置文件的方式

BasicDataSourceFactory

static DataSource

createDataSource(Properties properties)
          Creates and configures a BasicDataSource instance based on the given properties.

public void setDdcpByProp() throws Exception{

      Propertiesprop=new Properties();

      InputStreaminputStream=JdbcUtils.class.getClassLoader().getResourceAsStream("dbcp.properties");

      prop.load(inputStream);

      DataSourceds=BasicDataSourceFactory.createDataSource(prop);

      Connectionconn=null;

      PreparedStatementpstate=null;

      try{

         conn=ds.getConnection();

         String sql="updateuser set username=? where id=?";

         pstate=conn.prepareStatement(sql);

         pstate.setString(1, "dbcpbyprop");

         pstate.setInt(2, 1);

         pstate.executeUpdate();

      }catch(Exception e){

         e.printStackTrace();

      }finally{

         JdbcUtils.release(conn, pstate);

      }

   }

3 C3P0

C3P0 是一个开源的JDBC连接池,他实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的扩展。目前他的开源项目有spring和hibernate。spring 和 hibernate对C3P0进行支持。

3.1     手动配置

public void setC3po() throws PropertyVetoException{

      ComboPooledDataSource cpds=newComboPooledDataSource();

      cpds.setDriverClass("com.mysql.jdbc.Driver");

      cpds.setJdbcUrl("jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=utf8");

      cpds.setUser("root");

      cpds.setPassword("1234");

      Connection conn=null;

      PreparedStatement pstate=null;

      try{

         conn=cpds.getConnection();

         String sql="updateusers set username=? where id=?";

         pstate=conn.prepareStatement(sql);

         pstate.setString(1, "eee");

         pstate.setInt(2, 1);

         pstate.executeUpdate();

      }catch(Exception e){

         e.printStackTrace();

      }finally{

         JdbcUtils.release(conn, pstate);

      }

   }

3.2     配置文件的方式

public void setC3poByXml() throws PropertyVetoException{

      ComboPooledDataSource cpds=newComboPooledDataSource();

      Connection conn=null;

      PreparedStatement pstate=null;

      try{

         conn=cpds.getConnection();

         String sql="updateusers set username=? where id=?";

         pstate=conn.prepareStatement(sql);

         pstate.setString(1, "xml");

         pstate.setInt(2, 1);

         pstate.executeUpdate();

      }catch(Exception e){

         e.printStackTrace();

      }finally{

         JdbcUtils.release(conn, pstate);

      }

   }

3.3     使用c3p0的JdbcUtils

private static final ComboPooledDataSource cpds=newComboPooledDataSource();

public static Connection getConnection() throws SQLException{

      return cpds.getConnection();

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值