JDBC之自定义数据源

实现一个自定义的数据源需要实现javax.sql.DataSource接口,并重写获取连接的getConnection()方法,但是调用close方法关闭资源时不能去关闭资源,而是把资源归还给连接池,所以还需重写close方法。以下代码采用了装饰和适配两种设计模式来完成自定义数据源。

说明:本文JDBCUtils工具类在https://blog.csdn.net/qq_15076569/article/details/82191167已经附上源码

一:编写MyDataSource实现DataSource接口

public class MyDataSource implements DataSource{

    private List<Connection> list = new ArrayList<>();

    public MyDataSource(){
        for (int i=0;i<10;i++){
            Connection conn = JDBCUtils.getConnection();
            MyConnection myConnection = new MyConnection(conn, list);
            list.add(myConnection);
        }
    }

    @Override
    public Connection getConnection() throws SQLException {
        if(list != null && !list.isEmpty()){
            return list.remove(0);
        }
        return null;
    }

    /**
     * 归还连接对象
     * @param conn:连接对象
     */
    public void putSource(Connection conn){
        list.add(conn);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}

二:创建装饰类MyConnection,该装饰类需要实现Connection接口,但该接口方法众多,可以采用适配器模式编写一个适配类ConnectionWrapper来实现Connection接口,该装饰类直接继承ConnectionWrapper,重写getConnection及close方法

/**
 *适配类
 */
public class ConnectionWrapper implements Connection {
    private Connection conn;
    public ConnectionWrapper(Connection conn){
        super();
        this.conn = conn;
    }
    @Override
    public Statement createStatement() throws SQLException {
        return conn.createStatement();
    }

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        return conn.prepareStatement(sql);
    }

         .....

}

/**
 * 装饰类
 */
public class MyConnection extends ConnectionWrapper {

    private Connection conn = null;
    private List<Connection> list = null;
    public MyConnection(Connection conn, List<Connection> list){
        super(conn);
        this.conn = conn;
        this.list = list;
    }

    @Override
    public void close() throws SQLException {
        list.add(this);
        System.out.println("归还了...");
    }

}

三:使用自定义数据源

Connection conn = null;
Statement st = null;
ResultSet rs = null;
MyDataSource mc = new MyDataSource();
try {
    conn = mc.getConnection();
    conn.setAutoCommit(false);
    st = conn.createStatement();
    st.addBatch("update users set name = 'wangwu3' where id = '3232423425sdsfs'");
    int[] ints = st.executeBatch();
    conn.commit();
    System.out.println(Arrays.toString(ints));
} catch (SQLException e) {
    try {
        conn.rollback();
    } catch (SQLException e1) {
        e1.printStackTrace();
    }
    e.printStackTrace();
}finally {
    JDBCUtils.closeResource(rs,st,conn);
}

猜你喜欢

转载自blog.csdn.net/qq_15076569/article/details/82192272
今日推荐