Guest User

Untitled

a guest
Jan 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. package serveur.modele.persistance.pool;
  2.  
  3. import java.util.*;
  4. import java.sql.*;
  5.  
  6. import serveur.modele.transfertobject.ConnectionDBTO;
  7.  
  8.  
  9.  
  10. public class ConnectionPoolManager {
  11.  
  12.  
  13. private final int MAX_POOL_SIZE = 10;
  14. private final int NB_MS_DELAIS_CONNECTION = 1000;
  15.  
  16. String server = "";
  17. String port = "";
  18.  
  19. String databaseUrl = "";
  20. String userName = "";
  21. String password = "";
  22.  
  23. Vector<Connection> connectionPool = new Vector<Connection>();
  24.  
  25. static ConnectionPoolManager instance;
  26.  
  27. ConnectionDBTO connectionBDTO;
  28.  
  29.  
  30. public static ConnectionPoolManager getInstance() {
  31. if(instance == null) {
  32. instance = new ConnectionPoolManager();
  33. }
  34. return instance;
  35. }
  36.  
  37. public ConnectionDBTO getConnectionDBTO(){
  38.  
  39. if(connectionBDTO != null)
  40. return connectionBDTO;
  41. else
  42. return new ConnectionDBTO(server, port, userName, password);
  43. }
  44.  
  45. public void setConnectionBDTO(ConnectionDBTO myConnectionBDTO){
  46.  
  47. server = myConnectionBDTO.server;
  48. port = myConnectionBDTO.port;
  49.  
  50. databaseUrl = "";
  51. userName = myConnectionBDTO.userName;
  52. password = myConnectionBDTO.password;
  53.  
  54. connectionBDTO = new ConnectionDBTO(server, port, userName, password);
  55. }
  56.  
  57. private ConnectionPoolManager() {
  58. initialize();
  59. }
  60.  
  61. public ConnectionPoolManager(
  62. //String databaseName,
  63. String databaseUrl,
  64. String userName,
  65. String password
  66. ) {
  67. this.databaseUrl = databaseUrl;
  68. this.userName = userName;
  69. this.password = password;
  70. initialize();
  71. }
  72.  
  73. private void initialize() {
  74. //Here we can initialize all the information that we need
  75. initializeConnectionPool();
  76. }
  77.  
  78. private void initializeConnectionPool() {
  79. while(!checkIfConnectionPoolIsFull()) {
  80. //System.out.println("Connection Pool is NOT full. Proceeding with adding new connections");
  81. //Adding new connection instance until the pool is full
  82. connectionPool.addElement(createNewConnectionForPool());
  83. }
  84. //System.out.println("Connection Pool is full.\n\n");
  85. }
  86.  
  87. private boolean checkIfConnectionPoolIsFull() {
  88.  
  89.  
  90. //Check if the pool size
  91. if(connectionPool.size() < MAX_POOL_SIZE) {
  92. return false;
  93. }
  94.  
  95. return true;
  96. }
  97.  
  98. //Creating a connection
  99. private Connection createNewConnectionForPool() {
  100. Connection connection = null;
  101.  
  102. try{
  103. Class.forName("oracle.jdbc.driver.OracleDriver");
  104. connection = DriverManager.getConnection(databaseUrl, userName, password);
  105. }
  106. catch(SQLException sqle) {
  107. System.err.println("SQLException: "+sqle);
  108. return null;
  109. }
  110. catch(ClassNotFoundException cnfe) {
  111. System.err.println("ClassNotFoundException: "+cnfe);
  112. return null;
  113. }
  114.  
  115. return connection;
  116. }
  117.  
  118. public Connection getConnectionFromPool() {
  119. Connection connection = null;
  120.  
  121. //Check if there is a connection available. There are times when all the connections in the pool may be used up
  122. if(connectionPool.size() > 0) {
  123. connection = (Connection) connectionPool.firstElement();
  124. connectionPool.removeElementAt(0);
  125. } else {
  126. String erreur = "Il manque de connection : " + MAX_POOL_SIZE + " connections atteind";
  127. System.out.println(erreur);
  128.  
  129. // tant que la connection est null
  130. while(connection == null) {
  131.  
  132. // attend 1000 ms seconde
  133. try {
  134. Thread.currentThread().sleep(NB_MS_DELAIS_CONNECTION);
  135. } catch (InterruptedException e) {
  136. e.printStackTrace();
  137. }
  138.  
  139. // Notification dans le log file
  140. String erreurNbEssaie = "Delais de " + NB_MS_DELAIS_CONNECTION + " ms d'une connection : " + MAX_POOL_SIZE + " connections atteind";
  141. System.out.println(erreurNbEssaie);
  142.  
  143. // reessaie d'obtenir une connection
  144. if(connectionPool.size() > 0) {
  145. connection = (Connection) connectionPool.firstElement();
  146. connectionPool.removeElementAt(0);
  147. }
  148. }
  149. }
  150. //Giving away the connection from the connection pool
  151. return connection;
  152. }
  153.  
  154. public void returnConnectionToPool(Connection connection) {
  155. //Adding the connection from the client back to the connection pool
  156. //System.out.println("returned :" + connection);
  157. connectionPool.addElement(connection);
  158. }
  159.  
  160. public void closeAllConnectionPool(){
  161.  
  162. for(int i=0; i< connectionPool.size(); i++ ){
  163. try {
  164. connectionPool.get(i).close();
  165. //System.out.println("connection no : " + i + " closed");
  166.  
  167. } catch (SQLException sqle) {
  168. System.err.println("SQLException closeAllConnection: " + sqle);
  169. }
  170. }
  171. // Clear all the connection in the pool
  172. connectionPool.clear();
  173. }
  174.  
  175. public static void main(String args[]) {
  176. ConnectionPoolManager ConnectionPoolManager = new ConnectionPoolManager();
  177.  
  178. Connection connection1 = ConnectionPoolManager.getConnectionFromPool();
  179. Connection connection2 = ConnectionPoolManager.getConnectionFromPool();
  180. ConnectionPoolManager.returnConnectionToPool(connection1);
  181.  
  182. ConnectionPoolManager.getConnectionFromPool();
  183.  
  184. /*
  185.  
  186. InterfaceDAO monDAOMySQL = DAOMySQL.getInstance();
  187. monDAOMySQL.connection();
  188. System.out.println(monDAOMySQL.getRepresentation(1));
  189. System.out.println(monDAOMySQL.decrementerBillet(1,10));
  190. System.out.println(monDAOMySQL.getRepresentation(1));
  191. monDAOMySQL.incrementerBillet(1,10);
  192. System.out.println(monDAOMySQL.getRepresentation(1));
  193. monDAOMySQL.fermerConnection();
  194. */
  195. }
  196.  
  197. }
Add Comment
Please, Sign In to add comment