Guest User

Untitled

a guest
May 27th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. interface Connection {
  2. int read(); // reads an int from the connection
  3. void close(); // closes the connection
  4. }
  5.  
  6. class StreamConnection implements Connection {
  7. private final InputStream input;
  8. int read(){ return input.read(); }
  9. void close(){ input.close(); }
  10. }
  11.  
  12. class StreamConnectionPool {
  13. List<StreamConnection> freeConnections = openSomeConnectionsSomehow();
  14. StreamConnection borrowConnection(){
  15. if (freeConnections.isEmpty()) throw new IllegalStateException("No free connections");
  16. return freeConnections.remove(0);
  17. }
  18. void returnConnection(StreamConnection conn){
  19. freeConnections.add(conn);
  20. }
  21. }
  22.  
  23. class ConnectionPool {
  24.  
  25. private final StreamConnectionPool streamPool = ...;
  26.  
  27. Connection getConnection() {
  28. final StreamConnection realConnection = streamPool.borrowConnection();
  29. return new Connection(){
  30. private boolean closed = false;
  31. int read () {
  32. if (closed) throw new IllegalStateException("Connection closed");
  33. return realConnection.read();
  34. }
  35. void close() {
  36. if (!closed) {
  37. closed = true;
  38. streamPool.returnConnection(realConnection);
  39. }
  40. }
  41. protected void finalize() throws Throwable {
  42. try {
  43. close();
  44. } finally {
  45. super.finalize();
  46. }
  47. }
  48. };
  49. }
  50.  
  51. }
  52.  
  53. import java.sql.Connection;
  54. import java.sql.DriverManager;
  55. import java.sql.SQLException;
  56. import java.util.ArrayList;
  57. import java.util.List;
  58.  
  59.  
  60.  
  61. /** A Connection Pool with 5 Available Connections **/
  62. class ConnectionPool {
  63.  
  64. private List<Connection>availableConnections =
  65. new ArrayList<Connection>();
  66. private List<Connection>usedConnections = new ArrayList<Connection>();
  67. private final int MAX_CONNECTIONS = 5;
  68.  
  69. private String URL;
  70. private String USERID;
  71. private String PASSWORD;
  72.  
  73.  
  74. /** Initialize all 5 Connections and put them in the Pool **/
  75. public ConnectionPool(String Url, String UserId, String password)
  76. throws SQLException {
  77. this.URL = Url;
  78. this.USERID = UserId;
  79. this.PASSWORD = password;
  80.  
  81. for (int count = 0; count <MAX_CONNECTIONS; count++) {
  82. availableConnections.add(this.createConnection());
  83. }
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. /** Private function,
  93. used by the Pool to create new connection internally **/
  94.  
  95. private Connection createConnection() throws SQLException {
  96. return DriverManager
  97. .getConnection(this.URL, this.USERID, this.PASSWORD);
  98. }
  99.  
  100.  
  101.  
  102.  
  103. /** Public function, used by us to get connection from Pool **/
  104. public Connection getConnection() {
  105. if (availableConnections.size() == 0) {
  106. System.out.println("All connections are Used !!");
  107. return null;
  108. } else {
  109. Connection con =
  110. availableConnections.remove(
  111. availableConnections.size() - 1);
  112. usedConnections.add(con);
  113. return con;
  114. }
  115. }
  116.  
  117.  
  118.  
  119. /** Public function, to return connection back to the Pool **/
  120. public boolean releaseConnection(Connection con) {
  121. if (null != con) {
  122. usedConnections.remove(con);
  123. availableConnections.add(con);
  124. return true;
  125. }
  126. return false;
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /** Utility function to check the number of Available Connections **/
  134. public int getFreeConnectionCount() {
  135. return availableConnections.size();
  136. }
  137. }
Add Comment
Please, Sign In to add comment