hz90 / diy

0 stars 0 forks source link

db connection pool #2

Open hz90 opened 5 years ago

hz90 commented 5 years ago

package com.dbcon; public class DBConfigXML { public static final String JDBCDriver="com.mysql.jdbc.Driver"; public static final String JDBCURL="jdbc:mysql://localhost:3306/test"; public static final String JDBCUsername="fly"; public static final String JDBCPassword="123456"; //init connection count public static final int initCount=10; //连接池不足的时候增长的步进值 public static final int step=10; //max pool conneciton public static final int maxCount=10; }

package com.dbcon;

import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;

public class MyPooledConnection {

private Connection connnection;
private boolean isBusy = false;

public MyPooledConnection(Connection connection, boolean isbusy) {
    this.connnection = connection;
    this.isBusy = isbusy;
}

public void close() {
    this.isBusy = false;
}

public ResultSet query(String sql) {
    Statement statement;
    ResultSet resultSet = null;
    try {
        statement = connnection.createStatement();
        resultSet = statement.executeQuery(sql);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return resultSet;
}

public Connection getConnnection() {
    return connnection;
}

public void setConnnection(Connection connnection) {
    this.connnection = connnection;
}

public boolean isBusy() {
    return isBusy;
}

public void setBusy(boolean isBusy) {
    this.isBusy = isBusy;
}

}

package com.dbcon;

public interface FMyPool { public MyPooledConnection getMyPoolConnection(); public void createMyPooledConnection(int count); }

package com.dbcon;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Vector;

public class FMyDefaultPool implements FMyPool {

private Vector<MyPooledConnection> myPooledConnectionVector = new Vector<>();
private static String jdbcURL;
private static String jdbcUsername;
private static String jdbcPassWord;
private static int initcount;
private static int step;
private static int maxCount;

public FMyDefaultPool() {
    //初始化连接池配置
    init();
    try {
        Class.forName(DBConfigXML.JDBCDriver);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    //初始化数据库连接池管道
    createMyPooledConnection(initcount);
}

private void init() {
    jdbcURL = DBConfigXML.JDBCURL;
    jdbcUsername = DBConfigXML.JDBCUsername;
    jdbcPassWord = DBConfigXML.JDBCPassword;
    initcount = DBConfigXML.initCount;
    step = DBConfigXML.step;
    maxCount = DBConfigXML.maxCount;
}

@Override
public MyPooledConnection getMyPoolConnection() {
    if (myPooledConnectionVector.size() < 1) {
        throw new RuntimeException("connection init error");
    }
    MyPooledConnection myPooledConnection = null;

    try {
        myPooledConnection = getRealConnectionFromPool();
        while (myPooledConnection == null) {
            createMyPooledConnection(step);
            myPooledConnection = getRealConnectionFromPool();
            return myPooledConnection;
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return myPooledConnection;
}

private synchronized MyPooledConnection getRealConnectionFromPool() throws SQLException {
    for (MyPooledConnection myPooledConnection : myPooledConnectionVector) {
        if (!myPooledConnection.isBusy()) {
            if (myPooledConnection.getConnnection().isValid(3000)) {
                myPooledConnection.setBusy(true);
                return myPooledConnection;
            } else {
                Connection connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassWord);
                myPooledConnection.setConnnection(connection);
                myPooledConnection.setBusy(true);
                return myPooledConnection;
            }
        }
    }
    return null;
}

@Override
public void createMyPooledConnection(int count) {
    if (myPooledConnectionVector.size() > maxCount
            || myPooledConnectionVector.size() + count > maxCount) {
        throw new RuntimeException("connection is too many");
    }

    for (int i = 0; i < count; i++) {
        try {
            Connection connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassWord);
            MyPooledConnection myPooledConnection = new MyPooledConnection(connection, false);
            myPooledConnectionVector.add(myPooledConnection);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

}

package com.dbcon;

public class FMyPoolFactory { public static class CreatePool{ public static FMyPool mypool=new FMyDefaultPool(); }

public static FMyPool getInstance() {
    return CreatePool.mypool;
}

}

package com.dbcon;

import java.sql.ResultSet; import java.sql.SQLException;

public class TestPool { public static FMyPool fMyPool = FMyPoolFactory.getInstance();

public static void main(String[] args) {
    MyPooledConnection myPooledConnection=fMyPool.getMyPoolConnection();
    ResultSet resultSet=myPooledConnection.query("select * from test");
    try {
        while(resultSet.next()) {
            System.out.println(resultSet.getString(0));
        }
        myPooledConnection.close();

    } catch (SQLException e) {
        e.printStackTrace();
    }

}

}