class TestThread extends Thread { private DBPool pool; private long workTime = 0; private long foundStr = 0; @Override public void run() { workTime = System.currentTimeMillis(); // Connection con = null; PreparedStatement st = null; ResultSet rs = null; Random rnd = new Random();// for (int i = 0; i < 100; i++) { try { con = pool.getConnection();// // st = con.prepareStatement("SELECT a.* FROM test.test_table a WHERE id =?"); st.setObject(1, rnd.nextInt(10)); rs = st.executeQuery();// if (rs.next()) { String tmp = (rs.getString(2)); // if (tmp != null) { foundStr++; } } } catch (SQLException ex) { // , System.out.println("Pool " + pool + " exeption " + ex); } finally { // , try { if (rs != null) rs.close(); } catch (SQLException e) { //ignore } try { if (st != null) st.close(); } catch (SQLException e) { //ignore } try { if (con != null) pool.putConnection(con); // } catch (SQLException e) { //ignore } } } workTime = System.currentTimeMillis() - workTime; // } }
class DBPool { private String url, user, password; DBPool(String url, String user, String password) throws ClassNotFoundException { this.url = url; this.user = user; this.password = password; Class.forName("org.postgresql.Driver"); } public Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } public void putConnection(Connection connection) throws SQLException { connection.close(); } }
class DBPoolCache extends DBPool { private PGPoolingDataSource source; DBPoolCache(String host, String database, String user, String password) { source = new PGPoolingDataSource(); source.setDataSourceName("A Data Source"); source.setServerName(host); source.setDatabaseName(database); source.setUser(user); source.setPassword(password); source.setMaxConnections(20);// source.setInitialConnections(20);// } public Connection getConnection() throws SQLException { return source.getConnection(); } public void putConnection(Connection connection) throws SQLException { connection.close(); } }
class DBPoolCacheMy extends DBPool { private String url, user, password; private PGSimpleDataSource source; private BlockingQueue<Connection> connections = new ArrayBlockingQueue<Connection>(20); DBPoolCacheMy(String host, String database, String user, String password) throws SQLException { source = new PGSimpleDataSource(); source.setServerName(host); source.setDatabaseName(database); source.setUser(user); source.setPassword(password); for (int i = 0; i < 20; i++) {// connections.add(new MyConnection(source.getConnection())); } } public Connection getConnection() throws SQLException { try { // return connections.poll(2, TimeUnit.SECONDS); } catch (InterruptedException e) { return null; } } public void putConnection(Connection connection) throws SQLException { connections.add(connection); } }
class MyConnection implements Connection { private Connection connection; protected MyConnection(Connection connection) { this.connection = connection; } private ConcurrentHashMap<String, PreparedStatement> statements = new ConcurrentHashMap<String, PreparedStatement>(); public PreparedStatement prepareStatement(String sql) throws SQLException { PreparedStatement statement = statements.get(sql); if (statement == null) { statement = new MyStatement(connection.prepareStatement(sql)); statements.put(sql, statement); } return statement; } ..... }
class MyStatement implements PreparedStatement { private PreparedStatement statement; MyStatement(PreparedStatement statement) throws SQLException { this.statement = statement; ((PGStatement) statement).setPrepareThreshold(1); } public void close() throws SQLException { //ignore } ..... }
Source: https://habr.com/ru/post/194142/
All Articles