Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package serveur.modele.persistance.pool;
- import java.util.*;
- import java.sql.*;
- import serveur.modele.transfertobject.ConnectionDBTO;
- public class ConnectionPoolManager {
- private final int MAX_POOL_SIZE = 10;
- private final int NB_MS_DELAIS_CONNECTION = 1000;
- String server = "";
- String port = "";
- String databaseUrl = "";
- String userName = "";
- String password = "";
- Vector<Connection> connectionPool = new Vector<Connection>();
- static ConnectionPoolManager instance;
- ConnectionDBTO connectionBDTO;
- public static ConnectionPoolManager getInstance() {
- if(instance == null) {
- instance = new ConnectionPoolManager();
- }
- return instance;
- }
- public ConnectionDBTO getConnectionDBTO(){
- if(connectionBDTO != null)
- return connectionBDTO;
- else
- return new ConnectionDBTO(server, port, userName, password);
- }
- public void setConnectionBDTO(ConnectionDBTO myConnectionBDTO){
- server = myConnectionBDTO.server;
- port = myConnectionBDTO.port;
- databaseUrl = "";
- userName = myConnectionBDTO.userName;
- password = myConnectionBDTO.password;
- connectionBDTO = new ConnectionDBTO(server, port, userName, password);
- }
- private ConnectionPoolManager() {
- initialize();
- }
- public ConnectionPoolManager(
- //String databaseName,
- String databaseUrl,
- String userName,
- String password
- ) {
- this.databaseUrl = databaseUrl;
- this.userName = userName;
- this.password = password;
- initialize();
- }
- private void initialize() {
- //Here we can initialize all the information that we need
- initializeConnectionPool();
- }
- private void initializeConnectionPool() {
- while(!checkIfConnectionPoolIsFull()) {
- //System.out.println("Connection Pool is NOT full. Proceeding with adding new connections");
- //Adding new connection instance until the pool is full
- connectionPool.addElement(createNewConnectionForPool());
- }
- //System.out.println("Connection Pool is full.\n\n");
- }
- private boolean checkIfConnectionPoolIsFull() {
- //Check if the pool size
- if(connectionPool.size() < MAX_POOL_SIZE) {
- return false;
- }
- return true;
- }
- //Creating a connection
- private Connection createNewConnectionForPool() {
- Connection connection = null;
- try{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- connection = DriverManager.getConnection(databaseUrl, userName, password);
- }
- catch(SQLException sqle) {
- System.err.println("SQLException: "+sqle);
- return null;
- }
- catch(ClassNotFoundException cnfe) {
- System.err.println("ClassNotFoundException: "+cnfe);
- return null;
- }
- return connection;
- }
- public Connection getConnectionFromPool() {
- Connection connection = null;
- //Check if there is a connection available. There are times when all the connections in the pool may be used up
- if(connectionPool.size() > 0) {
- connection = (Connection) connectionPool.firstElement();
- connectionPool.removeElementAt(0);
- } else {
- String erreur = "Il manque de connection : " + MAX_POOL_SIZE + " connections atteind";
- System.out.println(erreur);
- // tant que la connection est null
- while(connection == null) {
- // attend 1000 ms seconde
- try {
- Thread.currentThread().sleep(NB_MS_DELAIS_CONNECTION);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- // Notification dans le log file
- String erreurNbEssaie = "Delais de " + NB_MS_DELAIS_CONNECTION + " ms d'une connection : " + MAX_POOL_SIZE + " connections atteind";
- System.out.println(erreurNbEssaie);
- // reessaie d'obtenir une connection
- if(connectionPool.size() > 0) {
- connection = (Connection) connectionPool.firstElement();
- connectionPool.removeElementAt(0);
- }
- }
- }
- //Giving away the connection from the connection pool
- return connection;
- }
- public void returnConnectionToPool(Connection connection) {
- //Adding the connection from the client back to the connection pool
- //System.out.println("returned :" + connection);
- connectionPool.addElement(connection);
- }
- public void closeAllConnectionPool(){
- for(int i=0; i< connectionPool.size(); i++ ){
- try {
- connectionPool.get(i).close();
- //System.out.println("connection no : " + i + " closed");
- } catch (SQLException sqle) {
- System.err.println("SQLException closeAllConnection: " + sqle);
- }
- }
- // Clear all the connection in the pool
- connectionPool.clear();
- }
- public static void main(String args[]) {
- ConnectionPoolManager ConnectionPoolManager = new ConnectionPoolManager();
- Connection connection1 = ConnectionPoolManager.getConnectionFromPool();
- Connection connection2 = ConnectionPoolManager.getConnectionFromPool();
- ConnectionPoolManager.returnConnectionToPool(connection1);
- ConnectionPoolManager.getConnectionFromPool();
- /*
- InterfaceDAO monDAOMySQL = DAOMySQL.getInstance();
- monDAOMySQL.connection();
- System.out.println(monDAOMySQL.getRepresentation(1));
- System.out.println(monDAOMySQL.decrementerBillet(1,10));
- System.out.println(monDAOMySQL.getRepresentation(1));
- monDAOMySQL.incrementerBillet(1,10);
- System.out.println(monDAOMySQL.getRepresentation(1));
- monDAOMySQL.fermerConnection();
- */
- }
- }
Add Comment
Please, Sign In to add comment